系统仿真(MATLAB)实验2
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验二
一、实验目的:
1. Learn to design branch and loop statements program
2. Be familiar with relational and logical operators
3. Practice creating two-dimension plot
二、实验内容:
I. 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; ans =1
2. b>d; ans =0
3. a>b&c>d; ans =0
4. a==b; ans =0
5. a&b>c; ans =0
6. ~~b; ans =1
a=2; b=[1 –2;-0 10]; c=[0 1;2 0]; d=[-2 1 2;0 1 0];
7. ~(a>b) ans = 0 0
0 1
8. a>c&b>c ans =1 0
0 1
9. c<=d ??? Error using ==> <=
Matrix dimensions must agree
a=2; b=3; c=10; d=0;
10. a*b^2>a*c ans =0 11. d|b>a ans =1 12. (d|b)>a ans =0 a=20; b=-2; c=0; d=’Test’;
13. isinf(a/b) ans =0 14. isinf(a/c) ans =1 15. a>b&ischar(d) ans =1 16. isempty(c) ans =0
II. Write a Matlab program to solve the function
1()ln
1y x x
=- where x is a number <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. Solution: disp('This program solves for the value of a quadratic');
x=input('Enter the coefficient x='); if x>=1
disp('The value of x is legal!');
else
a=1/(1-x); y=log(a);
fprintf('y=%f\n',y);
end
III. 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 0 4.t ≤≤ t=0:1:10; a=0.5-0.25*i; f=a*t-1.0;
subplot(211);plot(t,abs(f));title('amp');xlabel('t');ylabel('amp(f)');grid on;
subplot(212);plot(t,angle(f));title('phase');xlabel('t');ylabel('phas
e(f)');grid on;
IV. Write the Matlab statements required to calculate y(t) from the equation
22350
()350t t y t t t -+≥⎧=⎨
+<⎩
for value of t between –9 and 9 in steps of 0.5. Use loops and
branches to perform this calculation. a=input('Please enter t='); if a>=0
t=0:0.5:9; y=-3*t.^2+5; plot(t,y);
title('Plot of y(t)'); xlabel('t'); ylabel('y(t)'); grid on; else
t=0:-0.5:-9;
y=3*t.^2+5; plot(t,y);
title('Plot of y(t)'); xlabel('t'); ylabel('y(t)'); grid on; end t>=0
t<0
V. Write an m.file to evalue the equation
2()32y x x x =-+for 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 3.0 thick dashed red line. First method: x=0.1:0.1:3; y=x.^2-3*x+2;
plot(x,y ,'b--','LineWidth',4.0); title('Plot of y(x)=x^2-3*x+2'); xlabel('x'); ylabel('y(x)');
grid on
Second method: n=1;
for x=0.1:0.1:3
y(n)=x.^2-3*x+2; n=n+1; end
x=0.1:0.1:3;
plot(x,y ,'b--','LineWidth',4.0);