实验三 MATLAB数值运算.docx
《实验三 MATLAB数值运算.docx》由会员分享,可在线阅读,更多相关《实验三 MATLAB数值运算.docx(11页珍藏版)》请在冰豆网上搜索。
![实验三 MATLAB数值运算.docx](https://file1.bdocx.com/fileroot1/2022-12/31/723d9364-bad0-46eb-817b-1f166cab4593/723d9364-bad0-46eb-817b-1f166cab45931.gif)
实验三MATLAB数值运算
实验三MATLAB数值运算
班级:
机工1004班学号:
10405701308姓名:
胡健民
1.实验目的
掌握MATLAB的数值运算及其运算中所用到的函数,掌握结构数组和细胞数组的操作。
2.实验内容
(1)多项式运算。
(2)多项式插值和拟合。
(3)数值微积分。
(4)结构数组和细胞数组。
3.实验步骤
1.多项式运算
1)多项式表示。
在MATLAB中,多项式表示成向量的形式。
如:
S^4+3S^3-5S^2+9在MATLAB中表示为
>>S=[13-509]
2)多项式的加减法相当于向量的加减法,但须注意阶次要相同。
如不同,低阶的要补0.如多项式2S^2+3S+9与多项式S^4+3S^3-5S^2+4S+7相加。
>>S=[00239]
S=
00239
>>S2=[13-547]
S2=
13-547
>>S3=S1+S2
S3=
13-3718
3)多项式的乘,除法分别用函数conv和deconv实现
>>S1=[237]
S1=
237
>>S2=[13-547]
S2=
13-547
>>S3=conv(S1,S2)
S3=
29614-94949
>>S4=deconv(S3,S1)
S4=
13-547
4)多项式求根用函数roots
>>S1=[242]
S1=
242
>>roots(S1)
ans=
-1
-1
5)多项式求值用函数polyval
>>S1=[241-3]
S1=
241-3
>>polyval(S1,3)
ans=
90
>>X=1:
10
X=
12345678910
>>Y=polyval(S1,X)
Y=
Columns1through7
43190193352579886
Columns8through10
128517882407
2.多项式插值和拟合
X
1
2
3
4
5
6
7
8
9
10
Y
16
32
70
142
260
436
682
1010
1432
1960
请分别用拟合(二阶至三阶)和插值(线性和三次样条)的方法估测X=9.5时Y的值。
二阶拟合:
>>X=1:
10
X=
12345678910
>>Y=[163270142260436682101014321960]
Y=
Columns1through7
163270142260436682
Columns8through10
101014321960
>>P2=polyfit(X,Y,2)
P2=
32.0000-147.2000181.6000
>>Y2=polyval(P2,9.5)
Y2=
1.6712e+003
3.数值微分积
(1)差分使用diff函数实现。
>>x=1:
2:
9
x=
13579
>>diff(x)
ans=
2222
(2)可以用因变量和自变量差分的结果相除得到数值微分。
>>x=linspace(0,2*pi,100)
x=
Columns1through9
00.06350.12690.19040.25390.31730.38080.44430.5077
Columns10through18
0.57120.63470.69810.76160.82510.88850.95201.01551.0789
Columns19through27
1.14241.20591.26931.33281.39631.45971.52321.58671.6501
Columns28through36
1.71361.77711.84051.90401.96752.03092.09442.15792.2213
Columns37through45
2.28482.34832.41172.47522.53872.60212.66562.72912.7925
Columns46through54
2.85602.91952.98293.04643.10993.17333.23683.30033.3637
Columns55through63
3.42723.49073.55413.61763.68113.74453.80803.87153.9349
Columns64through72
3.99844.06194.12534.18884.25234.31574.37924.44274.5061
Columns73through81
4.56964.63314.69654.76004.82354.88694.95045.01395.0773
Columns82through90
5.14085.20435.26775.33125.39475.45815.52165.58515.6485
Columns91through99
5.71205.77555.83895.90245.96596.02936.09286.15636.2197
Column100
6.2832
>>y=sin(x)
y=
Columns1through9
00.06340.12660.18930.25110.31200.37170.42980.4862
Columns10through18
0.54060.59290.64280.69010.73460.77610.81460.84970.8815
Columns19through27
0.90960.93410.95490.97180.98480.99380.99890.99990.9969
Columns28through36
0.98980.97880.96380.94500.92240.89600.86600.83260.7958
Columns37through45
0.75570.71270.66680.61820.56710.51370.45820.40090.3420
Columns46through54
0.28170.22030.15800.09510.0317-0.0317-0.0951-0.1580-0.2203
Columns55through63
-0.2817-0.3420-0.4009-0.4582-0.5137-0.5671-0.6182-0.6668-0.7127
Columns64through72
-0.7557-0.7958-0.8326-0.8660-0.8960-0.9224-0.9450-0.9638-0.9788
Columns73through81
-0.9898-0.9969-0.9999-0.9989-0.9938-0.9848-0.9718-0.9549-0.9341
Columns82through90
-0.9096-0.8815-0.8497-0.8146-0.7761-0.7346-0.6901-0.6428-0.5929
Columns91through99
-0.5406-0.4862-0.4298-0.3717-0.3120-0.2511-0.1893-0.1266-0.0634
Column100
-0.0000
>>plot(x,y)
>>y1=diff(y)./diff(x);
>>plot(x(1:
end-1),y1)
(3)Cumsum函数求累计积分,trapz函数用梯形法求定积分,即曲线的面积。
>>x=ones(1,10)
x=
1111111111
>>cumsum(x)
ans=
12345678910
>>x=linspace(0,pi,100)
x=
Columns1through9
00.03170.06350.09520.12690.15870.19040.22210.2539
Columns10through18
0.28560.31730.34910.38080.41250.44430.47600.50770.5395
Columns19through27
0.57120.60290.63470.66640.69810.72990.76160.79330.8251
Columns28through36
0.85680.88850.92030.95200.98371.01551.04721.07891.1107
Columns37through45
1.14241.17411.20591.23761.26931.30111.33281.36451.3963
Columns46through54
1.42801.45971.49151.52321.55491.58671.61841.65011.6819
Columns55through63
1.71361.74531.77711.80881.84051.87231.90401.93571.9675
Columns64through72
1.99922.03092.06272.09442.12612.15792.18962.22132.2531
Columns73through81
2.28482.31652.34832.38002.41172.44352.47522.50692.5387
Columns82through90
2.57042.60212.63392.66562.69732.72912.76082.79252.8243
Columns91through99
2.85602.88772.91952.95122.98293.01473.04643.07813.1099
Column100
3.1416
>>y=sin(x)
y=
Columns1through9
00.03170.06340.09510.12660.15800.18930.22030.2511
Columns10through18
0.28170.31200.34200.37170.40090.42980.45820.48620.5137
Columns19through27
0.54060.56710.59290.61820.64280.66680.69010.71270.7346
Columns28through36
0.75570.77610.79580.81460.83260.84970.86600.88150.8960
Columns37through45
0.90960.92240.93410.94500.95490.96380.97180.97880.9848
Columns46through54
0.98980.99380.99690.99890.99990.99990.99890.99690.9938
Columns55through63
0.98980.98480.97880.97180.96380.95490.94500.93410.9224
Columns64through72
0.90960.89600.88150.86600.84970.83260.81460.79580.7761
Columns73through81
0.75570.73460.71270.69010.66680.64280.61820.59290.5671
Columns82through90
0.54060.51370.48620.45820.42980.40090.37170.34200.3120
Columns91through99
0.28170.25110.22030.18930.15800.12660.09510.06340.0317
Column100
0.0000
>>s=trapz(x,y)
s=
1.9998
4.结构数组与细胞数组
(1)结构数组的创建。
>>student.number='20050731001';
>>student.name='Jack';
>>student
(2).number='20050731002';
>>student
(2).name='Lucy';
(2)结构数组的操作。
>>student
(1).subject=[]
student=
1x2structarraywithfields:
number
name
subject
sorce
>>student
(1).sorce=[]
student=
1x2structarraywithfields:
number
name
subject
sorce
>>student
student=
1x2structarraywithfields:
number
name
subject
sorce
>>fieldnames(student)
ans=
'number'
'name'
'subject'
'sorce'
>>getfield(student,{2},'name')
ans=
Lucy
>>student=rmfield(student,'subject')
student=
1x2structarraywithfields:
number
name
sorce
>>student=setfield(student,{1},'sorce',90);
>>student
(2).sorce=88;
(3)细胞数组的创建。
>>A={'Howareyou!
',ones(3);[12;34],{'cell'}};
>>B(1,1)={'Helloworld'};
>>B(1,2)={magic(3)};
>>B(2,1)={[1234]};
(4)细胞数组的操作。
>>A={'Howareyou!
',ones(3);[12;34],{'cell'}};
>>B(1,1)={'Helloworld'};
>>B(1,2)={magic(3)};
>>B(2,1)={[1234]};
>>ans1=A(1,1)
ans1=
'Howareyou!
'
>>ans2=A(1,1)
ans2=
'Howareyou!
'
>>whosans1ans2
NameSizeBytesClass
ans11x184cellarray
ans21x184cellarray
Grandtotalis26elementsusing168bytes
>>celldisp(A)
A{1,1}=
Howareyou!
A{2,1}=
12
34
A{1,2}=
111
111
111
A{2,2}{1}=
cell
>>a1=A{2,1}(1,2)
a1=
2
>>[a2a3]=deal(A{1:
2})
a2=
Howareyou!
a3=
12
34