A Concise Introduction to MATLAB.docx

A Concise Introduction to MATLAB – William J. Palm III Chapter 5 Advanced Plotting and Model Building 5.1 xy Plotting Fu

Views 133 Downloads 6 File size 1MB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

A Concise Introduction to MATLAB – William J. Palm III Chapter 5 Advanced Plotting and Model Building 5.1 xy Plotting Functions Plots of complex Numbers >> z=0.1+0.9i; >> n=[0:0.01:10]; >> plot(z.^n),xlabel('Real'),ylabel('Imaginary')

The Function Plot Command fplot

>> f=@(x) (cos(tan(x))-tan(sin(x))); >> fplot(f,[1 2])

Plotting Polynomials >> x=[-6:0.01:6]; >> p=[3,2,-100,2,-7,90]; >> plot(x,polyval(p,x)),xlabel('x'),ylabel('p')

T5.1-1 Plot the equation y  0.4 1.8 x for 0  x  35 and 0  y  3.5 >> x=0:0.01:35; >> y=0.4.*sqrt(1.8*x); >>plot(x,y),xlabel('x'),ylabel('y'),title('y=0.4sqrt(1.8x)'),axi s([0 35 0 35]),grid

>> x=0:0.01:35; >> y=0.4.*sqrt(1.8*x); >> plot(x,y),xlabel('x'),ylabel('y'),title('Function y'),grid

T 5.1-2 use fplot command to plot the function

tan(cos( x))  sin(tan( x)) for 0  x  2 >> f=@(x)(tan(cos(x))-sin(tan(x))); >> fplot(f,[0 2*pi])

T5.1-3 Plot (0.2  0.8i)n for 0  n  20 >> x=0.2+0.8i; >> n=0:0.001:20; >> plot(x.^n),xlabel('Real'),ylabel('Imaginary'),title('Complex Plot'),axis([0 20 0 x.^n])

5.2 Additional Commands and Plot types – Subplots >> cd F:\MATLAB_PRA >> edit x=[0:0.01:5]; y=exp(-1.2*x).*sin(10*x+5); subplot(1,2,1),plot(x,y),xlabel('x'),ylabel('y'),axis([0 5 -1 1]) x=[-6:0.01:6]; y=abs(x.^3-100); subplot(1,2,2),plot(x,y),xlabel('x'),ylabel('y'),axis([-6 6 0 350]) Ctrl+Ssubplt1 To execute >> suplt1

T5.2-1 >> cd F:\MATLAB_PRA >> edit t=0:0.001:8; v=-8:0.001:8; z=exp(-0.5*t).*cos(20*t-6); u=6.*log10(v.^2+20); subplot(2,1,1),plot(t,z),xlabel('x'),ylabel('z'),title('Graph1'),grid subplot(2,1,2),plot(v,u),xlabel('x'),ylabel('u'),title('Graph2'),grid Ctrl+Ssubplt2 To execute >> suplt2

Labeling Curves and Data >> cd F:\MATLAB_PRA >> x=[0:0.01:2]; >> y=sinh(x); >> z=tanh(x); >> plot(x,y,x,z,'--'),xlabel('x'),ylabel('sinh and tanh'),legend('sinh(x)','tanh(x)')

The hold command >> x=[-1:0.01:1]; >> n=[0:0.01:10]; >> y1=3+exp(-x).*sin(6*x); >> y2=4+exp(-x).*cos(6*x);z=0.1+0.9i; >> plot(y1,y2),xlabel('x'),ylabel('y'),hold,plot(z.^n),title('Two plots'),gtext('y2,y1 plot'),gtext('Complex plots') Current plot held

T5.2-2 >> x=[0,1,2,3,4,5]; >> y1=[11,13,8,7,5,9]; >> y2=[2,4,5,3,2,4]; >> plot(x,y1,'-o',x,y2,'-d'),xlabel('x'),ylabel('y1&Y2'),title('Two curves'),legend('y1','y2'),grid

T5.2-3 >> x=0:0.001:2; >> y1=cosh(x); >> y2=0.5*exp(x); >> plot(x,y1,x,y2,'--'),xlabel('x'),ylabel('y1&y2'),title('Two Curves'),legend('cosh(x)','0.5.e^{x}'),grid

T5.2-4 >> x=0:0.001:2; >> y1=sinh(x); >> y2=0.5*exp(-x); >> plot(x,y1,'r',x,y2,'k'),ylabel('x'),ylabel('y1&y2'),title('Two Curves'),title('0.5.e^{x}'),text(1,8,'exp curve'),gtext('sinhx'),grid

T5.2-5 >> x=0:0.001:1; >> y1=sin(x); >> y2=x-(x.^3./3); >> plot(x,y1),xlabel('x'),ylabel('y1,y2'),hold,plot(x,y2,'k'),gtext ('y1'),gtext('y2'),title('Holding Graphs'),grid Current plot held

Log log plot of the function

y >> >> >> >> >> >>

100(1  0.01x 2  0.02 x 2 (1  x 2 )2  0.1x 2

x=logspace(-1,2,500); u=x.^2; num=100*(1-0.01*u).^2+0.02*u; den=(1-u).^2+0.1*u; y=sqrt(num./den); loglog(x,y),xlabel('x'),ylabel('y')

Script

file:Hermite

Contents    

0.1  x  100

Define the range Hermite family Plots

Define the range t=-5:0.1:5;

Hermite family H0=exp(-t.^2./4); H1=t.*exp(-t.^2./4); H2=(t.^2-1).*exp(-t.^2./4); H3=(t.^4-6*t.^2+3).*exp(-t.^2./4);

Plots plot(t,H0,'*:',t,H1,'d-.',t,H2,'h--',t,H3,'s-') xlabel('t'),ylabel('Amplitude') title('First four members of the Hermite family') legend('Her 0','Her 1','Her 2','Her 3')

Script File:lagu.m Contents   

Define time range Laguerre polynomials Plots

Define time range t=0:0.1:15;

Laguerre polynomials L0=exp(-t./2); L1=(1-t).*t.*exp(-t./2); L2=(1-2.*t+0.5.*t.^2).*exp(-t./2); L3=(t.^3-3.*t).*exp(-t.^2/.4);

Plots plot(t,L0,'*:',t,L1,'d-.',t,L2,'s-',t,L3,'p:') xlabel('t'),ylabel('Amplitude') title('Members of Laguerre

family')

legend('Lag 0','Lag 1','Lag 2','Lag 3')

Published with MATLAB® R2018a

>> >> >> >>

x1=0:0.001:100; y1=sin(x1); y2=tan(x1); plotyy(x1,y1,x1,y2)

Plotting Orbits r

p 1   cos 

>> >> >> >> >>

p=2; e=0.5; theta=[0:pi/90:2*pi]; r=p./(1-e.*cos(theta)); polar(theta,r),title('Orbital Eccentricity=0.5')

>> x=0:0.001:1.5; >> y1=2.*x.^(-0.5); >> y2=10.^(1-x); >> subplot(2,2,1),plot(x,y1,x,y2),xlabel('x'),ylabel('y'),gtext('Ex ponential'),gtext('power'),subplot(2,2,2),semilogy(x,y1,x,y2),xl abel('x'),ylabel('y'),gtext('Exponential'),gtext('power'),subplo t(2,2,3),loglog(x,y1,x,y2),xlabel('x'),ylabel('y'),gtext('Expone ntial'),gtext('power')

>> >> >> >>

theta=[0:pi/90:4*pi]; a=2; r=a.*theta; polar(theta,r),title('Spiral of Archimedes')

>> contour(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z') >> meshc(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z') >> Z=X.*exp(-(X.^2+Y.^2)); >> subplot(2,2,1),mesh(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z'),s ubplot(2,2,2),meshc(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z'),s ubplot(2,2,3),meshz(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z'),s ubplot(2,2,4),waterfall(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z ')

Contents  

Script file:sincn.m Plots

Script file:sincn.m % Define time range

t=-5*pi:0.25:5*pi; % sinc functions

s0=sin(t)./(pi.*t); s1=sin(t-pi)./(pi*(t-pi)); s2=sin(t-2*pi)./(pi*(t-2*pi)); s3=sin(t-3*pi)./(pi*(t-3*pi));

Plots plot(t,s0,'*:',t,s1,'d-.',t,s2,'h--',t,s3,'s-') xlabel('t'),ylabel('Amplitude') title('Members of sinc family') legend('sinc 0','sinc 1','sinc 2','sinc 3')

>> image(magic(10)),title('Image pattern of Magic Square')