# Matlab Tutorials | Examples

## Practice 12:

### Plotting: Concentrations, curve fitting, 3D Gaussian plot

Create the three plot windows detailed below using the data in the file **practice12data.mat.**

Your plots should match the provided sample outputs. Pay attention to titles, x-y labels, grids, legends and their positions, colours etc. Running your main script should produce all three plots, in separate windows. You can start new figure windows by giving the commanding **figure**.

**Figure 1**

In the structure array named **reaction**, you can find the time and concentration measurements for the chemical reaction

Create the two plots shown below within a figure window.

The plot at the top should show the molecule concentration of the three components of the reaction versus time.

The plot at the bottom should show the reaction rate versus time, where this rate is calculated using:

**Figure 2**

In the structure array named **icecream**, temperature and total ice-cream sales for arbitrary days are saved.

Plot sales versus temperature, using dots with marker size of 20.

Then find the first order polynomial that best fits this data and plot it on top of the previous plot. Using the coefficients of this polynomial, come up with an approximate equation between temperature and sales, and use this equation as the title of the plot. In the title, you can round values to the nearest integers.

**Figure 3**

Write a function named **gaussian2d** that gets four variables from its caller in its parameter list:

- Matrix containing the x-values of the grid
- Matrix containing the y-values of the grid
- The mean vector μ containing [μx, μy]
- The standard deviation vector σ containing [σx, σy]

The function should evaluate the below 2-dimensional Gaussian distribution function for the given x and y grid matrices. The result will be a matrix containing the z-values. This matrix must be returned to the caller program.

In your main program, create the 50×50 X and Y grid matrices in the interval [-1,1]. Call your **gaussian2d** function twice to get two surface matrices **Z1** and **Z2**. The parameters for **Z1** and **Z2** (other than the **X** and **Y** grid matrices) are as follows:

In the third figure window, plot the surface** Z = Z1 + Z2.** Use colormap** jet** and view it from azimuth 45° and elevation 70°.

**Files: https://drive.google.com/drive/folders/1jM_HZDhz2_V7V03T1PjWXCT-MFwm4X7N?usp=sharing**

**Solution:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
%%Cemalettin Yılmaz close all; load('practice12data.mat'); %%First Plot of First Figure subplot(2,1,1); plot(reaction.time, reaction.NO2,'r'); hold on; plot(reaction.time, reaction.NO,'g'); hold on; plot(reaction.time,reaction.O2,'b'); grid; legend('NO_2','NO','O_2','Location','northeast'); title('2NO_2 -> 2NO + O_2'); ylabel('Concentration (mol/L)'); xlabel('Time(s)'); %%%% %%Calculate Rate for Second Plot for i=1:40 rate.NO(i)=(reaction.NO(i+1)-reaction.NO(i))/(reaction.time(i+1)-reaction.time(i)); rate.O2(i)=(reaction.O2(i+1)-reaction.O2(i))/(reaction.time(i+1)-reaction.time(i)); rate.NO2(i)=(reaction.NO2(i+1)-reaction.NO2(i))/(reaction.time(i+1)-reaction.time(i)); end %Create second plot under of firstplot subplot(2,1,2); plot(reaction.time(1:end-1), rate.NO2,'r'); hold on; plot(reaction.time(1:end-1), rate.NO,'g'); hold on; plot(reaction.time(1:end-1),rate.O2,'b'); grid; legend('NO_2','NO','O_2','Location','southeast'); ylabel('Rate (mol/Ls)'); xlabel('Time(s)'); %%% %%%Open New Figure figure scatter(icecream.temp,icecream.sales,25,'filled'); grid p=polyfit(icecream.temp,icecream.sales,1); f=polyval(p,icecream.temp); hold on plot(icecream.temp,f,'-r'); title(['Sales ~ ' num2str(round(p(1))) ' x Temperature ' num2str(round(p(2)))]); %%%Title can chage if polyfit changes %%%%3rd Figure figure z1meanvector = [-0.2,0]; z2meanvector = [0.2,0]; std = [0.15,0.3]; a=linspace(-1,1,50); [X,Y] = meshgrid(a,a); Z=gaussian2d(X,Y,z1meanvector,std) + gaussian2d(X,Y,z2meanvector,std); %%Call function 2 times and add them surf(X,Y,Z); view(45,70); %%View angle colormap jet title('Sum of two Gaussian distributions'); %%%End |

Hі there, just wanted to mention, I liked this post.

Ӏt was inspiring. Keep on posting!