matlab译文第4章.ppt

合集下载

matlab编程基础与工程应用第四章PPT课件

matlab编程基础与工程应用第四章PPT课件

clear;clc;
%直接赋值法一:生成元胞数组
a(1,1)={[1 3 6;4 7 9;2 5 8]};
a(1,2)={'Hello'};
a(2,1)={(0:0.2:pi)};
a(2,2)={52};
a
2021/3/9
授课:XXX
4
%直接赋值法二:生成元胞数组
b={randn(2,3),'Carrot'; 1+2i,1:20}
在命令行窗口输入下列命令
>> A=cell(2)
%建立22空元胞数组
A=
[] []
[] []
>> B=cell(3,4)
%建立34空元胞数组
B=
[] [] [] []
[] [] [] []
[] [] [] []
>> C=cell(2,3,4)
%建立多维空元胞数组
C(:,:,1) =
[] [] []
[] [] []
2021/3/9
授课:XXX
10
“元胞标识”:以二维元胞数组a为例,a(2,1)是指中的第 2行第1列的元胞元素;
cell函数的语法为: c = cell(n);建立nn的空元胞数组。 c = cell(m, n)或c = cell([m, n]);建立mn的空元胞数组。 c = cell(size(A));建立与A数组同样尺寸的空元胞数组
2021/3/9
授课:XXX
8
【例4.1-3】用cell函数建立元胞数组
%直接赋值法三:生成元胞数组
c=[{randn(2,3)},{'Carrot'};{1+2i},{1:20}]

MATLAB讲稿——第四章.ppt

MATLAB讲稿——第四章.ppt

P [an an1 a1 a0 ]
二、 多项式行向量的生成方法 1、直接输入法
将多项式的各项系数依降幂次序排放在行 向量的元素位置上。
缺项系数输为0。
2、利用指令生成法 指令 P=poly(AR)
说明:(1)若AR是方阵,则多项式P就是该方阵的 特征多项式;
(2)若AR是行向量,即
AR [ar1 ar2
再例:
>> R=[-0.5 -0.3+0.4i -0.3-0.4i] %根向量
R=
-0.5000
-0.3000 + 0.4000i -0.3000 - 0.4000i
>> P=poly(R) % R的特征多项式
P=
1.0000 1.1000 0.5500 0.1250
>> PPR=poly2str(P,'x') %用习惯的方式显示多项式
例:
>> A=[1 4 7;3 11 6;5 32 68]; >> PA=poly(A) %A的特征多项式
PA =
1.0000 -80.0000 588.0000 -147.0000 >> PPA=poly2str(PA,'s') %用习惯的方式显示多项式
PPA =
s^3 - 80 s^2 + 588 s - 147
PPR =
x^3 + 1.1 x^2 + 0.55 x + 0.125
4.2 多项式运算函数及调用格式
举例
(s2 2)(s 4)(s 1)
例:求
s3 s 1
的“商”和“余”多项式。
>> p1=conv([1 0 2],conv([1 4],[1 1])); %计算分子多

matlab讲义第四章

matlab讲义第四章

图4.11 函数PEAKS的带洞孔曲面图
MATLAB的surf也有两个同种函数:surfc,它画出具有 基本等值线的曲面图。surfl,它画出一个有亮度的曲面 图。例如:
» [X,Y,Z]=peaks(30); » surfc(X,Y,Z) % surf plot with contour plot » grid,xlabel('x-axis'),ylabel('yaxis'),zlabel('z-axis') » title('SURFC of PEAKS')
图4.3 正弦曲线图
利用在x-y平面的矩形网格点上的z轴坐标值,MATLAB 定义了一个网格曲面。MATLAB通过将邻接的点用直线连接起 来形成网状曲面 ,其结果好象在数据点有结点的鱼网。例 如,用MATLAB的函数peaks可以画一个简单的曲面。 » [X,Y,Z]=peaks(30); » mesh(X,Y,Z) » grid on » xlabel('x-axis'),ylabel('y-axis'),zlabel('zaxis') » title('MESH of PEAKS')
图4.4 函数PEAKS的网格图
在显示器上要注意到线的颜色与网格的高度有关。一般情况下, 函数mesh有可选的参量来控制绘图中所用的颜色。关于MATLAB如何 使用、改变颜色在下面讨论。在任何情况下,由于颜色用于增加图 形有效的第四维,这样使用的颜色被称做伪彩色。 上图所示,网格线条之间的区域是不透明的。MATLAB命令 hidden控制网格图的这个特性。例如,用MATLAB的函数sphere产生 两个球面如下: » [X,Y,Z]=sphere(12); » subplot(1,2,1) » mesh(X,Y,Z),title('Opaque') » hidden on; » axis off; » subplot(1,2,2),title('Transparent'); » mesh(X,Y,Z); » hidden off; » axis off;

MATLAB 培训 第4章 MATLAB文件操作ppt课件

MATLAB 培训 第4章  MATLAB文件操作ppt课件
COUNT=fwrite (fid, A, precision) 其中COUNT前往所写的数据元素个数,fid为文 件句柄,A用来存放写入文件的数据,precision 用于控制所写数据的类型,其方式与fread函数一 样。
例4-1 建立一数据文件magic5.dat,用于存放5 阶魔方阵。 程序如下: fid=fopen('magic5.dat','w'); cnt=fwrite(fid,magic(5),'int32'); fclose(fid);
fid=fopen('magic5.dat', 'r'); [B,cnt]=fread(fid,[5,inf], 'int32'); fclose(fid);
4.2.2 文本文件的读写操作 1.读文本文件 fscanf 函数的调用格式为:
[A,COUNT]= fscanf (fid, format, size) 其中A用以存放读取的数据,COUNT前往所读取 的数据元素个数。fid为文件句柄。format用以控 制读取的数据格式,由%加上格式符组成,常见 的格式符有d,f,c,s。 size为可选项,决议矩阵A中数据的陈列方式。
fid用于存储文件句柄值,句柄值用来标识该数据
文件,其他函数可以利用它对该数据文件进展操 作。
文件数据格式有两种方式,一是二进制文件,二是 文本文件。在翻开文件时需求进一步指定文件格 式类型,即指定是二进制文件还是文本文件。
4.1.2 文件的封锁 文件在进展完读、写等操作后,应及时封锁。封 锁文件用fclose函数,调用格式为: sta=fclose(fid) 该函数封锁fid所表示的文件。sta表示封锁文件操 作的前往代码,假设封锁胜利,前往0,否那么前 往–1。

matlab第四章PPT.ppt.ppt

matlab第四章PPT.ppt.ppt

25
3.矩阵逆 如果n×n矩阵A和B,满足AB=In×m,那么B称作A的逆, 并采用符号A-1记述之。
MATLAB提供一个求矩阵逆的指令如下:
A_1=inv(A) 求非奇异方阵A的逆,使A*A_1=I
26
4.2.4 一般代数方程的解
[x, favl]=fzero(fun,x0) [x, fval]=fsolve(fun,x0) 求一元函数零点指令的最简格式 解非线性方程组的最简单格式
2 0 . 1 t
例 4 . 2 9 求 f ( t ) s i n te 0 . 5 | t | 的 零 点 。
27
(2) 数值法求解 (A)使用内联对象表示被处理函数。 (B)作图法观察函数零点分布
28
(C)利用zoom和ginput指令获得零点的初始近似值。 >>zoom on >>[tt,yy]=ginput(5);zoom off
一、近代交通业发展的原因、特点及影响 1.原因 (1)先进的中国人为救国救民,积极兴办近代交通业,促
进中国社会发展。
(2)列强侵华的需要。为扩大在华利益,加强控制、镇压
中国人民的反抗,控制和操纵中国交通建设。
(3)工业革命的成果传入中国,为近代交通业的发展提供 了物质条件。
2.特点 (1)近代中国交通业逐渐开始近代化的进程,铁路、水运和
23
1 5 9 13 2 6 10 14 x 的解。 例4.2 8求方程 3 7 11 15 4 8 12 16 如何确定解得性状(唯一与否,准确与否); 如何求特解和齐次解; 如何检查解得正确性。
24
(3) 求特解和通解,并对由它们 (1)创建待解方程的A和b (2)检查b是否在A的值空间中 构成的全解进行验算 (确定解:不唯一,准确解)

matlab第四章课件

matlab第四章课件

4.1.1 M文件的分类
M文件是由若干 Matlab 命令组合在一起构成的,它可 以完成某些操作,也可以实现某种算法
事实上,Matlab 提供的内部函数以及各种工具箱,都是利用 Matlab 语言编写的 M文件 用户也可以结合自己的工作需要,开发自己的程序或工具箱
M文件根据调用方式的不同可以分为两类: Script file:命令文件/脚本文件 Function file:函数文件
例2 输入x,y的值,并将它们的值互换后输出(swap.m)。 x=input('Input x please.'); y=input('Input y please.'); z=x; x=y; y=z; disp(x); disp(y); 例3 求一元二次方程ax2 +bx+c=0的根(root.m)。 a=input('a=?'); b=input('b=?'); c=input('c=?'); d=b*b-4*a*c; x=[(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a)]; disp(['x1=',num2str(x(1)),',x2=',num2str(x(2))]);
例如:
s=0; a=[12 13 14;15 16 17;18 19 20;21 22 23] for k=a s=s+k; end disp(s); 该程序的功能是求矩阵各行元素之和,执行结果是: 39 48 57 66
while语句
while expr (条件) statement(循环体语句) end 若expr成立,则执行循环体的内容,执行后 再判断条件是否为真,如果不成立则跳出循环体。

matlab第四讲PPT课件

matlab第四讲PPT课件

如需在同一窗口中布置独立的子图,则可在plot命令前加 上subplot命令将一个窗口划分为多个区域,每个区域一幅 子图。
语法:
subplot(m,n,k) 前图
%使m*n幅子图中的第k幅成为当
子图排列次序:先向右后向下
例:画4个子图
>>x=0:0.1:2*pi subplot(2,2,1) %分割为2*2个子图,左上方为当前图
(2)步骤2一般在图形较多的情况下,需要 指定图形窗口、子图时使用。
(3)除步骤1、2、3的其他步骤,可根据需 要改变前后次序。
4.1.3 多个图形绘制的方法
指定图形窗口
前面介绍的plot命令,绘制图形都是在默认 的Figure No.1窗口中,重复使用plot命令时, 后绘制的图形就将前面绘制的图形覆盖了。
plot(x,sin(x)) subplot(2,2,2) %右上方为当前图
plot(x,cos(x))
subplot(2,2,3) plot(x,sin(3*x))
subplot(224) 略逗号
plot(x,cos(3*x))
%左下方为当前图 %右下方为当前图,省
绘图后使用clf命令清除图形窗。 例:>>clf
(2)如果x是矩阵,而y是向量,则y的长度必须等于x的行 数或列数,绘制的方法与前述方法相似。
(3)如果x和y都是矩阵,则大小必须相同,将矩阵x的每列 和y的每列画一条曲线。
例:>>x1=[1 2 3]
y1=[1 2 3;4 5 6]
plot(x1,y1)
%每行1条曲线,第一条包含
(1,1)(2,2)(3,3),第二条包含(1,4)(2,5)(3,6)
用plot(x1,y1,x2,y2,…)命令绘制多条曲线

Matlab课件 chapter4

Matlab课件 chapter4

elseif c>='a'& c<='z'
disp(setstr(abs(c)-1)); elseif c>='0'& c<='9' disp(abs(c)-abs('0')); else
abs
ASCII of the character
setstr convert ASCII into character
3.Try/catch construct try statement 1 statement 2 …… catch statement 1 statement 2 …… end
When a try/catch construct is reached, the statements in the try block of a will be executed. If on error occurs, the statements in the catch block will be skipped, and execution will continue at the first statement following the end of the construct.
Chapter 4 MATLAB program design 4.1 M file 4.2 program controlling structure 4.3 function file 4.4 program debug
MATLAB command has two implement way: interactive command execution way M file program execution way

第4章 MATLAB程序设计PPT课件

第4章 MATLAB程序设计PPT课件
while 表达式 循环体
end
当表达式的结果为真时,反复执行循环体内 的语句,直到表达式的结果为假时,才退出循 环体
23
例:使用while结构计算1+2+3+…+100
clear sum=0; i=0; while i<100
i=i+1; sum=sum+i; end sum
运行结果: sum = 5050
28
例:分别绘制花瓣图案:r 2 s i n 3 () , r c o s 3 () , r s i n 2 () , r 3 . 5 c o s 3 ( 3 . 5 )
clear; theta=-pi:0.01:pi; rho(1,:)=2*sin(theta).^3; rho(2,:)=cos(theta).^3; rho(3,:)=sin(theta).^2; rho(4,:)=5*cos(3.5*theta).^3; for i=1:4 subplot(2,2,i); polar(theta,rho(i,:)) end
函数体内必须对输出表中的变量赋值 函数头与函数体之间可以有多个以符号"%"开始的
注释行,说明函数的功能和使用方法。当执行命令 help <文件名> 时,将显示这些注释,直到遇到第 一个非注释行为止。但是,函数体内包含的注释不 能被Help命令显示。 函数体内最常用的两个特殊变量:
NARGIN — 表示输入表中的参数个数 NARGOUT — 表示输出表中的参数个数
9

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

4. If it is possible to implement a calculation either with a for loop or using vectors, always implement the calculation with vectors, your program will be much faster.
The code in a while loop is repeated an indefinite number of times until some user-specified condition is satisfied.
Example 4.1----statistical analysis ex4.4
3.always preallocate all arrays used in a loop before executing the loop, this practice greatly increases the execution speed of the loop. The steps of extending an array: 1.Create a new array 2.Copy the contents of the old array to the new longer array 3.Add the new value to the array 4.Delete the old array
Spot the flaw! 4.2 the for loop Executes a block of statements a specified number of times. for index=expr
commands end Index is the loop variable. The loop is executed once for each column in expr.
for ii=[5 9 7] commands
பைடு நூலகம்end
for ii=[1 2 3; 4 5 6] commands
end
Quiz4.1 P162 1,2,3,4,5,6,7
Example4.3----calculating the day of year P145
Solution: for loop switch construct
Quiz 4.1 P162 8,9
Nesting loops P156 Note: inner for loop executes completely before the index variable of the outer for loop is incremented. (seldom use, too slow)
Example 4.5-----Comparing Loops and Vectorization Three ways P152
Using the MATLAB functions tic and toc
The break and continue statements
Break----terminates the execution of a loop and passes control to the next statement after the end of the loop. Continue----terminates the current pass through the loop and returns control to the top of the loop.
3.After the statements in the body of the loop have been executed, the program assigns the next column of the expression to the loop variable index, and the program executes the statements within the body of the loop again.
4.Step 3 is repeated over and over as long as there are additional columns in the control expression.
for ii=1:10 commands
end
for ii=1:2:10 commands
end
Good programming practice: 1.always indent the body of a for loop by 2 or more spaces to improve the readability of the code. 2.never modify the value of a loop index with the body of the loop.
The for loop construct functions as follows.
1.At the beginning of the loop, MATLAB generates the control expression.
2.The first time through the loop, the program assigns the first column of the expression to the loop variable index, and the program executes the statements within the body of the loop.
loops
Function: execute a sequence of statements more than once Type: while loops , for loops 4.1 the while loops General form: while expression
commands end
相关文档
最新文档