Giáo trình MATLAB 54m

252 322 0
Giáo trình MATLAB 54m

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

MATLAB BEGINNING MATLAB MATLAB – High Level Programming Language Easy Development MATLAB BASIC JAVA Fortran C C++ Assembly Efficient Operation Machine Code MATLAB CHAPTER An Overview of MATLAB MATLAB : Chapter An Overview of MATLAB References Co So Matlab va Ung dung, Author: Pham Thi Ngoi Yen Publisher: Nha xuat ban Khoa hoc va Ky thuat, Ha Noi Year: 2006 The MATLAB Help System Essential MATLAB® for Engineers and Scientists Author: Brian D Hahn and Daniel T Valentine Publisher: Elsivier Year: 2007 MATLAB : Chapter An Overview of MATLAB Starting Matlab The default MATLAB Desktop MATLAB : Chapter An Overview of MATLAB Operations (1/2) Scalar arithmetic operations Symbol Operation MATLAB form ^ exponentiation: ab a^b * multiplication: ab a*b / right division: a/ b  b \ left division: + addition: - subtraction: a a\b  a b a b b a a/b a\b a+b a-b MATLAB : Chapter An Overview of MATLAB Operations (2/2) Calculation result is saved to ‘ans’ by default : ans = is saved to ans by using ans one more time 1.5 is saved to the variable ‘a’ by assign ‘a’ as the storing place In the Matlab, ‘\’ = ‘\’ MATLAB : Chapter An Overview of MATLAB assignment , workspace, and command  The sign ‘=‘ is called the ‘assignment or replacement’ operator e.g x = : assign the value to the variable x x = x + : add to the current value of x    Workspace : the names and values of any variables in use in the current work session Commands for managing the work session clc clears the Command window clear Removes all variables from memory exist(‘name’) Determines if a file or variable exists having the name ’name’ quit Stops MATLAB who Lists the variable currently in memory whos Lists the current variables and sizes, and indicates if they have imaginary parts : Colon; generates and array having regularly spaced elements , Comma; separates elements of an array ; Semicolon; suppresses screen printing; also denotes a new row in an array … Ellipsis; continues a line MATLAB : Chapter An Overview of MATLAB recall feature, and special variable  recall feature recall a previously typed function or variable  e.g up-arrow key(↑) & down-arrow key(↓): move up and down through the previously typed lines one line at a time Tab key: automatically completes the name of a function, variable, or file if you type the first few letters of the name and press the Tab key   Special variables and constants ans Temporary variable containing the most recent answer eps Specifies the accuracy of floating point precision i,j The imaginary unit Inf Infinity NaN Indicates an undefined numerical result pi The number π 1 MATLAB : Chapter An Overview of MATLAB Complex Number Operations ‘;’ is a command which leads to a new line, also omits calculation results MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (differentiation) (1/9)  diff (E) ex dx n  nx n 1 dx ex >> diff((sin(x)^2) >> syms n x y >> diff(x^n) ans = x^n*n/x >> simplify(ans) ans = x^(n-1)*n ans = 2*sin(x)*cos(x) ex ex d ln x  dx x >> diff(log(x)) d sin x  sin x cos x dx d sin y  cos y dy >> diff((sin(y)) ans = cos(y) ans = 1/x MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (differentiation) (2/9) ex f ( x, y)  sin( xy ) df  y cos(xy ) dx  diff (E, v) [ x sin( xy )]  x cos( xy ) y ex >> syms x y >> diff(x*sin(x*y),y) >> diff(sin(x*y)) ans = ans = x^2*cos(x*y) cos(x*y)*y ex x2 1) >> diff(sin(x*y))  diff (E, n) ex ans = cos(x*y)*y 2) 3) >> E=‘x^2’; >> diff(E) ans= 2*x >> syms x >> diff(x^2) ans = 2*x  diff (E, v, n) ex d ( x3 )  6x dx >> syms x >> diff(x^3,2) ans = 6*x  [ x sin( xy )]   x sin( xy ) y >> syms x y >> diff(x*sin(x*y),y,2) ans = -x^3*sin(x*y) MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (integration) (3/9)  ex int (E) >> syms x >> int(2*x) ans = x^2 ex n  x dx  x n 1 n 1  xdx  ln x  cos xdx  sin x  sin ydy   cos y >> syms n x y >> int(x^n) ans = x^(n+1)/(n+1) >> int(1/x) ans = log(x) >> int(cos(x)) ans= sin(x) >> int(sin(y)) ans= -cos(y) MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (integration) (4/9)  int (E, v) ex xn  x dn  ln x n >> syms n x >> int(x^n,n) ans = 1/log(x)*x^n  int (E, a, b) ex  x dx  x3  39 >> syms x >> int(x^2,2,5) ans = 39 MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (integration) (5/9)  ex int (E, v, a, b)  y 125 xy dy  x x 0 3  ex  a b3 a  3 >> syms a b x >> int(x^2,a,b) ans = 1/3*b^3-1/3*a^3 xdx  x2 t 1  t2  2 sin xdx   cos x te   cos(et )  cos t t >> syms t x >> int(x,1, t) ans = 1/2*t^2-1/2 >> int(sin(x),t,exp(t)) ans = -cos(exp(t))+cos(t) 125/3*x x dx   et t ans = b  t >> syms x y >> int(x*y^2,y,0,5) ex int (E, m, n) ex  x  1dx  ln x  >> syms x >> int(1/(x-1)) ans = log(x-1) >> int(1/(x-1),0,2) ans = NaN :singularity at x=1 MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (Taylor series) (6/9)  Taylor’s theorem  d2 f  df  f ( x)  f (a)    x a ( x  a)   2  dx  dx  ex sin x  x  x3 x5 x7    ,  x   3! 5! 7! cos x   x2 x4 x6    ,  x   2! 4! 6! ex  1 x     dk f  x a ( x  a)     k k!  dx   dn f   x a ( x  a) k    Rn , Rn   n n!  dx  x x3 x    ,  x   2! 3! 4! taylor (f, n, a) ex >> syms x >> f=exp(x); >> taylor(f,4) ans = 1+x+1/2*x^2+1/6*x^3 >> taylor(f,3,2) ans = exp(2)+exp(2)*(x-2)+1/2*exp(2)*(x-2)^2 =( e [1  ( x  2)  ( x  2) ] )   x b ( x  a) n  MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (sums) (7/9)  symsum (E) x 1  E( x)  E(0)  E(1)  E (2)    E( x  1) x 0 ex  S = symsum (E, a, b) b  E( x)  E(a)  E(a  1)  E(a  2)    E (b) x a 10 ex  k         10  55 k 0 n 1 1 k        n 1  n2  n  2 k 0 k k 1     16  30 >> syms k n >> symsum(k,0,10) ans = 55 >> symsum(k,0,n-1) ans = 1/2*n^2-1/2*n >> factor(ans) ans = 1/2*n*(n-1) >> symsum(k^2,1,4) ans = 30 MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (limits) (8/9)  E ( x) ) limit (E, a) (x lim a  limit (E) : limit as x  ex sin(ax) a x 0 x >> syms h x >> limit((x-3)/(x^2-9),3) ans = 1/6 >> limit((sin(x+h)-sin(x))/h,h,0) ans = cos(x) lim >> syms a x >> limit(sin(a*x)/x) ans = a  limit (E, v, a) : limit as v  a ex x 3 lim  x 3 x 9 sin( x  h)  sin( x) h h0  limit (E, v, a, 'right')  limit (E, v, a, ‘left') ex   x  x lim  x  x lim >> syms x >> limit(1/x,x,0,'left') ans = -Inf lim >> limit(1/x,x,0,'right') ans = Inf MATLAB : Chapter 10 Symbolic processing with MATLAB Calculus (9/9) Symbolic calculus functions Command Description diff (E) Returns the derivative of the expression E with respect to the default independent variable diff (E,v) Returns the derivative of the expression E with respect to the variable v diff (E,n) Returns the nth derivative of the expression E with respect to the default independent variable diff (E,v,n) Returns the nth derivative of the expression E with respect to the variable v int (E) Returns the integral of the expression E with respect to the default independent variable int (E,v) Returns the integral of the expression E with respect to the variable v int (E,a,b) Returns the integral of the expression E with respect to the default independent variable over the interval [a,b], where a and b are numeric quantities int (E,v,a,b) Returns the integral of the expression E with respect to the variable v over the interval [a,b], where a and b are numeric quantities int (E,m,n) Returns the integral of the expression E with respect to the default independent variable over the interval [m,n], where m and n are symbolic expressions limit (E) Returns the limit of the expression E as the default independent variable goes to limit (E,a) Returns the limit of the expression E as the default independent variable goes to a limit (E,v,a) Returns the limit of the expression E as the variable v goes to a limit (E,v,a,’d’) Returns the limit of the expression E as the variable v goes to a from the direction specified by d, which may be right or left symsum (E) Returns the symbolic summation of the expression E taylor (f,n,a) Gives the first n-1 terms in the Taylor series for the function defined in the expression f, evaluated at the point x=a If the parameter a is omitted, the function returns the series evaluated at x=0 MATLAB : Chapter 10 Symbolic processing with MATLAB Differential Equations (1/6)  Solving a Single Differential Equation The dsolve function’s syntax: dsolve (‘eqn’) → returns a symbolic solution of the ODE specified by the symbolic expression eqn  The uppercase letter D → the first derivative, D2 → the second derivative (ex) Dw → dw/dt  Cannot use uppercase D as symbolic variable when using the dsolve function  (example) dy  y  12 dt Analytic solution: y(t )   C1e2t A symbolic solution using the dsolve function MATLAB : Chapter 10 Symbolic processing with MATLAB Differential Equations (2/6)  Solving Sets of Equations  The appropriate syntax: dsolve (‘eqn1’, ‘eqn2’,…) → returns a symbolic solution of the set of equations specified by the symbolic expressions eqn1 and eqn2 (example) dx  3x  y dt dy  4 x  y dt Analytic solution: x(t )  C1e3t cos 4t  C2e3t sin 4t y (t )  C1e3t sin 4t  C2e3t cos 4t MATLAB : Chapter 10 Symbolic processing with MATLAB Differential Equations (3/6)  Specifying Initial and Boundary Conditions  The appropriate syntax: dsolve(‘eqn’,’cond1’,’cond2’) → returns a symbolic solution of the ODE specified by the symbolic expression eqn, subject to the conditions specified in the expressions cond1, cond2, and so on (example) d2y  c y, y(0)  1, y(0)  dt Analytic solution: y(t )  (ect  ect ) / MATLAB : Chapter 10 Symbolic processing with MATLAB Differential Equations (4/6)  Plotting the Solution (1)  The ezplot function can be used to plot the solution (example) 1-4/29 cos(4 t)+10/29 sin(4 t)-25/29 exp(-10 t) dy  10 y  10  sin(4t ), y(0)  dt Analytic solution: y(t )   10 25 cos(4t )  sin(4t )  e 10t 29 29 29 1.8 1.6 1.4 1.2 0.8 0.6 0.4 0.2 0 0.5 1.5 2.5 t 3.5 4.5 MATLAB : Chapter 10 Symbolic processing with MATLAB Differential Equations (5/6)  Plotting the Solution (2)  Sometimes the ezplot function uses too few values of the independent variable and thus does not produce a smooth plot  To override the spacing chosen by the ezplot function, you can use the subs function to substitute an array of values for the independent variable Define t to be a symbolic variable MATLAB : Chapter 10 Symbolic processing with MATLAB Differential Equations (6/6) (Table 10.4-1) The dsolve function Command Description dsolve(‘eqn’) Returns a symbolic solution of the ODE specified by the symbolic expression eqn Use the uppercase letter D to represent the first derivative; use D2 to represent the second derivative, and so on Any character immediately following the differentiation operator is taken to be the dependent variable dsolve(‘eqn1’,’eqn2’,…) Returns a symbolic solution of the set of equations specified by the symbolic expressions eqn1, eqn2, and so on dsolve(‘eqn’,’cond1’,’cond2’,…) Returns a symbolic solution of the ODE specified by the symbolic expression eqn, subject to the conditions specified in the expressions cond1, cond2, and so on If y is the dependent variable, these conditions are specified as follows: y(a) = b, Dy(a) = c, D2(a) = d, and so on dsolve(‘eqn1’,’eqn2’,…,’cond1’,’cond2’,…) Returns a symbolic solution of a set of equations specified by the symbolic expressions eqn1, eqn2, and so on, subject to the initial conditions specified in the expressions cond1, cond2, and so on [...]... (the ordinate) MATLAB : Chapter 1 An Overview of MATLAB Plotting with MATLAB Variable pairs must be written (x,y) and (x,z) are pairs, so express ‘plot(x,y,x,z)’ MATLAB : Chapter 1 An Overview of MATLAB Linear Algebra Equations  the left division operator(\)  e.g 6x  12y 4z  70 7x - 2y  3z  5 2x  8y - 9z  64 Matlab s basic unit is ‘matrix’ MATLAB : Chapter 1 An Overview of MATLAB Statistics,... http://www.mathworks.com MATLAB : Chapter 1 An Overview of MATLAB The MATLAB Help System(2/4) - Help Browser  The MATLAB Help Browser  Contents: a contents listing tab  Index: a global index tab  Search: a search tab having a find function and full text search features  Demos: a bookmaking tab to start built-in demonstrations MATLAB : Chapter 1 An Overview of MATLAB The MATLAB Help System(3/4) - Help Functions  MATLAB. .. the directory dirname from the search path what Lists the MATLAB- specific files found in the current working directory Most data files and other non -MATLAB files are not listed Use dir to get a list of all files what dirname Lists the MATLAB- specific files in directory dirname MATLAB : Chapter 1 An Overview of MATLAB Plotting commands  Some MATLAB plotting commands [x , y] = ginput(n) Enables the... Displays the M-file filename without opening it with a text editor MATLAB : Chapter 1 An Overview of MATLAB The MATLAB Help System(4/4) - Help Functions  Examples MATLAB CHAPTER 2 Numeric, Cell, and Structure Arrays 28 MATLAB : Chapter 2 Numeric, Cell, and Structure Arrays Arrays  Arrays : Collection of numbers  the basic building block in MATLAB  addition, subtraction, multiplication, division, and... functions  Some commonly used mathematical functions ◈The MATLAB trigonometric functions use radian measure Function ex x ln x l og1 0x cos x si n x tan x cos-1x si n-1 x tan-1 x MATLAB syntax(◈) exp(x) sqrt(x) log(x) log10(x) cos(x) sin(x) tan(x) acos(x) asin(x) atan(x) MATLAB : Chapter 1 An Overview of MATLAB Working with Files  M-file: MATLAB function files and program files are saved with the... :when need to use many commands or arrays with many elements  function files :when need to repeat the operation of a set of commands MATLAB : Chapter 1 An Overview of MATLAB Script Files and the Editor/Debugger(2/2)  M-file make and save MATLAB : Chapter 1 An Overview of MATLAB Input/output commands disp (A) Displays the contents, but not the name, of the array A disp (‘text’) Displays the text string... ‘option1’,’option2’, and so on MATLAB : Chapter 1 An Overview of MATLAB The MATLAB Help System(1/4)  Help Browser  Graphical user interface  find information, view online documentation  Help Functions  help, lookfor, doc  display syntax information for specified function  Other Resources  run demos, contact technical support, participate in a newsgroup  The MathWorks Website  the home of MATLAB  http://www.mathworks.com... 10] MATLAB : Chapter 1 An Overview of MATLAB Array(2/2)  You can compute ‘w=5*sin (u)’ for ‘u=[0: 0.1: 10]’ >> u=[0: 0.1: 10]; >> w=5*sin(u);  computed the formula ‘w=5*sin(u)’ 101 times  array index: points to a particular element in the array >>u(7) ans = 0.6000 >>w(7) ans = 2.8232  length: determine how many values are in an array >>m = length(w) m= 101 MATLAB : Chapter 1 An Overview of MATLAB. .. Equations, and Simulink  MATLAB can numerically compute the derivative dy/dx and  y d x the integral  Symbolic Processing y d x in  obtain the derivative dy/dx and the integral  symbolic form (a formula instead of as a set of numerical values) MATLAB : Chapter 1 An Overview of MATLAB Script Files and the Editor/Debugger(1/2)   Two ways for performing operations in MATLAB  Interactive mode :... vector i, j, k  express the vector p = xi + yj + zk  e.g  p = 5i + 7j + 2k  in MATLAB: write in a specific order, separate with a space, identify the group with brackets  [5 7 2]  vector  row vector [5 7 2]  column vector 30 MATLAB : Chapter 2 Numeric, Cell, and Structure Arrays Arrays  Creating Vectors in MATLAB  MATLAB displays row vectors horizontally and column vectors vertically  Create

Ngày đăng: 17/09/2016, 10:24

Tài liệu cùng người dùng

Tài liệu liên quan