MATLAB简介3MATLABm-file程式与多项式函数.ppt_第1页
MATLAB简介3MATLABm-file程式与多项式函数.ppt_第2页
MATLAB简介3MATLABm-file程式与多项式函数.ppt_第3页
MATLAB简介3MATLABm-file程式与多项式函数.ppt_第4页
MATLAB简介3MATLABm-file程式与多项式函数.ppt_第5页
已阅读5页,还剩47页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

What is an m-file?,An m-file is a file with extension .m It can be used to store commands Matlab reads and executes the commands in an m-file if you type the name of the file in the command window,Use an ordinary text editor and save the file as filename.m Choose new file under the File Menu,Different ways of creating m-files,In the first case the function inv(A) is used to find the inverse. This is then multiplied by b. The Matlab code for this operation is,In the second case left- division is performed straight away with the command,X= 9.2500 4.2500 2.7500,X= 9.2500 4.2500 2.7500,Example,Example solvetime.m A = rand(1000,1000); % Creates a random % matrix A b = rand(1000,1); % Creates a random % vector b det(A) % Calculates the determinant of A tic, % Starts the time-watch x = inv(A)*b; % Solves the system toc % Stops the watch tic, y = Ab; toc % Solves and times the %system with left division,lapsed_time = 1.2820 elapsed_time = 0.5310,以下的tutex1.m档是一个简易绘图程式做为示范使用M-file,% M-file, tutex1.m % Simple plot for illustration of using M-file. % 简易绘图以做为示范使用M-file x=linspace(0,2*pi,20); y=sin(x); plot(x,y,r+) xlabel(x-value) ylabel(y-value) title(2D plot),When you write something in Matlabs command window, the following things are checked in turn: 1. Is there such a variable in the workspace? 2. Is there an m-file called that thing in the current directory? 3. Is there an m-file called that somewhere else? When Matlab searches for the m-file it looks in specific folders, its search path. If the folder is not added to the path Matlab can not run your script.,How to make sure Matlab finds your m-file,Change current directory using the cd command or by clicking in the Directory panel Select the Set Path option in the File menu Type one of the following commands in the command window: addpath C:ProgramMatlab6k5myfiles path(path,C:ProgramMatlab6k5myfiles), cd wufilemy_work % 切换至目录wufilemy_work cd % 如果只用 cd 则会显示目前的目录 c:WUFILEMY_WORK dir % 列出目录下的档案 . tutex1.m tutex2.m test.txt delete test.txt % 删除 test.txt, path(path,c:wufilemy_work) % 将自己的目录 wufilemy_work 加在 % MATLAB的搜寻路径之后 path(c:wufilemy_work,path) % 将自己的目录 wufilemy_work 加在 % MATLAB的搜寻路径之前,What is a function A function is an m-file beginning with the word function A function has a user-specified input and output A function has its own local workspace. Variables defined in the function are not stored in the ordinary workspace. Nor are your workspace variables altered if given a new value in the local workspace.,myfile.m function c = myfile(a,b) c =sqrt(a.2)+ (b.2);,myfun.m function y = myfun(t) y = t * sin(t);,Example,a=4 b=3 c = myfile(a,b) c= 5.0000,A function does not assign any values to anything. Exception to this rule is if you define a global variable. Type help global for more information You can have several functions stored in one file. The subfunctions work exactly as a normal function, but can not be accessed directly from the command window (unlike other languages).,Example spir.m function x,y = spir(t) x = cos(20*t).*exp(-t.2); y = sin(20*t).*exp(-t.2); spirplot.m function x,y = spirplot(t) x = cos(20*t).*exp(-t.2); y = sin(20*t).*exp(-t.2); plot(x,y),spir3.m function x,y,z = spir3(t) x = cos(20*t).*exp(-t.2); y = sin(20*t).*exp(-t.2); z = exp(-t.2); plot3(x,y,z),Loops A for-loop assigns a number of different values to a parameter, then stops performing the task. Begins with the word for, ends with the word end. for n = 1:N a(n) = 1/n; end,You can write the loops directly in the command window: for n = 1:N, t(n) = 1/n; end for r = linspace(0,1,20), q = sin(r); end for a = A, plot(a), end Commas separate several commands on a line. If is a matrix the loop variable becomes a vector of the separate columns in A,clear % Clears the workspace A = peaks; % A is the test-matrix peaks for a = A plot(a) % a obtains the value of % each column in A drawnow % Updates the figure window % immediately pause end,Example,ApBloop.m A = ones(1000,1000); % Creates matrices A and B B = zeros(1000,1000); C = zeros(1000,1000); tic, % Starts the stop-watch for n = 1:1000 % Start of the first for-loop for m = 1:1000 % Start of the second for -loop C(n,m) = A(n,m)+B(n,m); end % Ends the second loop end % Ends the second loop toc tic, C = A+B; toc,elapsed_time = 0.1250 elapsed_time = 0.0150,elapsed_time = 0 elapsed_time = 0.0310,resizeloop.m A = ones(500,500); % Creates matrices A, B and C B = zeros(500,500); tic, C = A+B; toc tic, % Starts the stop-watch for n = 1:500 % Start of the first for-loop for m = 1:500 % Start of the second for -loop C(n,m) = A(n,m)+B(n,m); end % Ends the first loop end % Ends the second loop toc,A while-loop performs a task until a logical test proves false. Begins with the word while, ends with the word end while t tmax, a = sin(t); t = t + dt; end,while-loops,Save time Use Matlab! Document your scripts and functions: Add comments and help. If the first lines in an m-file are commented, these will be displayed in the command window when you type help filename Indents makes it easier to read the program Include error possibilities in your functions and scripts: error(error message),Use matrices rather than loops whenever possible. Include matrix arguments in your functions. vectorize.m % Compares the computational speed % of vectorized code and a for-loop clear tic, A = sin(1:1e6).*1:1e6; % Vectorized code toc tic, for n = 1:1e6 A(n) = sin(n)*n; % For-loop end toc,Matlab多项式函数,多项式常被用来模拟一个物理现象的解析函数,之所以采用多项式,是因为它很容易计算。在这里我们将说明如何做多项式的计算及解多项式的根。,令p(x) 代表一个多项式如下 MATLAB 以一最简便方式代表上述的多项式 p=1 4 -7 -10,其中的数值是多项式的各阶项(从高到低)的 各个系数,其实p 也是一个阵列不过是用以代表这个多项式。 有了多项式的表示式后,我们即可来计算其函数值。假设要计算一组数据x对应的多项式值,依照一般的函数 计算须以下列式子计算:, p=x.3+4*x.2-7*x-10 为了能直接运用多项式,可以用函数 polyval直接做运算,语法为 polyval(p,x),其中p 即是代表多项式各阶系数 的阵列。因此 x=linspace(-1,3,N); p=1 4 7 -10; v=polyval(p,x);,我们接著说明如何对二个多项式做加减乘除运算。当二个多项式间要做加减乘除时,加 减运算可以直接进行。假设有二个多项式 a(x) 和 b(x) 定义如下:,如果多项式 c(x) 为上述二多项式相加,即 c(x) = a(x) + b(x), 因此,如果是二多项式相减得到的多项式为 d(x) = a(x) - b(x), 则,以下就介绍相关范例,来说明二个多项式的加减运算:, a=1 2 3 4; b=1 4 9 16; c=a+b c = 2 6 12 20 d=a-b d = 0 -2 -6 -12,而将两个多项式相乘可以得到一新的多项式 e(x) = a(x) b(x),如果是两个多项式相除,即 :,上述二个运算式不能直接运算,须要另外定义函数conv做乘法运算以及函数deconv做除法运算。当二多项式相乘,在数学上等于二个阵列做卷积(convolution)运算(因为我们是以阵列来代表一个多项式的各阶系数), 因此可利用conv函数做乘法运算,其语法为conv(a,b),其中a, b代表二个多项式的阵列。而二多项式相除就相 当于反卷积(de-convolution) 运算,因此有 deconv 函数,其语法稍有不同 q,r=deconv(a,b),其中q,r分别代表整 除多项式及余数多项式。如果直接使用q=deconv(a,b)则只求出整除多项式。,以下就介绍相关范例,来说明二个多项式的乘除运算: a=1 2 3 4; b=1 4 9 16;, e=conv(a,b) e = 1 6 20 50 75 84 64 g=e+0 0 0 c g = 1 6 20 52 81 96 84,(c = 2 6 12 20), f,r=deconv(e,b) f = 1 2 3 4 r = 0 0 0 0 0 0 0 % 因为是整除所以余数多项式的各系数皆为零, h,r=deconv(g,a) h = 1 4 9 18 r = 0 0 0 0 2 6 12 % 余数多项式为 2*x2 + 6*x + 12,多项式的根,一个多项式视其阶数而定,它的根可以有一个到数个,可能为实数也可能是复数。要求一高阶多项式的根往 往须借助数值方法,所幸MATLAB已将这些数值方法写成一函数roots(p),我们只要输入多项式的各阶系数( 以 p 代表)即可求解到对应的根, p=1 3 2; r=roots(p) r = -2 -1 p=1 -12 0 25 116; % 注意二阶项系数为零须要输入,否则多项式的阶数就不对 r=roots(p) % 有实数根及复数根 r = 11.7473 2.7028 -1.2251 + 1.4672i -1.2251 - 1.4672i,与 roots 相关的函数尚有 poly,real,这二个函数的用途是要验算求解的根展开能求得原多项式。 例如有一个二次方程式的根为-2, -1,则以下式计算原多项式 p(x)=(x+2)(x+1)=x2+3x+2 poly 函数就是在求出多项式的各阶系数,其语法为 poly(r),其中 r 是代表根的阵列。而 real 则是用来去除因计算时产生的假虚部系数,为何会有此种情形请参考以下的例子。, r=-2 -1; pp=poly(r) % pp=(x+2)(x+1)=x2+3x+2 pp = 1 3 2 p=1 -4 6 -4; r=roots(p) r = 2.0000 1.0000 + 1.0000i 1.0000 - 1.0000i pp=poly(r) % 这个多项式的系数与原多项式 p 相同 pp = 1 -4 6 -4, pp=1 7 12 9; % 再看另一个多项式 r=roots(pp) r = -4.9395 -1.0303 + 0.8721i -1.0303 - 0.8721i pp=poly(r) % 注意因计算的误差会有假虚部产生 pp = 1.0000 7.0000 12.0000 9.0000 + 0.0000i pp=real(pp) % 可以real将假虚部去除,将原多项式还原 pp = 1.0000 7.0000 12.0000 9.0000,非线性方程的实根,如果求根的方程不为多项式的形式, 就不能用 roots 函数。而这类的方程多半是非线性方程, 其函数形式变化很大。对于解这类方程的根,可以用 fzero函数,它其实是用来找一函数 f(x) 的 x 值代入时,会使该函数值为零 (f(x)=0);而这也就是根的特性,因此我们可以用 fzero求根。,要求任一方程的根有三步骤: (1)先定义方程。要注意必须将方程安排成 f(x)=0 的形式,例如一方程为sin(x)=3, 则该方程式应表示为 f(x)=sin(x)-3。可以 用m-file 定义方程。 (2)代入适当范围的 x, y(x) 值,将该函数的分布图画出,藉以了解该方程的长相。,(3)由图中决定y(x)在何处附近(x0)与 x 轴相交,以fzero的语法fzero(function,x0) 即可求出在 x0附近的根,其中 function 是先前已定义的函数名称。如果从函数分布图看出根不只一 个,则须再代入另一个在根附近的 x0,再求出下一个根。 以下分别介绍几数个方程式,来说明如何求解它们的根。,例一、方程为 sin(x)=0 我们知道上式的根有 ,求根方式如下: r=fzero(sin,3) % 因为sin(x)是内建函数,其名称为sin, %因此无须定义它选择 x=3 附近求根 r = 3.1416 r=fzero(sin,6) % 选择 x=6 附近求根 r = 6.2832,例二、方程为MATLAB 内建函数 humps,我们不须要知道这个方程的形态为何,不过我们可以将它画出来,再找出根的位置。求根方式如下: x=linspace(-2,3); y=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论