MATLAB作业姚转菊.docx
《MATLAB作业姚转菊.docx》由会员分享,可在线阅读,更多相关《MATLAB作业姚转菊.docx(18页珍藏版)》请在冰点文库上搜索。
MATLAB作业姚转菊
MATBAL
编
程
作
业
12级电信一班姚转菊
学号:
20121060107
2013.6.20
第三章MATLAB程序设计
例3.2.实现变量互换的功能
function[x,y]=fexch(x,y)
%将不同变量的值互换
z=x;x=y;y=z
>>clear;
>>x=15:
-1:
1;
>>y=[1234;5678];
>>[a,b]=fexch(x,y)
y=
151413121110987654321
a=
1234
5678
b=
151413121110987654321
例3.7.检测输入值是否为-1、0、1。
x=input('请输入x的值:
');
ifx==-1
disp('negativeone');
elseifx==0
disp('zeroc');
elseifx==1
disp('positiveone');
else
disp('othervalue');
end
>>we
请输入x的值:
-1
negativeone
>>we
请输入x的值:
0
zeroc
>>we
请输入x的值:
1
positiveone
>>we
请输入x的值:
6
othervalue
例3.9已知y=1+2+…+n,利用for语句求解当y=1000时,y的值
解:
y=0;N=1000
forn=1:
N;
y=y+n;
end
y
>>ci
N=
1000
y=
500500
例3.10.for循环示例
A=rand(4,5);
fori=A
sum=mean(i)
end
>>li
sum=
0.5685
sum=
0.5321
sum=
0.6684
sum=
0.5605
sum=
0.7891
例3.12.While循环
y=0;n=1;
whilen<=100
y=y+1/n/n;
n=n+1;
end
y
>>me
y=
1.6350
课后习题
4.x=input('请输入x的值;');
if(x<0&x~=-3)==1
y=x*x+x-6;
elseif(x>=0&x<10&x~=2&x~=3)==1
y=x*x-5*x+6;
else
y=x*x-x-1
end
y
>>my
请输入x的值;-5.0
y=
14
>>my
请输入x的值;-3.0
y=
11
y=
11
>>my
请输入x的值;0
y=
6
5.P=input('输入学生的成绩P的值:
')
switchfloor(P/10)
case{0,1,2,3,4,5}
disp('E');
case{6}
disp('D');
case{7}
disp('C');
case{8}
disp('B');
case{9,10}
disp('A');
end
>>yang
输入学生的成绩P的值:
98
P=
98
A
>>yang
输入学生的成绩P的值:
85
P=
85
B
>>yang
输入学生的成绩P的值:
76
P=
76
C
>>yang
输入学生的成绩P的值:
68
P=
68
D
>>yang
输入学生的成绩P的值:
55
P=
55
7.y=0,N=100;;
forn=1:
N
y=y+1/n/n;
end
pi=y*6
y=0;N=1000;
forn=1:
N;
y=y+1/(n*n);
end
pi=y*6
y=0;N=10000;
forn=1:
N;
y=y+1/(n*n);
end
pi=y*6
Untitled1
pi=
9.8099
>>Untitled2
pi=
9.8636
>>Untitled3
pi=
9.8690
第四章图形基础
例4.253种图形着色方式的效果展示
z=peaks(20);colormap(copper);
subplot(1,3,1);surf(z);
subplot(1,3,2);surf(z);shadingflat;
subplot(1,3,3);surf(z);shadinginterp;
例4.13绘制rho=sin(3theta)cos(3theta)的极坐标图
theta=0:
0.01:
2*pi;
rho=sin(3*theta).*cos(3*theta);
polar(theta,rho,'r');
例4.8用图形保持功能在同一坐标内绘制曲线y=2*exp(-0.5*x.).*sin(2*pi*x)
x=(0:
pi/100:
2*pi)';
y1=2*exp(-0.5*x)*[1,-1];y2=2*exp(-0.5*x).*sin(2*pi*x);
plot(x,y1,'b:
');
axis([0,2*pi,-2,2]);%设置坐标
holdon;%设置图形保持状态
plot(x,y2,'k');
gridon;%加网格线
boxoff;%不加坐标边框
holdoff;%关闭图形保持
程序运行如下:
>>mei
例4.10在一个图形窗口中以子图形式同时绘制正弦、余弦、正切、余切曲线。
x=linspace(0,2*pi,60);
y=sin(x);z=cos(x);
t=sin(x)./(cos(x)+eps);ct=cos(x)./(sin(x)+eps);
subplot(2,2,1);
plot(x,y);title('sin(x)');axis([0,2*pi,-1,1]);
subplot(2,2,2);
plot(x,z);title('cos(x)');axis([0,2*pi,-1,1]);
subplot(2,2,3);
plot(x,t);title('tangent(x)');axis([0,2*pi,-40,40]);
subplot(2,2,4);
plot(x,ct);title('cotangent(x)');axis([0,2*pi,-4040
例4.21绘制标准三维曲面图形。
t=0:
pi/20:
2*pi;
[x,y,z]=cylinder(2+sin(t),30);
subplot(1,3,1);
surf(x,y,z);
subplot(1,3,2);
[x,y,z]=sphere;
surf(x,y,z);
subplot(1,3,3);
[x,y,z]=peaks(30);
meshz(x,y,z);
课后习题:
2绘制二维图形
(1)y=x^3+x+1
x=-10:
1:
10;
y=x.*x.*x+x+1;
plot(y)
(4)x=2t^2;y=6t^3
t=-5:
1:
50;
x=2*t.*t;y=6*t.*t.*t;
plot(x,y);
2.绘制下列二维图形:
x*x+y*y=4
t=0:
pi/100:
2*pi;
x=2*cos(t);y=2*sin(t);
plot(x,y,'b:
');
gridon;
holdon;
3.绘制极坐标rho=3*cos(theta)+sin(theta).
theta=0:
0.01:
2*pi;
rho=3*cos(theta)+sin(theta);
polar(theta,rho,'r');
4.绘制下列三维图形
(1)t=0:
pi/100:
2*pi;
x=2*cos(t);
y=sin(t);
z=t;
[x,y,z]=sphere(20);
surf(x,y,z);
title('surfl(x,y,z)');
(4)theta=0:
pi/100:
2*pi;
x=8*cos(theta);
y=8*sin(theta);
z=8;
[x,y,z]=sphere(20);
surf(x,y,z);
6已知y1=x^2,y2=cos(2x);y3=y1*y2
(1)在同一坐标系中用不同的线型绘制3条曲线
x=0:
0.01:
2*pi;
y1=x.*x;y2=cos(2*x);y3=(y1).*(y2);
plot(x,y1,'--',x,y2,'o',x,y3,'s');
第五章MATLAB数值计算
例5.5求多项式x^4+3*x^3+x*2+2*x+1对于对于矩阵[36;84]及标量7的值。
>>p=[13121];
>>polyvalm(p,[36;84])
ans=
71866666
88888297
>>polyvalm(p,7)
ans=
3494
例5.13用一个3次多项式在区间[0,2π]内逼近函数sin(x)。
X=linspace(0,2*pi,50);Y=sin(X);
[p,s]=polyfit(X,Y,3)
plot(X,Y,'r:
*',X,polyval(p,X),'-o')
结果以及图形:
p=
0.0912-0.85961.8527-0.1649
s=
R:
[4x4double]
df:
46
normr:
0.5055
例5.14a和b均为方阵,求方阵系统的根。
命令窗口键入:
a=ceil(rand(4,4)*12)
a=
12111012
31069
8683
61105
b=ceil(rand(4,4)*10)
b=
10123
10432
5921
9178
x=a\b
x=
-0.6400-0.3800-0.5800-0.3600
0.08141.4697-0.2959-0.7021
0.98831.06790.97480.7876
0.5752-1.77380.20550.5972
例5.17y=sin(x),x从0~2π,△x=0.02π,求y的最大值、最小值、均值和标准差。
>>x=0:
0.02*pi:
2*pi;
>>y=sin(x);
>>ymax=max(y)
ymax=
1
>>ymin=min(y)
ymin=
-1
>>ymean=mean(y)
ymean=
2.2995e-017
>>ystd=std(y)
ystd=
0.7071
例5.21计算z=e^x²的梯度。
命令窗口键入:
x=-1:
0.2:
1;
z=exp(x.^2);
f=gradient(z)
fx=
-0.8218-0.6425-0.3615-0.1963-0.0868
00.08680.19630.36150.6425
0.8218
课后习题
2.用roots命令求多项式x^5-2x^4+5x^3-6x^2+7x-20的根
a=[1,-2,5,-6,7,-20];
r=roots(a)
r=
1.7944
0.8167+1.9502i
0.8167-1.9502i
-0.7139+1.4084i
-0.7139-1.4084i
4计算多项式除法(x^4+2x^3+10x^2+7x+9)/(x+9)
>>p1=[1,2,10,7,9];
>>p2=[1,9];
>>[p]=deconv(p1,p2)
p=
1-773-650
双叶抛物面
clear;close;
u=-4:
0.1:
4;v=-4:
0.1:
4;
[u,v]=meshgrid(u,v);
X=u;Y=v;
Z=(u.^2-v.^2)/3;
mesh(X,Y,Z);
shadinginterp;
colormap(jet);