Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering
Dr. Jie Zou PHY Introduction to MATLAB: Fundamentals 1 Outline The MATLAB environment Assignment Mathematical operations Use of built-in functions Graphics 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2 nd ed., Steven C. Chapra, McGraw Hill, 2008, Ch. 2.
Dr. Jie Zou PHY The MATLAB environment Start MATLAB, command window, command prompt >> You can type in commands line by line after >>; for each command, you get a result. Example: Type in the following and hit return >> MATLAB will display ans = 39 MATLAB assigns the result to ans whenever you do not explicitly assign the calculation to a variable of your own choosing. Now, type in >> ans + 11 What do you think MATLAB will display?
Dr. Jie Zou PHY Assignment Assign values to variable names: scalars, arrays, vectors, and matrices. Scalars: A scalar is a single value. EnterThen enter >> a = 4 >> a = 4, A = 6; a = 4 4 Notice Echo Printing if “,” is used; echo printing is suppressed when “;” is used. EnterThen enter >> a >> A Note that names are Case-Sensitive in MATLAB.
Dr. Jie Zou PHY Assignment (cont.) Arrays: An array is a collection of values that are represented by a single variable name; Vectors: 1D arrays; Matrices: 2D arrays. Examples: Create a row vector and a column vector EnterThen enter >> a = [ ] >> b = [ 2; 4; 6] a = or>> b = [2 4 6]’ 1 2 3b = Also note that this current assignment of a Overrides the previous assignment of a = 4.
Dr. Jie Zou PHY Assignment (cont.) Example: Create a matrix >> A = [1 2 3; 4 5 6; 7 8 9] A = Example: Access an individual element of an array >> b (2) >> A (2, 3) ans = 46 MATLAB built-in functions: ones (m, n), zeros (m, n), and size () >> E = zeros(2, 3) >> u = ones (1, 3) >> size (A)
Dr. Jie Zou PHY Assignment (cont.) The colon “:” operator Examples >> t = 1: 5 >> t = 1: 0.5: 3 t = >> t = 10: -1: 5>> A (2, :) t =ans = Note that when a colon “:” is used in place of a specific subscript, the colon represents the entire row or column. Now try >> t (2: 4) What do you think the result will be? The linspace function: linspace(x1, x2, n). Try >> linspace (0,1,6) and >> linspace(0,1)
Dr. Jie Zou PHY Mathematical operations and built-in functions Common operators: ^, -, * /, + - Examples: >> 2 * pi >> y = - 4^2 ans =y = Example: Vector-matrix calculations >> A.^2 What is the result? Built-in functions: log, exp, abs, sin, cos, sqrt, sum, min, max, mean, sort, length, etc. The Built-in functions operate directly on vector and matrix quantities. Example: >> log (A) What is the result? For a list of all built-in elementary functions, type >> help elfun For how to use a specific built-in function, for example, sum, type >> help sum
Dr. Jie Zou PHY Graphics 2D plots: plot (x, y); 3D plots: plot3 (x, y, z); Note: x, y, and z are vectors of the same length. Example: The velocity of a falling bungee jumper:. Plot v from t = 0-20 s. Step 1: >> g = 9.81; m = 68.1; cd= 0.25; Step 2: >> t = [0:2:20]’; >> v = sqrt (g*m/cd)*tanh(sqrt(g*cd/m)*t); Step 3: >> plot (t, v) % try plot (t, v, t, v, ‘o’) Step 4: >> title (‘Plot of v versus t’) >> xlabel (‘Values of t’) >> ylabel (‘Values of v’) >> grid Also, check hold on; hold off; and specifiers for colors, symbols, and line types (see Table 2.2 on the hand out). McGraw-Hill Companies
Dr. Jie Zou PHY Plot of v versus t After Step 3: plot After Step 4: title, xlabel, ylabel, and grid