MATLAB实验二
一、实验目的:
1. Learn to design branch and loop statements program 2. Be familiar with relational and logical operators 3. Practice 2D plotting
二、实验内容:
1. Assume that a,b,c, and d are defined, and evaluate the following expression. a=20; b=-2; c=0; d=1;
(1) a>b;e=a>b;e=1;(2) b>d; f=b>d;f=0;(3) a>b&c>d;g=a>b&c>d;g=0; (4) a==b; h=(a==b);h=0;(5) a&b>c;k=a&b>c;k=0;(6) ~~b; l=~~b;l=1; a=2; b=[1 –2;-0 10]; c=[0 1;2 0]; d=[-2 1 2;0 1 0];
(7) ~(a>b)e=~(a>b);(8) a>c&b>cf=a>c&b>c;
(9) c<=dError using <= Matrix dimensions must agree. a=2; b=3; c=10; d=0;
(10) a*b^2>a*ce= a*b^2>a*c ;e=0; (11) d|b>af=d|b>a ;f=1; (12) (d|b)>ag=(d|b)>a;g=0;
a=20; b=-2; c=0; d=’Test’;
(13) isinf(a/b)e=isinf(a/b); e=0; (14) isinf(a/c)f=isinf(a/c) ; f=1; (15) a>b&ischar(d)g=a>b&ischar(d);g=1 (16) isempty(c)h=isempty(c);h=0;
12. Write a Matlab program to solve the functiony(x)ln, where x is a number
1x<1. Use an if structure to verify that the value passed to the program is legal. If
the value of x is legal, caculate y(x). If not ,write a suitable error message and quit.
x=input('please input x=') if(x<1)
y=log(1/(1-x)); disp('y='); disp(y); else
disp('The value of x is illegal'); end
1
3. Write out m. file and plot the figures with grids
Assume that the complex function f(t) is defined by the equation
f(t)=(0.5-0.25i)t-1.0
Plot the amplitude and phase of function for 0t4. t=[0:0.001:4];
m=sqrt((-0.25.*t).^2+(0.5.*t-1).^2) n=atan((-0.25.*t)./(0.5.*t-1)) plot(t,m,'--') holdon; plot(t,n) holdoff;
4. Write the Matlab statements required to calculate y(t) from the equation
3t25t0 y(t)253tt0for value of t between –9 and 9 in steps of 0.5. Use loops and branches to perform this calculation. for t=-9:0.5:9 if t<0 y=3*t^2+5 else
2
y=-3*t^2+5 end end
y =248 y =221.7500 y =197 y =173.7500 y =152 y =131.7500 y =113 y =95.7500 y =80 y =65.7500 y =53 y =41.7500 y = 32 y =23.7500 y =17 y =11.7500 y = 8 y =5.7500 y = 5 y = 4.2500 y =2 y = -1.7500 y =-7 y =-13.7500 y =-22 y =-31.7500 y = -43 y =-55.7500 y =-70 y =-85.7500 y=-103 y =-121.7500 y =-142 y =-163.7500 y =-187 y =-211.7500 y =-238
5. Write anm.file to evalue the equation y(x)x23x2for all values of x
between 0.1 and 3, in steps of 0.1. Do this twice, once with a for loop and once with vectors. Plot the resulting function using a 4.0 thick dashed blue line.
for x=0.1:0.1:3 y=x^2-3*x+2; plot(x,y,'bo'); holdon; end
x=0.1:0.1:3; y=x.^2-3*x+2;
plot(x,y,'b--','LineWidth',4.0)
3