Matlab_fundamentals.ppt_第1页
Matlab_fundamentals.ppt_第2页
Matlab_fundamentals.ppt_第3页
Matlab_fundamentals.ppt_第4页
Matlab_fundamentals.ppt_第5页
已阅读5页,还剩43页未读 继续免费阅读

下载本文档

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

文档简介

1、,MATLAB Fundamentals,MATLAB,Matrix Laboratory,Problem-Solving Methodology,State the problem clearly Describe the Input/Output (I/O) Work the problem by hand Algorithm - Numerical Method Develop a MATLAB Solution Debugging and Testing Documentation,Why MATLAB?,Industry standard software application W

2、ealth of built-in functions and libraries Toolboxes (add-on software modules) image and signal processing, control systems design, fuzzy logic, etc. Has own structured programming language Ease of application and testing (pre- and post-processing without lots of programming and formatting) Platform

3、independent,MATLAB,MATLAB is a numerical analysis system Can write “programs”, but they are not formally compiled Should still use structured programming Should still use comments Comments are indicated by “%” at the beginning of the line,Comments!,Program Documentation,You must include comments in

4、the computer programs you turn in - otherwise we will have great difficulty knowing what you are doing,For example, here is some cryptic code without comment for j=0:2 k=(2-j)*(1+3*j)/2 end What does it do?,Put a comment in % turns (0,1,2) into (1,2,0),MATLAB Windows,Command Window - enter commands

5、and data - print results Graphics Window - display plots and graphs Edit Window - create and modify m-files,Managing MATLAB Environment,who or whos - See the current runtime environment clear - remove all variables from memory clc - clear the command window clf - clear the graphics window save - sav

6、e the workspace environment load - restore workspace from a disk file abort - CTRL-C help - help “command”,Really good “help” command,MATLAB Syntax,No complicated rules Perhaps the most important thing to remember is semicolons (;) at the end of a line to suppress output Type “more on” to keep text

7、from leaving screen too fast diary “filename” saves a text record of session diary off turns it off,MATLAB,MATLABs basic component is a Vector or Matrix Even single value variables (Scalars) All operations are optimized for vector use Loops run slower in MATLAB than in Fortran (not a vector operatio

8、n) “size” command gives size of the matrix,Scalars, Vectors, Matrices,MATLAB treat variables as “matrices” Matrix (m n) - a set of numbers arranged in rows (m) and columns (n) Scalar: 1 1 matrix Row Vector: 1 n matrix Column Vector: m 1 matrix, pi ans = 3.1416 size(pi) ans = 1 1, a=1 2 3; 4 5 6 a =

9、1 2 3 4 5 6 size(a) ans = 2 3,a=,Complex variables,MATLAB handles complex arithmetic automatically No need to compute real and imaginary parts separately The unit imaginary number i = is preassigned, x=5+2*i x = 5.0000 + 2.0000i y=5*x+3 y = 28.0000 +10.0000i, x=3+5-0.2 x = 7.8000 y=3*x2+5 y = 187.52

10、00 z=x*sqrt(y) z = 106.8116 A=1 2 3; 4 5 6 A = 1 2 3 4 5 6 b=3;2;5 b = 3 2 5 C=A*b C = 22 52, who Your variables are: A b y C x z whos Name Size Bytes Class A 2x3 48 double array C 2x1 16 double array b 3x1 24 double array x 1x1 8 double array y 1x1 8 double array z 1x1 8 double array save Saving to

11、: matlab.mat save matrix1,MATLAB Example,default filename filename “matrix1”,Data types,All numbers are double precision Text is stored as arrays of characters You dont have to declare the type of data (defined when running) MATLAB is case-sensitive!,Variable Names,Usually, the name is identified wi

12、th the problem Variable names may consist of up to 31 characters Variable names may be alphabetic, digits, and the underscore character ( _ ) Variable names must start with a letter,ABC, A1, C56, CVEN_302 day, year, iteration, max time, velocity, distance, area, density, pressure Time, TIME, time (c

13、ase sensitive!),Initializing Variables,Explicitly list the values reads from a data file uses the colon (:) operator reads from the keyboard,A = 1; 3; 5; 10; B = 1 3 5; -6 4 -1 C = 2 3 5 1; 0 1 (continuation) 1 -2; 3 5 1 -3 E = A; 1; A; F = C(2,3); A,Matrix Concatenation,Colon Operator,Creating new

14、matrices from an existing matrix,C = 1,2,5; -1,0,1; 3,2,-1; 0,1,4 F = C(:, 2:3) = 2,5; 0,1; 2,-1; 1,4,Colon Operator,Creating new matrices from an existing matrix,C = 1,2,5; -1,0,1; 3,2,-1; 0,1,4 E = C(2:3,:) = -1 0 1; 3 2 -1,Colon Operator,Creating new matrices from an existing matrix,C = 1,2,5; -1

15、,0,1; 3,2,-1; 0,1,4 G = C(3:4,1:2) = 3,2; 0,1,Colon Operator,Variable_name = a:step:b time = 0.0:0.5:2.5 time = 0.0, 0.5, 1.0, 1.5, 2.0, 2.5 Negative increment values = 10:-1:2 values = 10, 9, 8, 7, 6, 5, 4, 3, 2,linspace Function,linspace(x1, x2) gives 100 evenly spaced values between x1 and x2 x =

16、 linspace(x1,x2) linspace(a,b,n) generate n equally spaced points between a and b x = linspace(a,b,n), linspace(0,2,11) ans = Columns 1 through 7 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 Columns 8 through 11 1.4000 1.6000 1.8000 2.0000,logspace Function,logspace(a,b,n) generates a logarithmically

17、 equally spaced row vector x = logspace(a,b,n) logspace(a,b) generates 50 logarithmically equally spaced points x = logspace(a,b), logspace(-4,2,7) ans = 0.0001 0.0010 0.0100 0.1000 1.0000 10.0000 100.0000,Special Matrices,Scalar Arithmetic Operations,Example: x = (a + b*c)/d2 count = count + 1,(Mat

18、rix inverse),In order of priority,Order of Precedence of Arithmetic Operations,Parentheses, starting with the innermost pair Exponentiation, from left to right Multiplication and division with equal precedence, from left to right Addition and subtraction with equal precedence, from left to right,Exa

19、mples: factor = 1 + b/v + c/v2 slope = (y2 - y1)/(x2 - x1) loss = f * length/dia * (1/2 * rho * v2) func = 1 + 0.5*(3*x4 + (x + 2/x)2),Order of Precedence of Arithmetic Operations,The priority order can be overridden with parentheses, y = -7.32 y = -53.2900 y=(-7.3)2 y = 53.2900, a=3; b=5; c=2; s1 =

20、 a-b*c s1 = -7 s2=(a-b)*c s2 = -4,Exponentiation has higher priority than negation,Multiplication has higher priority than subtraction,Array Operations,An array operation is performed element-by-element,MATLAB: C = A.*B;,Element-by-Element Operations,But a*b gives an error (undefined) because dimens

21、ions are incorrect. Need to use .*,Vector and Matrix operations,Vectorized Matrix Operations,Array Operations for m n Matrices,Matrix Transpose,Built-in Functions,All the standard operators +, , *, /, Sqrt( ), abs( ), sin( ), cos( ), exp( ), tanh( ), acos( ), log( ), log10( ), etc. These operators a

22、re vectorized,Built-in Functions,Certain functions, such as exponential and square root, have matrix definition also Use “help expm” and “help sqrtm” for details, A = 1 3 5; 2 4 6; -3 2 -1 A = 1 3 5 2 4 6 -3 2 -1 B = sqrt(A) B = 1.0000 1.7321 2.2361 1.4142 2.0000 2.4495 0 + 1.7321i 1.4142 0 + 1.0000

23、i C = sqrtm(A) C = 2.1045 + 0.0000i 0.1536 - 0.0000i 1.8023 + 0.0000i 1.7141 - 0.0000i 1.1473 + 0.0000i 1.7446 + 0.0000i -2.0484 + 0.0000i 1.3874 + 0.0000i 0.5210 - 0.0000i,(element by element),(C*C = A),MATLAB Graphics,One of the best things about MATLAB is interactive graphics “plot” is the one yo

24、u will be using most often Many other 3D plotting functions - plot3, mesh, surfc, etc. Use “help plot” for plotting options To get a new figure, use “figure” logarithmic plots available using semilogx, semilogy and loglog,plot(x,y) defaults to a blue line plot(x,y,ro) uses red circles plot(x,y,m*) u

25、ses magenta asterisks If you want to put two plots on the same graph, use “hold on” plot(a,b,r:) (red dotted line) hold on plot(a,c,ko) (black circles),Plotting Commands,Free-Falling Bungee Jumper,Use built-in functions sqrt m = 75.2; cd = 0.24; t = 0:1:20 t = Columns 1 through 15 0 1 2 3 4 5 6 7 8

26、9 10 11 12 13 14 Columns 16 through 21 15 16 17 18 19 20 v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t) v = Columns 1 through 9 0 9.7089 18.8400 26.9454 33.7794 39.2956 43.5937 46.8514 49.2692 Columns 10 through 18 51.0358 52.3119 53.2262 53.8772 54.3389 54.6653 54.8956 55.0579 55.1720 Columns 19 through 21 55

27、.2523 55.3087 55.3484,Plot the velocity versus time curve, plot(t,v); grid on title(Free Falling Bungee Jumper) xlabel(time t (second); ylabel(velocity v (m/s) print djpeg bungee.jpg,How to change line thickness, line color, font size, symbols, marker size, etc.?,Color, Symbols, and Line Types,Use “

28、help plot” to find available Specifiers,b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus - dashed m magenta * star y yellow s square k black d diamond v triangle (down) triangle (up) triangle (right) p pentagram h hexagram,Colors Symbols Line Types, x=0:0.1:5;

29、 y=2*x.3-12*x.2+8*x-6; H=plot(x,y,b,x,y,r*); set(H,LineWidth,3,MarkerSize,12) xlabel(x); ylabel(y); title(f(x)=2x3-12x2+8x-6); print -djpeg075 poly.jpg,element-by-element operations x.n,Adjust line thickness, font size, marker size, etc., x=0:0.1:10; y=sin(2.*pi*x)+cos(pi*x); H1=plot(x,y,m); set(H1,LineWidth,3); hold on; H2=plot(x,y,bO); set(H2,LineWidth,3,MarkerSize,10); hold off; xlabel(x); ylabel(y); title(y = sin(2pix)+cos(pix); print -djpeg075 function.jpg, x=0:0.1:3; y1=exp(-x); y2=sqrt(x); H=plot(x,y1,b-,x,y2

温馨提示

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

评论

0/150

提交评论