基于matlab的科学计算实验.docx

上传人:b****3 文档编号:27054547 上传时间:2023-06-26 格式:DOCX 页数:28 大小:24.83KB
下载 相关 举报
基于matlab的科学计算实验.docx_第1页
第1页 / 共28页
基于matlab的科学计算实验.docx_第2页
第2页 / 共28页
基于matlab的科学计算实验.docx_第3页
第3页 / 共28页
基于matlab的科学计算实验.docx_第4页
第4页 / 共28页
基于matlab的科学计算实验.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

基于matlab的科学计算实验.docx

《基于matlab的科学计算实验.docx》由会员分享,可在线阅读,更多相关《基于matlab的科学计算实验.docx(28页珍藏版)》请在冰豆网上搜索。

基于matlab的科学计算实验.docx

基于matlab的科学计算实验

科学计算与数据处理实验报告

学  号

S3*******9

姓  名

张园

实验名称

基于MATLAB的科学计算实验

实验目的

1、掌握MATLAB中数组的创建和操作方法

2、掌握MATLAB中常用的数值计算方法

3、掌握MATLAB中常用的符号计算方法

实验方案

一、一维数组创建实验:

(1)直接输入法:

>>test=[1234]

>>test=[1;2;3;4]

(2)步长生成法:

>>test=1:

0.5:

10

(3)定数线性采样法:

>>test=linspace(1,12,5)

(4)定数对数采样法:

>>logspace(2,6,4)

二、高维数组创建实验:

(1)直接输入法:

>>A=[123;456;789]

(2)使用下标:

>>clear,A(2,3,2)=1

(3)使用低维数组:

>>clear,A=eye(3,4);A(:

:

2)=eye(3,4)*2;A(:

:

3)=eye(3,4)*3;A(:

:

4)=eye(3,4)*4

(4)使用创建函数(cat、repmat、reshape)创建高维数组:

>>cat(3,[1,2,3;4,5,6],eye(2,3)*2,ones(2,3))

>>repmat([1,2;3,4],[1,2,3])

>>reshape(1:

20,2,5,2)

三、标准数组创建实验:

(1)全0矩阵:

>>zeros(3)

(2)全1矩阵:

>>ones(5)

(3)单位矩阵:

>>eye(4)

(4)magic矩阵:

>>magic(4)

(5)随机矩阵:

>>randn(4)

四、矩阵变换实验:

令Data=[1,2,3,4;5,6,7,8;9,10,11,12],分别使用diag、'、fliplr、flipud、rot90、tril、triu函数计算Data的对角、转置、翻转、旋转、三角矩阵,具体命令如下:

>>Data=[1,2,3,4;5,6,7,8;9,10,11,12]

>>diag(Data)

>>(Data)'

>>fliplr(Data)

>>flipud(Data)

>>rot90(Data)

>>tril(Data)

>>triu(Data)

五、字符串数组创建与操作实验:

(1)创建字符串数组:

>>arr=str2mat('I','am','a','student')

(2)去掉字符串末尾的空格deblank:

建立字符串,用abs函数验证空格的存在;用deblank去掉空格,用abs已经去掉空格

>>x='an';y=abs(x)

>>z=deblank(x);w=abs(z)

(3)删除字符串开头和结尾的空格strtrim

>>str1='Iamastudent';

>>str2='Iamastudent';

>>x=strtrim(str1)

>>y=strtrim(str2)

(4)执行简单的字符串替代strrep、

>>str1='Iamastudent.';

>>str2='student';

>>str3='teacher';

>>str=strrep(str1,str2,str3)

(5)规范格式strread;

>>strread('0.231','%5.3f')

(6)函数strtok找出由特定字符指定的字符串内的标记;

>>ar='Iamastudent'

>>strtok(ar,'s')

六、架构数组的创建与操作实验:

(1)直接创建法:

>>clearx;x.real=[12345];x.imag=ones(4)

(2)命令(struct)创建法

>>s=struct('name',{'x','y'},'id',{'3','4'},'w',{3,4})

(3)Fieldnames函数:

>>fieldnames(s)

(4)Getfield函数:

>>str(1,1).name='x';

>>str(1,1).ID=5;

>>str(2,1).name='y';

>>str(2,1).ID=3;

>>result=getfield(str,{2,1},'name')

(5)Setfield函数:

>>str(1,1).name='x';

>>str(1,1).ID=5;

>>str(2,1).name='y';

>>str(2,1).ID=3;

>>str=setfield(str,{2,1},'name','a');

>>str(2,1).name

七、基本运算符号实验:

(1)矩阵加:

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a+b

(2)矩阵减:

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a-b

(3)矩阵乘

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a*b

(4)数组乘

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a.*b

(5)矩阵乘方

>>a=[1,2,3;4,5,6;7,8,9];

>>a^2

(6)数组乘方

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a.^b

(7)矩阵左除

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[2;4;6];a\b

(8)矩阵右除

>>a=ones(3);

>>b=[1,1,1];

>>a/b

(9)数组左除

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a.\b

(10)数组右除

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a./b

(11)克罗内克张量积

>>a=[1,0,1;1,1,1;1,0,1];

>>b=[0,0,1;1,0,1;0,0,1];kron(a,b)

(12)逻辑与

>>a=[1,0,1;1,1,1;1,0,1];

>>b=[0,0,1;1,0,1;0,0,1];a&b

(13)逻辑或

>>a=[1,0,1;1,1,1;1,0,1];

>>b=[0,0,1;1,0,1;0,0,1];a|b

(14)逻辑非

>>a=[1,0,1;1,1,1;1,0,1];

>>~a

(15)逻辑异或

>>a=[1,0,1;1,1,1;1,0,1];

>>b=[0,0,1;1,0,1;0,0,1];xor(a,b)

八、矩阵分析实验:

(1)范数(norm):

>>a=[1,2,3;4,5,6;7,8,9];

>>norm(a,1)

>>norm(a,2)

(2)条件数(cond):

>>cond(a)

(3)行列式(det):

>>det(a)

(4)秩(rank):

>>rank(a)

(5)特征值(eig):

>>eig(a)

>>[V,D]=eig(a)

(6)化零矩阵(null)

>>Z=null(a)

(7)Cholesky分解(chol)

>>a=pascal(3)

>>a=pascal(3);chol(a)

(8)LU分解(lu)

>>a=[1,2,3;4,5,6;7,8,9];

>>[L1,U1]=lu(a)

(9)正交分解(qr)

>>a=[1,2,3;4,5,6;7,8,9];

>>[U,S]=qr(a)

(10)奇异值分解(svd):

>>a=[1,2,3;4,5,6;7,8,9];

>>[U,S,V]=svd(a)

九、数值计算实验:

(1)导数(diff):

>>a='x^3+4*x^2-x+20'

>>diff(a)

(2)梯度(gradient)

>>a=[1,2,3;4,5,6;7,8,9];

>>[fx,fy]=gradient(a)

(3)多项式求根(roots)、

>>p=[1,3,2,5];px=poly2str(p,'x');r=roots(p)

//p是多项式的MATLAB描述方法,我们可用poly2str(p,'x')函数,来显示多项式的形式,px=x^3+3x^2+2x+5

(4)零点(fzero、fsolve):

>>a=@(x)x^2+3*x+2;

>>x=fzero(a,0)

>>x=fsolve('x^2+3*x+2',0)

(5)极值(fminbnd、fminsearch、fminunc)、

1;>>f=@(x)x^2-4*x+5;>>fminbnd(f,0,1)

2;>>fun=inline('x

(1)^2-3*x

(1)*x

(2)+2*x

(2)^2');x0=[1,1];

>>fminsearch(fun,x0)

3;>>fun=inline('x

(1)^2-3*x

(1)*x

(2)+2*x

(2)^2');x0=[1,1];

>>fminunc(fun,x0)

(6)积分(quadl)

用内联函数定义被积函数:

>>fun=inline('-x.*x','x');

>>y=quadl(fun,0,1)

一十、符号计算实验:

(1)将

化简:

先用syms定义符号变量,再用simplify函数进行化简,具体命令如下:

>>simplify(cos(x)+sqrt(-sin(x)^2))

(2)求

的解:

用solve函数求解,命令如下:

>>x=solve('(x+2)^x=2','x')

实验记录

一、

(1)

>>test=[1234]

test=

1234

>>test=[1;2;3;4]

test=

1

2

3

4

(2)

>>test=1:

0.5:

10

test=

Columns1through4

1.00001.50002.00002.5000

Columns5through8

3.00003.50004.00004.5000

Columns9through12

5.00005.50006.00006.5000

Columns13through16

7.00007.50008.00008.5000

Columns17through19

9.00009.500010.0000

(3)

>>test=linspace(1,12,5)

test=

Columns1through4

1.00003.75006.50009.2500

Column5

12.0000

(4)

>>logspace(2,6,4)

ans=

1.0e+006*

0.00010.00220.04641.0000

二、

(1)直接输入法

>>A=[123;456;789]

A=

123

456

789

(2)使用下标

>>clear,A(2,3,2)=1

A(:

:

1)=

000

000

A(:

:

2)=

000

001

(3)使用低维数组

>>clear,A=eye(3,4);A(:

:

2)=eye(3,4)*2;A(:

:

3)=eye(3,4)*3;A(:

:

4)=eye(3,4)*4

A(:

:

1)=

1000

0100

0010

 

A(:

:

2)=

2000

0200

0020

 

A(:

:

3)=

3000

0300

0030

 

A(:

:

4)=

4000

0400

0040

(4)使用创建函数(cat、repmat、reshape)创建高维数组。

1,>>cat(3,[1,2,3;4,5,6],eye(2,3)*2,ones(2,3))

ans(:

:

1)=

123

456

 

ans(:

:

2)=

200

020

 

ans(:

:

3)=

111

111

2,>>repmat([1,2;3,4],[1,2,3])

ans(:

:

1)=

1212

3434

 

ans(:

:

2)=

1212

3434

 

ans(:

:

3)=

1212

3434

3,>>reshape(1:

20,2,5,2)

ans(:

:

1)=

13579

246810

 

ans(:

:

2)=

1113151719

1214161820

三、

(1)

>>zeros(3)

ans=

000

000

000

(2)

>>ones(5)

ans=

11111

11111

11111

11111

11111

(3)

>>eye(4)

ans=

1000

0100

0010

0001

(4)

>>magic(4)

ans=

162313

511108

97612

414151

(5)

>>randn(4)

ans=

1.06680.2944-0.6918-1.4410

0.0593-1.33620.85800.5711

-0.09560.71431.2540-0.3999

-0.83231.6236-1.59370.6900

四、

(1)

>>Data=[1,2,3,4;5,6,7,8;9,10,11,12]

Data=

1234

5678

9101112

>>diag(Data)

ans=

1

6

11

(2)

(Data)'

ans=

159

2610

3711

4812

(3)

>>fliplr(Data)

ans=

4321

8765

1211109

(4)

>>flipud(Data)

ans=

9101112

5678

1234

(5)

>>rot90(Data)

ans=

4812

3711

2610

159

(6)

>>tril(Data)

ans=

1000

5600

910110

(7)

>>triu(Data)

ans=

1234

0678

001112

五、

(1)

>>arr=str2mat('I','am','a','student')

arr=

I

am

a

student

(2)

>>x='an';y=abs(x)

y=

973211032

>>z=deblank(x);w=abs(z)

w=

9732110

(3)

>>str1='Iamastudent';

>>str2='Iamastudent';

>>x=strtrim(str1)

x=

Iamastudent

>>y=strtrim(str2)

y=

Iamastudent

(4)

>>str1='Iamastudent.';

>>str2='student';

>>str3='teacher';

>>str=strrep(str1,str2,str3)

str=

Iamateacher.

(5)

>>strread('0.231','%5.3f')

ans=

0.2310

(6)

>>ar='Iamastudent'

ar=

Iamastudent

>>strtok(ar,'s')

ans=

Iama

六;

(1)直接法

>>clearx;x.real=[12345];x.imag=ones(4)

x=

real:

[12345]

imag:

[4x4double]

(2)struct函数

>>s=struct('name',{'x','y'},'id',{'3','4'},'w',{3,4})

s=

1x2structarraywithfields:

name

id

w

(3)fieldnames功能演示

>>fieldnames(s)

ans=

'name'

'id'

'w'

(4)getfield功能演示

>>str(1,1).name='x';

>>str(1,1).ID=5;

>>str(2,1).name='y';

>>str(2,1).ID=3;

>>result=getfield(str,{2,1},'name')

result=

y

(5)setfield功能演示

>>str(1,1).name='x';

>>str(1,1).ID=5;

>>str(2,1).name='y';

>>str(2,1).ID=3;

>>str=setfield(str,{2,1},'name','a');

>>str(2,1).name

ans=

a

七:

(1)矩阵加

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a+b

ans=

4812

579

91215

(2)矩阵减

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a-b

ans=

-2-4-6

333

543

(3)矩阵乘

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a*b

ans=

112233

295887

4794141

(4)数组乘

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a.*b

ans=

31227

41018

143254

(5)矩阵乘方

>>a=[1,2,3;4,5,6;7,8,9];

>>a^2

ans=

303642

668196

102126150

(6)数组乘方

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a.^b

ans=

16419683

425216

494096531441

(7)矩阵左除

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[2;4;6];a\b

Warning:

Matrixisclosetosingularorbadlyscaled.

Resultsmaybeinaccurate.RCOND=2.203039e-018.

ans=

-0.4667

0.9335

0.1999

(8)矩阵右除

>>a=ones(3);

>>b=[1,1,1];

>>a/b

ans=

1

1

1

(9)数组左除

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a.\b

ans=

3.00003.00003.0000

0.25000.40000.5000

0.28570.50000.6667

(10)数组右除

>>a=[1,2,3;4,5,6;7,8,9];

>>b=[3,6,9;1,2,3;2,4,6];

>>a./b

ans=

0.33330.33330.3333

4.00002.50002.0000

3.50002.00001.5000

(11)克罗内克张量积

>>a=[1,0,1;1,1,1;1,0,1];

>>b=[0,0,1;1,0,1;0,0,1];kron(a,b)

ans=

001000001

101000101

001000001

001001001

101101101

001001001

001000001

101000101

001000001

(12)逻辑与

>>a=[1,0,1;1,1,1;1,0,1];

>>b=[0,0,1;1,0,1;0,0,1];a&b

ans=

001

101

001

(13)逻辑或

>>a=[1,0,1;1,1,1;1,0,1];

>>b=[0,0,1;1,0,1;0,0,1];a|b

ans=

101

111

101

(14)逻辑非

>>a=[1,0,1;1,1,1;1,0,1];

>>~a

ans=

010

000

010

(15)逻辑异或

>>a=[1,0,1;1,1,1;1,0,1];

>>b=[0,0,1;1,0,1;0,0,1];xor(a,b)

ans=

100

010

100

八、

(1)范数(norm)用来度量矩阵或者向量在某种意义下的长度

>>a=[1,2,3;4,5,6;7,8,9];

>>norm(a,1)

ans=

18

>>norm(a,2)

ans=

16.8481

(2)条件数(cond)可以描述矩阵为良性矩阵还是病态矩阵的一个参数

>>cond(a)

ans=

5.0524e+016

(3)行列式(det)

>>det(a)

ans=

0

(4)秩

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 水产渔业

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1