实验6 Matlab数值计算

更新时间:2023-07-19 01:44:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验6 Matlab数值计算

实验6 Matlab数值计算 disp('随机矩阵为:'); 实验目的:

1、 掌握数据统计与分析的方法; 2、 掌握数据插值和曲线拟合的方法及其应用;

3、 掌握多项式的常用运算。 实验内容:

1.

利用randn函数生成符合正态分布的

10×5随机矩阵A,进行如下操作:

(1) 求A的最大元素和最小元素;

(2) 求A的每行元素的和以及全部元素的和;

(3) 分别对A的每列元素按升序、每行元素按降序排列。

a=randn(10,5); am=max(max(a)); ai=min(min(a)); ah=sum(a,2); az=sum(ah); al=sort(a); ahj=-sort(-a,2);

a

disp('最大值:'); am

disp('最小值:'); ai

disp('每行和:'); ah

disp('总和:'); az

disp('列升序:'); al

disp('行降序:');

ahj

随机矩阵为: a =

-0.4326 -0.1867 6041

-1.6656 0.7258 2573

0.1253 -0.5883 0565

0.2877 2.1832 151

-1.1465 -0.1364 8051

1.1909 0.1139 287

1.1892 1.0668 193

-0.0376 0.0593 0.2944 -1.3362 0.7143 1.6236 -0.6918 0.8580 1.2540 -1.5937 -0.3999 0.6900 0.8156 0.7119 1.2902 0.6686 1.1908 -1.2025 -1.0.-1.1.4-0.0.50.2-0.

实验6 Matlab数值计算

9219

0.3273 -0.0956 -1.4410 -0.0198 -2.1707

0.1746 -0.8323 0.5711 -0.1567 -0.0592 最大值: 6041

-0.4326 -0.1867 -1.3362 -0.1567 -1.0565

-0.0376 -0.1364 -0.6918 -0.0198 -0.9219

0.1253 -0.0956 0.2944 0.6686 -0.am = 2.1832 最小值: ai = -2.1707 每行和: ah = -2.3288 -1.3287 0.0105 6.2215 -1.4895 3.3602 4.9201 -3.6964 -3.3998 -0.3025 总和: az = 1.9666 列升序: al =

-1.6656 1707

-1.1465 -0.8323 -0.5883 -1.5937 -1.4410 -1.2025 -0.3999 8051

0.1746 592

0.2877 193

0.3273 573

1.1892 287

1.1909 151 行降序: ahj =

0.2944 6041

0.7258 6656

0.8156 0565

2.1832 877

1.2902 -2.1465

1.1909 -1.

139

0.0593 0.1139 0.7258 1.0668 2.1832 -0.1867 0.6900 0.7143 1.6236 -0.1364 0.8580 0.5711 0.7143 0.8580 1.2540 1.6236 -0.3999 0.2573 0.1253 1.4151 -0.6918 0.6686 0.6900 0.7119 0.8156 1.1908 1.2902 -0.4326 -1.3362 -0.5883 0.7119 -0.8051 0.5287 -0.00.20.20.51.4-1.-1.-1.0.2-1.0.1

实验6 Matlab数值计算

1.2540 1.1908 1.1892 1.0668 0.2193

0.0593 -0.0376 -0.9219 -1.2025 -1.5937

0.3273 -0.0198 -0.0956 -1.4410 -2. Columns 7 through 12

2.6494 2.8292 3.0000 3.1636 3.3186 3.4661

Columns 13 through 18

3.6069 3.7422 3.8729 4.0000 4.11707

0.5711 0.1746 -0.0592 -0.1567 -0.8323 >>

2.

用3次多项式方法插值计算1-100之间

整数的平方根。

x=[1,4,9,16,25,36,49,64,81,100]; y=x.^(1/2); x1=[1:100];

y1=interp1(x,y,x1,'cubic'); disp('1到100的根为:'); y1

>> 1到100的根为: y1 =

Columns 1 through 6

1.0000 1.3729 1.7125 2.0000 2.2405 2.4551

237 4.2435

Columns 19 through 24

4.3599 4.4730 4.5832 958 4.8988

Columns 25 through 30

5.0000 5.0993 5.1966 857 5.4777

Columns 31 through 36

5.5681 5.6570 5.7446 160 6.0000

Columns 37 through 42

6.0829 6.1647 6.2454 035 6.4810

Columns 43 through 48

6.5577 6.6334 6.7082 556 6.9281

Columns 49 through 54

7.0000 7.0712 7.1416 804 7.3487

Columns 55 through 60

7.4164 7.4835 7.5500 812 7.7459

Columns 61 through 66

7.8102 7.8739 7.9372 623 8.1242

Columns 67 through 72

4.6907 4.75.2921 5.3

5.8309 5.96.3249 6.46.7823 6.87.2113 7.27.6159 7.68.0000 8.0

实验6 Matlab数值计算

8.1855 8.2464 8.3068 8.3668 8.4263 8.4854

Columns 73 through 78

8.5441 8.6024 8.6603 8.7178 8.7749 8.8317

Columns 79 through 84

8.8881 8.9442 9.0000 9.0555 9.1107 9.1655

Columns 85 through 90

9.2201 9.2744 9.3284 9.3821 9.4354 9.4884

Columns 91 through 96

9.5412 9.5935 9.6456 9.6973 9.7486 9.7996

Columns 97 through 100

9.8502 9.9005 9.9505 10.0000

3.

使用三次样条插值分别求出该日室内外6:30-17:30之间每隔2h各点的近似温度,并绘制插值后的温度曲线。 x=[6:2:18]

yn=[18,20,22,25,30,28,24]; yw=[15,19,24,28,34,32,30]; x1=[6.5:2:17.5];

y1n=interp1(x,yn,x1,'spline'); y1w=interp1(x,yw,x1,'spline'); y1n y1w

某气象观测站测得某日6:00-18:00之间每隔2h的室内外温度(°C)如下表所示。

xx=[x,x1]; yyn=[yn,y1n]; yyw=[yw,y1w]; plot(xx,yyn); plot(xx,yyw); y1n =

18.5020 20.4986 22.5193 26.3775 30.2051 26.8178 y1w =

实验6 Matlab数值计算

15.6553 20.3355 24.9089 29.6383 34.2568

30.9594

x=linspace(1,101,11); lgx=log(x)./log(10); px=polyfit(x,lgx,5); y1=polyval(px,x); plot(x,lgx,':o',x,y1,'-*'); legend('lgx','fit'); px

4.

已知lgx在[1,101]区间10个整数采样

px =

Columns 1 through 5

0.0000 -0.0000 0.0001 -0.0058 0.1537 Column 6 -0.1326

点的函数值如下表所示,

5.

有3个多项式

4

3

2

P1(x) x 2x 4x 5,P2(x) x 2,P3(x)

试求lgx的5次拟合多项式p(x),并绘制lgx和p(x)在[1,101]区间的函数曲线。

,试进行下列操作:求

P(x) P1(x) P2(x)P3(x)。求P(x)的

实验6 Matlab数值计算

根。 6.

当x取矩阵A的每一元素时,求P(x)

的值。其中: 1

1.2 1.4

A

0.75

23.5

0

5

2.5

p1=[1,2,4,0,5]; p2=[1,2]; p3=[1,2,3]; p23=conv(p2,p3); disp('p(x)为:'); px=poly_sum(p1,p23) disp('p(x)的根为:'); r=roots(px)

x=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5]; disp('p(x)的值为:'); p=polyval(px,x) %%%%%

function p_out=poly_sum(p1, p2) % calculate the sum of two polynomials if length(p1)==length(p2) p_out=p1+p2;

elseif length(p1)>length(p2)

p2=[zeros(1,length(p1)-length(p2)), p2]; p_out=p1+p2; else

p1=[zeros(1,length(p2)-length(p1)), p1];

p_out=p1+p2; end

%%%%%%%%%%% >> p(x)为: px =

1 3 8 7 11 p(x)的根为: r =

-1.3840 + 1.8317i -1.3840 - 1.8317i -0.1160 + 1.4400i -0.1160 - 1.4400i p(x)的值为: p =

1.0e+003 *

0.0100 0.0382 0.0125 0.0223 0.0970 0.4122 0.0110 1.2460 0.1644

7. 求函数在指定点的数值导数。

(1)

f(x) sin2x cos2

x,x

,

64,3,

2f=inli

ne('(sin(x)).^2+(cos(x)).^2'); dx1=diff(f([pi/6,0.001+pi/6]))/0.001; dx2=diff(f([pi/4,0.001+pi/4]))/0.001; dx3=diff(f([pi/3,0.001+pi/3]))/0.001;

实验6 Matlab数值计算

dx4=diff(f([pi/2,0.001+pi/2]))/0.001; dx1 dx2 dx3 dx4 >> dx1 = 0 dx2 = 0 dx3 = 0 dx4 = 0 (2

)f(x) x 1,2,3

(2)

f=inline('(x.^2+1).^(0.5)'); dx1=diff(f([1,0.001+1]))/0.001; dx2=diff(f([2,0.001+2]))/0.001; dx3=diff(f([3,0.001+3]))/0.001; dx1 dx2 dx3 >> dx1 = 0.7073 dx2 = 0.8945

dx3 = 0.9487

8. 用数值方法求定积分。

(1

)I1

2 0

的近似

值。

fun=inline('(cos(t.^2)+4.*sin((2.*t).^2)+1).^(1/2)','t');

isim=quadl(fun,0,2*pi)

isim = 6.7992 + 3.1526i(2)

I11 x)2

ln(0

1 x

2

fun=inline('(log(1+x))./(1+x.^2)','x'); isim=quadl(fun,0,1) isim = 0.2722

本文来源:https://www.bwwdw.com/article/b9g1.html

Top