Download presentation

Presentation is loading. Please wait.

1
Dr. Jie Zou PHY 33201 Welcome to PHY 3320 Computational Methods in Physics and Engineering

2
Dr. Jie Zou PHY 33202 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.

3
Dr. Jie Zou PHY 33203 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 >> 55 - 16 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?

4
Dr. Jie Zou PHY 33204 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.

5
Dr. Jie Zou PHY 33205 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 = [ 1 2 3 ] >> b = [ 2; 4; 6] a = or>> b = [2 4 6]’ 1 2 3b = 2 4 6 Also note that this current assignment of a Overrides the previous assignment of a = 4.

6
Dr. Jie Zou PHY 33206 Assignment (cont.) Example: Create a matrix >> A = [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 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)

7
Dr. Jie Zou PHY 33207 Assignment (cont.) The colon “:” operator Examples >> t = 1: 5 >> t = 1: 0.5: 3 t = 1 2 3 4 5 1.0 1.5 2.0 2.5 3.0 >> t = 10: -1: 5>> A (2, :) t =ans = 10 9 8 7 6 54 5 6 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)

8
Dr. Jie Zou PHY 33208 Mathematical operations and built-in functions Common operators: ^, -, * /, + - Examples: >> 2 * pi >> y = - 4^2 ans =y = 6.2832 -16 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

9
Dr. Jie Zou PHY 33209 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

10
Dr. Jie Zou PHY 332010 Plot of v versus t After Step 3: plot After Step 4: title, xlabel, ylabel, and grid

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google