matlab examples - plotting€¦ · → create a script in matlab (.m file) where you plot the...
TRANSCRIPT
![Page 1: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/1.jpg)
MATLABExamples
Hans-PetterHalvorsen,M.Sc.
Plotting
![Page 2: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/2.jpg)
Plotting>> x = 0:0.1:2*pi;>> y = sin(x);>> plot(x,y)
>> x = 0:0.1:2*pi;>> y = sin(x);>> y2 = cos(x);>> plot(x,y, x,y2)
...>> plot(x,y,'r*', x,y2,'g+')
![Page 3: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/3.jpg)
Plotting
Name Description
plot Create a Plot
figure Define a new Figure/Plot window
grid on/off Create Grid lines in a plot
title Add Title to current plot
xlabel Add a Label on the x-axis
ylabel Add a Label on the x-axis
axis Set xmin,xmax,ymin,ymax
hold on/off Add several plots in the same Figure
legend Create a legend in the corner (or at a specified position) of the plot
subplot Divide a Figure into several Subplots
>> x=0:0.1:2*pi;>> y=sin(x);>> plot(x,y)>> title('Plot Example')>> xlabel('x')>> ylabel('y=sin(x)')>> grid on>> axis([0,2*pi,-1,1])>> legend(’Temperature')
Plottingfunctions:
![Page 4: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/4.jpg)
Plotting
Day Rain Amount
Monday 2,1 mm
Tuesday 10 mm
Wednesday 9,7 mm
Thursday 6,2 mm
Friday 2,5 mm
Saturday 0 mm
Sunday 8,3 mm
GiventhefollowingRainDataforagivenWeek(MondaytoSunday):
Wewanttoplotthesevalues
![Page 5: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/5.jpg)
PlottingDay Rain
Amount
Monday 2,1 mm
Tuesday 10 mm
Wednesday 9,7 mm
Thursday 6,2 mm
Friday 2,5 mm
Saturday 0 mm
Sunday 8,3 mm
x = [2.1, 10, 9.7, 6.2, 2.5, 0, 8.5]>> plot(x, 'o')
Solution
![Page 6: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/6.jpg)
Plotting
Giventhefollowingfunction(−10 ≤ 𝑥 ≤ 10):
𝑦 𝑥 = 2𝑥* + 3𝑥 + 1
Wewill:• Plotthisfunction
• UsethePlottofindout:- Forwhichvalueof𝑥 is𝑓(𝑥) = 0?- Whatis𝑓(5) =?
![Page 7: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/7.jpg)
![Page 8: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/8.jpg)
PlotofDynamicSystemGiventheautonomoussystem(differentialequation):
�̇� = 𝑎𝑥
where𝑎 = − 45,where𝑇 isthetimeconstant
Thesolutionforthedifferentialequationis:𝑥 𝑡 = 𝑒9:𝑥;
Set𝑇 = 5andtheinitialcondition𝑥(0) = 1
→CreateaScriptinMATLAB(.mfile)whereyouplotthesolution𝑥(𝑡) inthetimeinterval0 ≤ 𝑡 ≤ 25→AddGrid,andproperTitleandAxisLabelstotheplot.
![Page 9: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/9.jpg)
clear, clc
%Define VariabesT=5;a=-1/T;
%Start Condition, etcx0=1;t=[0:1:25]
%Define the functionx=exp(a*t)*x0;
%Plottingplot(t,x);gridtitle('simulation of x'' = ax')xlabel('time')ylabel('x')
![Page 10: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/10.jpg)
Sub-plots
DisplayingMultiplePlotsinoneFigure– Sub-Plots
Wewillinthisexample:• PlotSin(x)andCos(x)in2differentsubplots.• AddTitlesandLabels.
subplot(m,n,p)
![Page 11: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/11.jpg)
% Define x-valuesx=0:0.01:2*pi;
% subplot 1subplot(2,1,1)plot(x, sin(x))title('Plotting sin(x)')xlabel('x')ylabel('sin(x)')
% Subplot 2subplot(2,1,2)plot(x, cos(x))title('Plotting cos(x)')xlabel('x')ylabel('cos(x)')
![Page 12: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/12.jpg)
Plotting- Subplot>> x=0:0.1:2*pi;>> y=sin(x);>> y2=cos(x);
>> subplot(2,1,1)>> plot(x,y)
>> subplot(2,1,2)>> plot(x,y2)
>> x=0:0.1:2*pi;>> y=sin(x);>> y2=cos(x);>> y3=tan(x);
>> subplot(3,1,1)>> plot(x,y)
>> subplot(3,1,2)>> plot(x,y2)
>> subplot(3,1,3)>> plot(x,y3)
>> x=0:0.1:2*pi;>> y=sin(x);>> y2=cos(x);>> y3=tan(x);>> y4=atan(x);
>> subplot(2,2,1)>> plot(x,y)
>> subplot(2,2,2)>> plot(x,y2)
>> subplot(2,2,3)>> plot(x,y3)
>> subplot(2,2,4)>> plot(x,y4)
![Page 13: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/13.jpg)
OtherPlots
• MATLABoffersmanydifferenttypesofplots:loglog,semilogx,semilogy,plotyy,polar,fplot,fill,area,bar,barh,hist,pie,errorbar,scatter.
• Wewilltrysomeofthem,e.g.,bar,hist andpie.
CheckoutthehelpforthedifferentplotfunctionsinMATLAB
![Page 14: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/14.jpg)
Wecreateabarplotusingthebar function:
>> x=rand(10,1)x =
0.65570.03570.84910.93400.67870.75770.74310.39220.65550.1712
>> bar(x)
![Page 15: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/15.jpg)
Usingthehist functiongives:
>> x=rand(10,1)x =
0.65570.03570.84910.93400.67870.75770.74310.39220.65550.1712
>> hist(x)
![Page 16: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/16.jpg)
Usingthepie functiongives:
>> x=rand(10,1)x =
0.65570.03570.84910.93400.67870.75770.74310.39220.65550.1712
>> pie(x)
![Page 17: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/17.jpg)
![Page 18: MATLAB Examples - Plotting€¦ · → Create a Script in MATLAB (.m file) where you plot the solution &(7) in the time interval 0 ≤ 7 ≤ 25 → Add Grid, and proper Title and](https://reader036.vdocuments.mx/reader036/viewer/2022062303/5fcf69107de22326e33f517f/html5/thumbnails/18.jpg)
Hans-PetterHalvorsen,M.Sc.
UniversityCollegeofSoutheastNorwaywww.usn.no
E-mail:[email protected]:http://home.hit.no/~hansha/