MATLAB Programming for
Engineers
姓名:户桂民
学号:06030229
班级:0290601
日期:2008/06/04
One:objectives
Help the students to became more familiar with loops and branches. Two :contents
4.1
Command lines and results
y=0;
for t=-9:0.5:9;
if t<0
y=3.*t.^2+5;
fprintf('y(%5.2f)=%5.4f\n',t,y);
else
y=-3.*t.^2+5;
fprintf('y(%5.2f)=%5.4f\n',t,y);
end
end
>> y(-9.00)=248.0000
y(-8.50)=221.7500
y(-8.00)=197.0000
y(-7.50)=173.7500
y(-7.00)=152.0000
y(-6.50)=131.7500
y(-6.00)=113.0000
y(-5.50)=95.7500
y(-5.00)=80.0000
y(-4.50)=65.7500
y(-4.00)=53.0000
y(-3.50)=41.7500
y(-3.00)=32.0000
y(-2.50)=23.7500
y(-2.00)=17.0000
y(-1.50)=11.7500
y(-1.00)=8.0000
y(-0.50)=5.7500
y( 0.00)=5.0000
y( 0.50)=4.2500
y( 1.00)=2.0000
y( 1.50)=-1.7500
y( 2.50)=-13.7500
y( 3.00)=-22.0000
y( 3.50)=-31.7500
y( 4.00)=-43.0000
y( 4.50)=-55.7500
y( 5.00)=-70.0000
y( 5.50)=-85.7500
y( 6.00)=-103.0000
y( 6.50)=-121.7500
y( 7.00)=-142.0000
y( 7.50)=-163.7500
y( 8.00)=-187.0000
y( 8.50)=-211.7500
y( 9.00)=-238.0000
4.2
Command lines and results
t1=-9:0.5:-0.5;
y1=3.*t1.^2+5;
t2=0:0.5:9;
y2=-3.*t2.^2+5;
for m=1:18
fprintf('y(%5.2f)=%5.3f\n',t1(m),y1(m)); end
for m=1:19
fprintf('y(%5.2f)=%5.3f\n',t2(m),y2(m)); end
>> y(-9.00)=248.000
y(-8.50)=221.750
y(-8.00)=197.000
y(-7.50)=173.750
y(-7.00)=152.000
y(-6.50)=131.750
y(-6.00)=113.000
y(-5.50)=95.750
y(-5.00)=80.000
y(-4.50)=65.750
y(-4.00)=53.000
y(-3.50)=41.750
y(-3.00)=32.000
y(-2.50)=23.750
y(-1.50)=11.750
y(-1.00)=8.000
y(-0.50)=5.750
y( 0.00)=5.000
y( 0.50)=4.250
y( 1.00)=2.000
y( 1.50)=-1.750
y( 2.00)=-7.000
y( 2.50)=-13.750
y( 3.00)=-22.000
y( 3.50)=-31.750
y( 4.00)=-43.000
y( 4.50)=-55.750
y( 5.00)=-70.000
y( 5.50)=-85.750
y( 6.00)=-103.000
y( 6.50)=-121.750
y( 7.00)=-142.000
y( 7.50)=-163.750
y( 8.00)=-187.000
y( 8.50)=-211.750
y( 9.00)=-238.000
4.3
Command lines and results
fprintf('even_number \t Its_square\n');
for m=0:2:50;
fprintf(' %4.1f \t\t\t %6.2f\n',m,m^2); end
>> even_number Its_square
0.0 0.00
2.0 4.00
4.0 16.00
6.0 36.00
8.0 64.00
10.0 100.00
12.0 144.00
14.0 196.00
16.0 256.00
18.0 324.00
20.0 400.00
22.0 484.00
24.0 576.00
26.0 676.00
28.0 784.00
30.0 900.00
32.0 1024.00
34.0 1156.00
36.0 1296.00
38.0 1444.00
40.0 1600.00
42.0 1764.00
44.0 1936.00
46.0 2116.00
48.0 2304.00
50.0 2500.00 4.4
Command lines and results
x=0.1:0.1:3;
y=x.^2-3.*x+2;
plot(x,y,'r--','LineWidth',3);
y=0;
t=0;
for x=0.1:0.1:3;
t=t+1;
y(t)=x^2-3*x+2;
end
xx=0.1:0.1:3;
plot(xx,y,'r--','LineWidth',3);
4.5
Command lines and results
%计算阶乘的函数程序
x=input('Please input a integer(no negative):\n'); if x<0
fprintf('Error input!\n');
elseif x==0
fprintf('0!=1');
else
mm=1;
for m=1:x
mm=mm*m;
end
fprintf('%d!=%d',x,mm);
end
% hu5是M文件的名字
>> hu5
Please input a integer(no negative):
-9
Error input!
>> hu5
Please input a integer(no negative):
0!=1
>> hu5
Please input a integer(no negative):
5
5!=120
4.6
Command lines and results
(a)
>> m=0;
>> for ii=-32768:32767
m=m+1;
end
>> m
m =
65536
(b)
>> m=0;
for ii=32768:32767
m=m+1;
end
>> m
m =
(c)
>> m=0;
for kk=2:4:3
m=m+1;
end
>> m
m =
1
(d)
>> m=0;
for jj=ones(5,5)
m=m+1;
end
>> m
m =
5
4.7
Command lines and results (a)
>> ires=0;
>> for index=-10:10
ires=ires+1;
end
>> ires
ires =
21
(b)
ires=0;
for index=10:-2:4
if index==0
continue;
end
ires=ires+index; end
>> ires
ires =
28
>> index
index =
4
(c)
ires=0;
for index=10:-2:4
if index==0
break;
end
ires=ires+index; end
>> index
index =
4
>> ires
ires =
28
(d)
ires=0;
for index1=10:-2:4
for index2=2:2:index1
if index2==6
break
end
ires=ires+index2;
end
end
>> ires
ires =
24
>> index1
index1 =
4
>> index2
index2 =
4
4.8
Command lines and results
(a)
ires=1;
index=0;
while mod(ires,10)~=0
ires=ires+1;
index=index+1; end
>> ires
ires =
10
>> index
index =
9
(b)
ires=2;
index=0;
while ires<=200
ires=ires^2;
index=index+1; end
>> index
index =
3
>> ires
ires =
256
(c)
ires=2;
index=0;
while ires>200
ires=ires^2;
index=index+1; end
>> index
index =
>> ires
ires =
2
4.9
Command lines and results
(a)
arr1=[1 2 3 4; 5 6 7 8; 9 10 11 12]; mask=mod(arr1,2)==0;
arr1(mask)=-arr1(mask);
>> arr1
arr1 =
1 -
2
3 -4
5 -
6
7 -8
9 -10 11 -12
(b)
arr1=[1 2 3 4; 5 6 7 8; 9 10 11 12];
arr2=arr1<=5;
arr1(arr2)=0;
arr1(~arr2)=arr1(arr2).^2;
>> arr1
arr1 =
0 0 0 0
0 36 49 64
81 100 121 144 6.6
Command lines and results
t=0:0.1:10;
v=10.*exp(-0.2.*t).*(cos(pi.*t)+i.*sin(pi.*t)); plot(t,v);
title('Plot(t,v)');
xlabel('t');
ylabel('v(t)');
6.7
Command lines and results
t=0:0.1:10;
v=10.*exp(-0.2.*t).*(cos(pi.*t)+i.*sin(pi.*t)); plot(v);
title('Plot(v)');
xlabel('t');
ylabel('v(t)');
6.8
Command lines and results
t=0:0.1:10;
v=10.*exp(-0.2.*t).*(cos(pi.*t)+i.*sin(pi.*t)); polar(angle(v),abs(v));
title('Polar(angle(v),abs(v)');
Command lines and results
subplot(2,2,1);
[x,y]=meshgrid(-1:0.02:1,-2*pi:0.1:2*pi); z=exp(x+i.*y);
mesh(x,y,real(z));
title('mesh figure');
xlabel('X');
ylabel('Y');
zlabel('Z');
subplot(2,2,2);
[x,y]=meshgrid(-1:0.02:1,-2*pi:0.1:2*pi); z=exp(x+i.*y);
surf(x,y,real(z));
title('surface figure');
xlabel('X');
ylabel('Y');
zlabel('Z');
subplot(2,2,3);
[x,y]=meshgrid(-1:0.02:1,-2*pi:0.1:2*pi); z=exp(x+i.*y);
contour(x,y,real(z));
title('contour figure');
xlabel('X');
ylabel('Y');
zlabel('Z');
Command lines and results
str=input('Please input a string:','s');
m=findstr(str,'a');
n=size(m);
fprintf('Appearred %d times !',n(2));
Please input a string:and me aaa Appearred 4 times !
6.13
Command lines and results
str=input('Please input a string:','s');
m=findstr(str,'a');
n=size(m);
p=findstr(str,'A');
q=size(p);
fprintf('Appearred %d times !',n(2)+q(2));
Please input a string:AAAaaa Appearred 6 times !
6.17
Command lines and results
str=input('Please input a string:','s');
str=lower(str);
m=isletter(str);
for n=1:length(m)
if m(n)==1
str(n)=upper(str(n));
q=n;
break
end
end
for nn=q:length(m)
if (m(nn)==0)&(m(nn+1)==1)
str(nn+1)=upper(str(nn+1));
end
end
disp(str);
(h617是M文件名)
>> h617
Please input a string:hOw beautiFul yOU are/// rIght How Beautiful You Are/// Right
6.18
Command lines and results
subplot(2,2,1);
x=0:0.1:2;
y=exp(-x).*sin(x);
stem(x,y);
title('(a) stem figure');
xlabel('X');
ylabel('Y');
subplot(2,2,2);
x=0:0.1:2;
y=exp(-x).*sin(x);
stairs(x,y);
title('(b) stair figure');
xlabel('X');
ylabel('Y');
subplot(2,2,3);
x=0:0.1:2;
y=exp(-x).*sin(x);
bar(x,y);
title('(c) bar figure');
xlabel('X');
ylabel('Y');
subplot(2,2,4);
x=0:0.1:2;
y=exp(-x).*sin(x);
compass(x,y);
title('(d) compass figure');
xlabel('X');
ylabel('Y');
6.19
Command lines and results
x=[5 10 7 5 15];
y=[0 1 0 0 0];
pie(x,y);
title('\bfContrbution Figure');
legend('George','Sam','Betty','Charlie','Suzie');
6.20
Command lines and results
fplot('1/sqrt(x)',[0.1,10.0]);
title('y=1/sqrt(x)');
xlabel('X');
ylabel('Y');
Three: Experience ………………………………………………