Skip to content

YlmzCmlttn

Cemalettin Yılmaz Blog

Menu
  • Home
  • About Me
  • Projects
    • Iot-AR
    • Magnifi-AR
    • Smarthome-IOS
    • Others
  • Categories
    • Articles
    • Augmented Reality
    • Capture The Flag
      • Google CTF
        • 2018
    • Embedded Systems
    • IoT
    • Logisim
    • My Essays
    • Nvidia Jetson
      • Jetson TX1
    • Operating Systems
      • Kali
      • Raspbian
      • Ubuntu
    • Personal
    • Programming
      • Arduino
      • C
      • C#
      • Css
      • Html
      • Js
      • Matlab
      • Node.js
      • Python
      • Swift
      • VHDL
    • Projects
      • Embedded Systems
      • Electric
      • IoT
      • IoT-AR
      • Logisim
      • Magnifi-AR
      • Pose Estimation
    • Raspberry Pi
    • Xilinx
    • Others
Menu

Plotting: Concentrations, curve fitting, 3D Gaussian plot | Matlab Examples

Posted on December 8, 2018February 20, 2019 by Yılmaz Cemalettin

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:

 

MATLAB
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

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

My Motto

“Learn to share, Share to learn”

LinkedIn Badge

Cemalettin Yılmaz

Ads

Archives

Categories

  • Articles (1)
  • Augmented Reality (3)
  • Capture The Flag (23)
    • Google CTF (22)
      • 2018 (13)
      • 2019 (9)
    • PicoCTF (1)
      • 2019 (1)
  • Embedded Systems (3)
  • IoT (3)
  • Logisim (1)
  • My Essays (3)
  • Nvidia Jetson (5)
    • Xavier (5)
  • Operating Systems (24)
    • Kali (3)
    • Raspbian (2)
    • Ubuntu (21)
  • Others (1)
  • Personal (1)
  • Programming (44)
    • Arduino (4)
    • C (10)
    • C# (4)
    • C++ (5)
    • Css (1)
    • CUDA (6)
    • Html (1)
    • Js (2)
    • Libraries (5)
      • OpenCV (3)
      • OpenGL (2)
    • Matlab (14)
    • Node.js (5)
    • Python (6)
    • Swift (3)
  • Programs (4)
    • Tools (4)
  • Projects (21)
    • Books Solutions (8)
    • Electric (2)
    • Embedded Systems (2)
    • Energy Harvesting (1)
    • IoT (2)
    • IoT-AR (1)
    • Logisim (1)
    • Magnifi-AR (3)
    • Pose Estimation (3)
    • Smarthome-Ios (1)
  • Raspberry Pi (3)
  • Uncategorized (2)
  • YZlib (1)

Recent Posts

  • atof stof stod problems with local floating point separator in C/C++
  • Pico CTF 2019 Answers
  • YZlib: Personal C++ Library
  • Drive to target | Google CTF 2019
  • FriendSpaceBookPlusAllAccessRedPremium | Google CTF 2019

Recent Comments

  • AbaShelha on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Peter on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • Yılmaz Cemalettin on 16-Bit CPU on Logisim
  • Jenny on 16-Bit CPU on Logisim
  • MOON on 16-Bit CPU on Logisim
  • anti on Ghidra Installation on Ubuntu |18.04, 16.04, 14.04
  • hunkerjr on STOP GAN | Google CTF 2019
  • Shaq on 16-Bit CPU on Logisim
  • NURUL AFIQAH MOHD HASBULLAH on 16-Bit CPU on Logisim

Linkedln

© 2022 YlmzCmlttn | Powered by Superbs Personal Blog theme