MATLAB课件:ch6 Additional Data Dypes and Plot Types
MATLAB(CH6)Additional Data Dypes and Plot Types2MATLAB(CH6)Complex data String functions Multidimensional arrays Additional data types 2D 3D Plots36.1.1 Complex Variablesc=a+bi =z a=zcos b=zsin 22zab1tanba46.1.1 Complex Variables c=3+4ic=3.0000+4.0000i c=3+4jc=3.0000+4.0000i isreal(c)ans=0 isreal(c)ans=156.1.1 Complex Variables c=2 3;4 5c=2 3 4 5 isreal(c)ans=1 c=2 3;4 5ic=2.0000 3.0000 4.0000 0+5.0000i isreal(c)ans=066.1.2 Using Complex Numbers with Relational Operatorsn,=,c1=4+i*3;c2=3+i*8;c1c2 ans=176.1.2 Using Complex Numbers with Relational Operatorsn=,=Both the real parts and the imaginary parts are compared.c1=4+i*3;c2=4+i*8;c1=c2 ans=086.1.3 Complex Functionsn conj(c)a-bi n real(c)an imag(c)bn isreal(c)n isreal(c)n abs(c)zn angle(c)(radian)c=a+bi =z 9Example(Quadratic Equation)%Calc_roots_2.mdisp(This program solves for the roots of a quadratic);disp(Equation of the form A*x2+B*X+C=0.);a=input(Enter the coefficient A:);b=input(Enter the coefficient B:);c=input(Enter the coefficient C:);10discriminant=b2-4*a*c;x1=(-b+sqrt(discriminant)/(2*a);x2=(-b-sqrt(discriminant)/(2*a);disp(The roots of this equation are:);fprintf(x1=(%f)+i(%f)n,real(x1),imag(x1);fprintf(x2=(%f)+i(%f)n,real(x2),imag(x2);Example(Quadratic Equation)11 Calc_roots_2This program solves for the roots of a quadraticEquation of the form A*x2+B*X+C=0.Enter the coefficient A:1Enter the coefficient B:2Enter the coefficient C:5The roots of this equation are:x1=(-1.000000)+i(2.000000)x2=(-1.000000)+i(-2.000000)Example(Quadratic Equation)126.1.4 Plotting Complex Data0.2()e(cossin)ty ttitPlot the functiont=0:pi/20:4*piy=exp(-0.2*t).*(cos(t)+sin(t)if plot(t,y)is used,only the real data will be plotted!We should use:1.plot(t,real(y);or 2.plot(y);hold on;or 3.polar(angle(y),abs(y);plot(t,imag(y);136.1.4 Plotting Complex Datat=0:pi/20:4*pi;y=exp(-0.2*t).*(cos(t)+i*sin(t);plot(t,y,b-,LineWidth,5.0);title(bfplot of Complex Function vs Time);xlabel(bfitt);ylabel(bfity(t);grid on;14plot(t,y)Only the real data is shown.02468101214-0.6-0.4-0.200.20.40.60.81plot of Complex Function vs Timety(t)156.1.4 Plotting Complex Datat=0:pi/20:4*pi;y=exp(-0.2*t).*(cos(t)+i*sin(t);plot(t,real(y),b-,LineWidth,5.0);hold on;plot(t,imag(y),r-,LineWidth,5.0);title(bfplot of Complex Function vs Time);xlabel(bfitt);ylabel(bfity(t);legend(real part,imaginary part)grid on;16plot(t,real(y);hold on;plot(t,imag(y);02468101214-0.6-0.4-0.200.20.40.60.81plot of Complex Function vs Timety(t)real partimaginary part176.1.4 Plotting Complex Datat=0:pi/20:10*pi;y=exp(-0.2*t).*(cos(t)+i*sin(t);plot(y,b-,LineWidth,5.0);%(It is equal to plot(real(y),imag(y).title(bfPlot of Complex Function);xlabel(bfReal Part);ylabel(bfImaginary Part);axis equalgrid on;18plot(y)-0.500.51-0.4-0.200.20.40.6Plot of Complex Function Real PartImaginary Part196.1.4 Plotting Complex Datat=0:pi/20:10*pi;y=exp(-0.2*t).*(cos(t)+i*sin(t);polar(angle(y),abs(y);title(bfPlot of Complex Function);20polar(angle(y),abs(y)0.2 0.4 0.6 0.8 13021060240902701203001503301800Plot of Complex Function216.2 String Functionse.g.str=Today is Sundaystr=Today is Sunday whos Name Size Bytes Class str 1x15 30 char arrayGrand total is 15 elements using 30 bytes Each character is stored in two bytes of memory.226.2.1 String Conversion Functions x=double(str)x=Columns 1 through 10 84 111 100 97 121 32 105 115 32 83 Columns 11 through 15 117 110 100 97 121 str2=char(x+5)str2=Ytif%nx%Xzsif str3=char(double(str2)-5)str3=Today is Sunday236.2.2 Creating Two-Dimensional Character Arrayse.g.MyUnv=QingDao;University?Error using=vertcatAll rows in the bracketed expression must have the same number of columns.MyUnv=char(QingDao,University)MyUnv=QingDao University246.2.3 Concatenating StringsnFunction strcat concatenates two or more strings horizontally(delete trailing blanks)nFunction strvcat concatenates two or more strings vertically e.g.strcat(Red,Green)ans=RedGreen strvcat(Hello,World)ans=Hello World256.2.4 Comparing Strings 6.2.4.1 Comparing Strings for Equalityn strcmp Determines if two strings are identical;n strcmpi Determines if two strings are identical ignoring case;n strncmp Determines if the first n characters of two strings are identical;n strncmpi Determines if the first n characters of two strings are identical ignoring case;266.2.4 Comparing Strings 6.2.4.1 Comparing Strings for Equality276.2.4.2 Comparing Individual Characters for Equality and Inequalitye.g.a=QingDaoa=QingDao b=huangdaob=huangdao a=b?Error using=eqMatrix dimensions must agree.286.2.4.2 Comparing Individual Characters for Equality and Inequalitye.g.a=QingDao%pad with a blanka=QingDao b=huangdaob=huangdao a=bans=0 0 0 1 1 0 1 1,=,str=hello,worldstr=hello,world isletter(str)ans=Columns 1 through 13 0 1 1 1 1 1 0 0 1 1 1 1 1 isspace(str)ans=Columns 1 through 13 1 0 0 0 0 0 0 1 0 0 0 0 0 306.2.6.5 Number to Stringnstr=num2str(number)nstr=sprintf(format,number)str=num2str(pi)str=3.1416 str=sprintf(The value of pi is%8.6f.,pi)str=The value of pi is 3.141593.316.2.6.6 String to Number nvalue=str2double(str)nvalue=sscanf(str,format)e.g.a=3.1415926 str2double(a)ans=3.1416value1=sscanf(a,%g)value1=3.1416value2=sscanf(a,%d)value2=3 32Other String Functionsn findstrn strmatchn strrepn strtokn uppern lowern int2strn num2strn dec2hexn mat2strn sprintfn sscanf33Other String Functions346.3 Multidimensional Arrays356.3 Multidimensional Arrays c=randn(2,2,3)c(:,:,1)=0.53767 -2.2588 1.8339 0.86217c(:,:,2)=0.31877 -0.43359 -1.3077 0.34262c(:,:,3)=3.5784 -1.3499 2.7694 3.0349 ndims(c)ans=3 size(c)ans=2 2 3366.4 Additional 2-D Plotsx=1 2 3 4 5 6;y=2 6 8 7 8 5;stem(x,y);title(bfExample of a Stem Plot);xlabel(bfitx);ylabel(bfity);axis(0 7 0 10);37Additional 2-D Plots01234567012345678910Example of a Stem Plotxy38Additional 2-D Plotsn bar(x,y)n barth(x,y)n compass(x,y)n pie(x)n pie(x,explode)n stairs(x,y)n stem(x,y)39Additional 2-D Plots40fplotCreate a file,myfun,that returns a two-column matrix:function Y=myfun(x)Y(:,1)=200*sin(x(:)./x(:);Y(:,2)=x(:).2;Create a function handle pointing to myfun:fh=myfun;Plot the function with the statementfplot(fh,-20 20)41-20-15-10-505101520-50050100150200250300350400fplot426.5 3-D Plotst=(0:0.02:2)*pi;x=sin(t);y=cos(t);z=cos(2*t);plot3(x,y,z,b-);43plot3-1-0.8-0.6-0.4-0.200.20.40.60.81-1-0.8-0.6-0.4-0.200.20.40.60.81-1-0.8-0.6-0.4-0.200.20.40.60.81446.5 3-D Plots meshx,y=meshgrid(-4:0.2:4,-4:0.2:4);z=exp(-0.5*(x.2+0.5*y.2);mesh(x,y,z)xlabel(bfx);ylabel(bfy);zlabel(bfz);Use meshgrid to generate a legal 2D dot array before plotting a 3D plot.45mesh-4-2024-4-202400.20.40.60.81xyz46surfx,y=meshgrid(-4:0.2:4,-4:0.2:4);z=exp(-0.5*(x.2+0.5*(x-y).2);surf(x,y,z)xlabel(bfx);ylabel(bfy);zlabel(bfz);47surf-4-2024-4-202400.20.40.60.81xyz48x,y=meshgrid(-4:0.2:4,-4:0.2:4);z=exp(-0.5*(x.2+0.5*(x-y).2);contour(x,y,z)xlabel(bfx);ylabel(bfy);zlabel(bfz);contour49contourxy-4-3-2-101234-4-3-2-101234