目录
一.MATLAB Working Environment and Basic Operation
(三)Relational operations and logical operations
二.MATLAB Numerical Calculation and Plotting
中国矿业大学《Matlab系统仿真》笔记及考试重点
中国矿业大学《Matlab系统仿真》实验报告(即本文内容)
两者均可以在本人在资源中找到,免费的。
一.MATLAB Working Environment and Basic Operation
(一)Perform the following operations in the command window, record the results of operations, and observe the changes in the workspace.
1.
>> (365-52*2-70)/3
ans =
63.6667
2.
>> area=pi*2.5^2
area =
19.6350
3.
>> z1=(2*sin((85/180)*pi))/(1+exp(2))
z1 =
0.2375
4. , where
>> x=[2,1+2i;-0.45,5]
x =
2.0000 + 0.0000i 1.0000 + 2.0000i
-0.4500 + 0.0000i 5.0000 + 0.0000i
>> z2=(1/2)*log(x+(1+x^2)^(1/2))
z2 =
0.7182 - 0.0088i 0.7461 + 0.5434i
-0.1473 - 1.5679i 1.1555 - 0.0016i
5.Known x = 3, y = 4, calculate
>> x=[2,1+2i;-0.45,5]
x =
2.0000 + 0.0000i 1.0000 + 2.0000i
-0.4500 + 0.0000i 5.0000 + 0.0000i
>> z2=(1/2)*log(x+(1+x^2)^(1/2))
z2 =
0.7182 - 0.0088i 0.7461 + 0.5434i
-0.1473 - 1.5679i 1.1555 - 0.0016i
6. Assign the following matrix to m1;
Perform the following commands:
>>m1( 2 , 3 )
>>m1( 11 )
>>m1( : , 3 )
>>m1( 2 : 3 , 1 : 3 )
>>m1( 1 ,4 ) + m1( 2 ,3 ) + m1( 3 ,2 ) + m1( 4 ,1)
>> m1=[16 2 3 13;5 11 10 8;9 7 6 12;4 14 15 1];
>> m1(2,3)
ans =
10
>> m1(11)
ans =
6
>> m1(:,3)
ans =
3
10
6
15
>> m1(2:3,1:3)
ans =
5 11 10
9 7 6
>> m1(1,4)+m1(2,3)+m1(3,2)+m1(4,1)
ans =
34
7.Perform the following commands:
>>x=0:0.1:6*pi;
>>y=5*sin(x);
>>plot(x,y)
>> x=0:0.1:6*pi;
>> y=5*sin(x);
>> plot(x,y)
(二)Matrix operation
1.Determine whether the following operation is legal and why? If legal, provide the result.
a) result1 = a'
b) result2 = a * b
c) result3 = a + b
d) result4 = b * d
e) result5 = [b; c' ] * d
f) result6 = a. * b
g) result7 = a. / b
h) result8 = a. * c
i) result9 = a. \ b
j) result10 = a. ^2
k) result11 = a ^2
l) result12 = 2. ^ a
>> a=[1,2,3;4,5,6];
>> b=[2,4,-1;1,3,5];
>> c=[1,0,-2]';
>> d=[1,4,7;8,5,2;3,6,0];
>> result1=a'
result1 =
1 4
2 5
3 6
>> result2=a*b
错误使用 *
内部矩阵维度必须一致。
>> result3=a+b
result3 =
3 6 2
5 8 11
>> result4=b*d
result4 =
31 22 22
40 49 13
>> result5=[b;c']*d
result5 =
31 22 22
40 49 13
-5 -8 7
>> result6=a.*b
result6 =
2 8 -3
4 15 30
>> result7=a./b
result7 =
0.5000 0.5000 -3.0000
4.0000 1.6667 1.2000
>> result8=a.*c
矩阵维度必须一致。
>> result9=a.\b
result9 =
2.0000 2.0000 -0.3333
0.2500 0.6000 0.8333
>> result10=a.^2
result10 =
1 4 9
16 25 36
>> result11=a^2
错误使用 ^
一个参数必须为方阵,另一个必须为标量。请使用 POWER (.^) 执行按元素求幂。
>> result12=2.^a
result12 =
2 4 8
16 32 64
2.Solve the following equation using MATLAB.
>> x=[1,1,1,0;1,2,1,-1;2,-1,0,-3;3,3,5,-6]
x =
1 1 1 0
1 2 1 -1
2 -1 0 -3
3 3 5 -6
>> y=[1,8,3,5]'
y =
1
8
3
5
>> ans=inv(x)*y
ans =
1.0000
5.0000
-5.0000
-2.0000
3.A is known as:
a) Rank;
b) Determinant;
c) Inverse;
d) Eigenvalue and eigenvector.
>> a=[7,2,1,-2;9,15,3,-2;-2,-2,11,5;1,3,2,13]
a =
7 2 1 -2
9 15 3 -2
-2 -2 11 5
1 3 2 13
>> rank(a)
ans =
4
>> det(a)
ans =
1.2568e+04
>> inv(a)
ans =
0.1744 -0.0303 -0.0125 0.0270
-0.1050 0.0789 -0.0121 0.0006
0.0083 0.0173 0.0911 -0.0311
0.0095 -0.0185 -0.0103 0.0795
>> eig(a)
ans =
4.8554 + 0.0000i
12.6460 + 1.8333i
12.6460 - 1.8333i
15.8526 + 0.0000i
(三)Relational operations and logical operations
As known a = 20, b = 2, c = 0, d = 1
a) r1 = a > b
b) r2 = a > b & c > d
c) r3 = a == b* (-10)
d) r4 = ~b | c
>> a=20;b=-2;c=0;d=1;
>> r1=a>b
r1 =
logical
1
>> r2=a>b&c>d
r2 =
logical
0
>> r3=a==b*(-10)
r3 =
logical
1
>> r4=~b|c
r4 =
logical
0
(四)Exercises
1.Is the following variable name legal? Why?
a) x2
b) 3col
c) _row
d) for
(a)Yes
(b)No,it must begin with a letter.
(c)No,it must begin with a letter.
(d)No,it must be unique in the first 31 characters.
2.Construct a randomly generated 2-by-2 matrix of positive integers.
>> rand(2)
ans =
0.6324 0.2785
0.0975 0.5469
二.MATLAB Numerical Calculation and Plotting
(一)Experimental contents
1.Find the roots of polynomial and mark the roots in the complex plane map with ‘*’. (Using roots and plot functions).
>> p=[3 4 7 2 9 12]
p =
3 4 7 2 9 12
>> roots(p)
ans =
-0.8612 + 1.4377i
-0.8612 - 1.4377i
0.6737 + 1.0159i
0.6737 - 1.0159i
-0.9583 + 0.0000i
>> plot(ans,'*')
2.Find the roots of polynomial and mark the roots in the complex plane map with ‘*’. (Using roots and plot functions).
>> k=[1 0 0 0 0 -1];
>> roots(k)
ans =
-0.8090 + 0.5878i
-0.8090 - 0.5878i
0.3090 + 0.9511i
0.3090 - 0.9511i
1.0000 + 0.0000i
>> plot(ans,'*')
3. Attempt to find zero-crossing points of the following equation within [0.5, 4] (Using fzero function).
建立f.m文件
function y = f(x)
y=x.^3-2.*x.^2.*sin(x)+5.*x.*cos(x)+x.^(-1)
>>fun = @f;
>>x0=2.5;
>> z = fzero(fun,x0)
>> z =
2.6095
4.Measured data of a pressure sensor
Table E.1 Measure data
Where P is the pressure, and u is the voltage, attempt to fit the characteristic using the polynomial , and calculate a, b, c, and d. Then, plot the curve fitting and measure the data in the same figure.
>> p=[0 1.1 2.1 2.8 4.2 5.0 6.1 6.9 8.1 9 9.9]
p =
列 1 至 7
0 1.1000 2.1000 2.8000 4.2000 5.0000 6.1000
列 8 至 11
6.9000 8.1000 9.0000 9.9000
>> u=[10 11 13 14 17 18 22 24 29 34 39]
u =
10 11 13 14 17 18 22 24 29 34 39
>> polyfit(p,u,3)
ans =
0.0195 -0.0412 1.4469 9.8267
>> plot(ans)
(5)Attempt the following 3-D line plot command.
z=0:0.1:4*pi;
x=cos(z);
y=sin(z);
plot3(x,y,z)
>> z=0:0.1:4*pi;
>> x=cos(z);
>> y=sin(z);
>> plot3(x,y,z)
(6) Using mesh or surf function, plot the three-dimensional space surface expressed by the equation below, where x and y are limits to [-3, 3].
>> [x,y]=meshgrid(-3:0.1:3,-3:0.1:3);
>> z=-x.^2/10+y.^2/10;
>> mesh(x,y,z)
(二)Exercises
Obtain a hard copy of the plot of the functions f (x) = x^2, g (x) = x^3 for x =‑1, …,1 on the same axis. Label the x and y axes and create a legend indicating each graph.
>> x=linspace(-1,1);
>> f=x.^2;
>> g=x.^3;
>> plot(x,f,x,g)
>> hold on
>> xlabel('x');
>> ylabel('y');
>> legend('f(x)=x^2','g(x)=x^3')
三.Programming in MATLAB
(一)Experimental contents
1.Familiarity with the m-file
Start MATLAB, click File>>New>>M-File>>Open a new m-file. Edit the following program, and then save and run the model. Record the results and analysis of the program.
%classic "3n+1" problem from number theory.
while 1
n=input('Enter n,negative quits:');
if n<=0
break
end
a=n;
while n>1
if rem(n,2)==0
n=n/2;
else
n=3*n+1;
end
a=[a,n];
end
a
end
Question: How to stop the program?
Enter n,negative quits:2
a =
2 1
Enter n,negative quits:3
a =
3 10 5 16 8 4 2 1
Enter n,negative quits:4
a =
4 2 1
Enter n,negative quits:-2
2. Loop programming
- According to the equation approximate . Try when n=100, 1000, 10000.
- Calculate 1!+2!+…+10! using for or while.
(1)
m=0;
for k=1:3
n=input('请输入n的值:\n');
m=0;
for i=1:n
m=1/i^2+m;
end
x=(m*6)^(1/2);
fprintf('pi的值为:%f\n',x);
end
>> Untitled4
请输入n的值:
100
pi的值为:3.132077
请输入n的值:
1000
pi的值为:3.140638
请输入n的值:
10000
pi的值为:3.141497
(2)
x=0;
for n=1:10
m=1;
for i=1:n
m=m*i;
end
x=m+x;
end
fprintf('x=%d',x);
>> Untitled5
x=4037913
3. Conditional programming
Using if or switch to realize conversion of the volume score.
score ≥90, excellent
90> score ≥80,good
80> score ≥70 , mean
70> score ≥60 , passed
60< score, failed
x=input('your grade:');
if(x>=90)
a=1;
elseif((x>=80)&(x<90))
a=2;
elseif((x>=70)&(x<80))
a=3;
elseif((x>=60)&(x<70))
a=4;
else a=5;
end
switch a
case 1
disp('excellent');
case 2
disp('good');
case 3
disp('mean');
case 4
disp('passed');
otherwise
disp('failed');
end
>> Untitled4
your grade:98
excellent
4. Function
Write a function to calculate the value of the following equation, given the value of one x, after calling the function, return Y. Select some data to check your answer.
function y=luan(x)
if(x<=0)
y=sin(x);
elseif((x>0)&(x<=3))
y=x;
else
y=-x+6;
end
>>y=luan(3)
y =
3
>> y=luan(4)
y =
2
(二) Exercises
1. Write a script m-file called rand_int.m that once called within MATLAB gives a random integer.
function n=rand_int(x)
n=fix(rand*900+10);
>> rand_int
ans =
389
>> rand_int
ans =
834
2. The n-by-n Hilbert matrix H, has as its entries Hi,j = 1/(i + j – 1), i,j = 1, 2, …, n. Create a double “for loop” to generate the 5-by-5 Hilbert matrix and check your answer using the built-in MATLAB command hilb.
h=ones(5);
for i=1:5;
for j=1:5;
j=1/(i+j-1);
end
end
>> hilb(5)
ans =
1.0000 0.5000 0.3333 0.2500 0.2000
0.5000 0.3333 0.2500 0.2000 0.1667
0.3333 0.2500 0.2000 0.1667 0.1429
0.2500 0.2000 0.1667 0.1429 0.1250
0.2000 0.1667 0.1429 0.1250 0.1111
四.Simulink Application
Using Simulink to obtain the response during of the following current diagram.
Where
Figure E.1 Current diagram
The circuit diagram in Figure E-1 can be transformed into the following equation:
1. Start the Simulink, open new model file, and build the following model.
Figure E.2 Simulink model
2. Module parameters:
Initial condition of Integrator1 is 15 kV
Set R, L, and C, in the command window.
3. Open configuration parameters modify the window and set the parameters as follows:
Start time: 0
Stop time: 100e‑6
Solver Type: Variable-step
Solver: ode45
Max step size: 1e‑7
Min step size: Auto
Initial step size: Auto
Relative tolerance: 1e‑3
Absolute tolerance: 1e‑6
4. Run the model, and open the scope block to check the results.