实验九 Matlab函数库及应用资料

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验九 Matlab 函数库及应用
9.1 一物理系统可用下列方程组来表示
⎢⎢⎢⎢⎣⎡00sin cos 11θθm m 0021
m m - θθθθcos sin cos sin --- ⎥⎥⎥⎥⎦⎤1000⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡2121N N a a =⎥⎥⎥⎥⎦
⎤⎢
⎢⎢⎢⎣⎡g m g m 2100 从键盘输入1m 、2m 和θ的值,求1α、2a 、1N 和2N 的值。

其中g 取9.8。

clear,clc g=9.8;
m1=input('请输入m1: ','s'); m2=input('请输入m2: ','s'); c=input('请输入角度: ','s');
m1=str2num(m1);m2=str2num(m2);c=str2num(c); c=c/180*pi; a=[0; m1*g;0;m2*g];
b=[m1*cos(c) -m1 -sin(c) 0;m1*sin(c) 0 cos(c) 0;0 m2 -sin(c) 0;0 0 -cos(c) 1]; d=inv(b)*a;
disp(['a1=',num2str(d(1))]); disp(['a2=',num2str(d(2))]); disp(['N1=',num2str(d(3))]); disp(['N2=',num2str(d(4))]);
运行结果: 请输入m1: 5 请输入m2: 5 请输入角度: 60 a1=9.6995 a2=2.4249 N1=14 N2=56 9.2 已知
⎪⎪⎩⎪⎪⎨
⎧+-====---3
2132
12101n n n n f f f f f f f 3>n 求1001~f f 中:
(1)最大值、最小值、各数之和。

(提示:可以考虑使用MATLAB 有关函数来实现。


(2)正数、零、负数的个数。

(提示:if 语句) clear,clc
f(1)=1;f(2)=0;f(3)=1; for n=4:100
f(n)=f(n-1)-2*f(n-2)+f(n-3); end a=max(f);
disp(['f1~f100中的最大值为: ',num2str(a)]); b=min(f);
disp(['f1~f100中的最小值为: ',num2str(b)]);
c=sum(f);
disp(['f1~f100的之和为: ',num2str(c)]);
i=0;j=0;k=0;
for n=1:100
if(f(n)>0)
i=i+1;
end
if(f(n)==0)
j=j+1;
end
if(f(n)<0)
k=k+1;
end
end
disp(['f1~f100中正数的个数:',num2str(i),'个']); disp(['f1~f100中零的个数:',num2str(j),'个']); disp(['f1~f100中负数的个数:',num2str(k),'个']); 运行结果:
f1~f100中的最大值为: 437763282635
f1~f100中的最小值为: -899412113528
f1~f100的之和为: -742745601951
f1~f100中正数的个数:49个
f1~f100中零的个数:2个
f1~f100中负数的个数:49个
9.3 假设有一组实测数据,分别绘制出1-4次及10-13次的拟合曲线。

clear,clc
x=0.1:0.1:1;
y=[2.3201 2.6470 2.9070 3.2885 3.6008 3.9090 4.2147
4.5191 4.8232
5.1275];
xi=linspace(0,1);
subplot(2,4,1);a1=polyfit(x,y,1);yi1=polyval(a1,xi);plot(x,y,'o',xi,yi1,'m');
subplot(2,4,2);a2=polyfit(x,y,2);yi2=polyval(a2,xi);plot(x,y,'o',xi,yi2,'r');
subplot(2,4,3);a3=polyfit(x,y,3);yi3=polyval(a3,xi);plot(x,y,'o',xi,yi3,'c');
subplot(2,4,4);a4=polyfit(x,y,4);yi4=polyval(a4,xi);plot(x,y,'o',xi,yi4,'b');
subplot(2,4,5);a10=polyfit(x,y,10);yi10=polyval(a10,xi);plot(x,y,'o',xi,yi10,'k');
subplot(2,4,6);a11=polyfit(x,y,11);yi11=polyval(a11,xi);plot(x,y,'o',xi,yi11,'g');
subplot(2,4,7);a12=polyfit(x,y,12);yi12=polyval(a12,xi);plot(x,y,'o',xi,yi12,'y');
subplot(2,4,8);a13=polyfit(x,y,13);yi13=polyval(a13,xi);plot(x,y,'o',xi,yi13,'m');
运行结果:
0.5
1
2
34
5
60
0.5
1
12
3
4
5
600.512
34
5
600.51
2
345
600.51
-5
5
1000.51-4
-2
024
600.51-2
02
4
600.51
-2
024
6
9.4 用字符串、单元阵列及结构阵列三种方式定义student1,student2及student3三个数组,此数组应包括Jone ,David 及Tom 三个人名。

比较三者的不同。

如果还要包括第二属性——他们的出生地birthplace ,分别为Shanghai ,Nanjing 和Hangzhou ,又有什么差别? 字符串定义: clear,clc format compact
student1='Jone';student2='David';student3='Tom'; student=char(student1,student2,student3); >> student student = Jone
David
Tom
单元阵列定义:
clear,clc
format compact
name={'Jone';'David';'Tom'};
student={name};
student{1}
ans =
'Jone'
'David'
'Tom'
>> student{1}{1}
ans =
Jone
结构阵列定义:
clear,clc
format compact
='Jone';='David';='T om';
>> student1
student1 =
name: 'Jone'
>> student2
student2 =
name: 'David'
>> student3
student3 =
name: 'Tom'
三者的不同:
字符串直接用串名索引;单元阵列用矩阵索引;结构阵列用域名访问数据。

字符串定义:
clear,clc
format compact
s1=['student1:','Jone',' ','birthplace:','Shanghai'];
s2=['student2:','David',' ','birthplace:','Nanjing'];
s3=['student3:','Tom',' ','birthplace:','Hangzhou']; student=char(s1,s2,s3);
>> student
student =
student1:Jone birthplace:Shanghai
student2:David birthplace:Nanjing
student3:Tom birthplace:Hangzhou
单元阵列定义:
clear,clc
format compact
name={'Jone';'David';'Tom'};
birthplace={'Shanghai','Nanjing','Hangzhou'};
student={name,birthplace};
>> student{1}{2}
ans =
David
>> student{2}{2}
ans =
Nanjing
结构阵列定义:
clear,clc
format compact
student(1)=struct('name','Jone','birthplace','Shanghai'); student(2)=struct('name','David','birthplace','Nanjing'); student(3)=struct('name','Tom','birthplace','Hangzhou'); for i=1:3
disp(['student(',num2str(i),')=']);
disp(student(i));
end
student(1)=
name: 'Jone'
birthplace: 'Shanghai'
student(2)=
name: 'David'
birthplace: 'Nanjing'
student(3)=
name: 'Tom'
birthplace: 'Hangzhou'
>> student
student =
1x3 struct array with fields:
name
birthplace
三者的差别:
字符串还是用串名索引,显示所有信息,定义时加入信息量就可以了。

单元阵列索引时要想准确找出,需分别对应。

结构阵列则可以用struct定义,索引比较方便。

相关文档
最新文档