Presentation is loading. Please wait.

Presentation is loading. Please wait.

ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.

Similar presentations


Presentation on theme: "ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design."— Presentation transcript:

1 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design

2 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Learning Objectives Learn how to use Matlab - layout of windows, interpreter, on-line help. Learn the types of programming methods. Learn Matlab language syntax: –variables, –scalars, vectors, matrices –if, for, while statements Learn how to plot graphs.

3 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory MATLAB MATLAB = MATrix LABoratory. Interactive system and programming language for technical computation. Interpreter - like Basic, not compiler like C, C++, Fortran. Toolboxes - prepared sets of integrated tools for work in a specific area (Controls, PDE’s). We will use it for simple 3D modeling, parametrics, and graphics.

4 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory User Interface 3 Windows: Command Window - enter Matlab commands, executed immediately by interpreter, answers printed in Window. Graphics Window - displays plots and graphs. Edit Window - prepare scripts and functions. Scripts - collection of commands that the interpreter executes. Function - collection of commands enveloped in a function call.

5 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory User Interface Command Window Edit Window Graphics Window

6 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Programming 3 Methods: Type directly into Command Window: try new things, experiment, learn Matlab. Write scripts using Edit Window: save commands to execute at one time. Write functions using Edit Window: save commands encapsulated into reusable fragments (functions). Example: math functions like sin(), cos(), tan()...

7 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Matlab Language Some similarities to C and Basic. Variables: Scalars, Vectors, Matrices Start with a letter Can contain letters, digits, and underscore Any length, but must be unique within first 19 characters Case-sensitive Examples: engine, Engine, degree2radian, cam_shaft Scalars: x = 2.0; deg2rad = pi/180; Vectors: x = [3.5]; y = [1.5, 3.1]; Matrices: z = [-1, 0, 0; 1, 1, 0; 1, -1, 0; 0, 0, 2];

8 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Language (2) xy = [x y] yields [3.5, 1.5, 3.1] Accessing vectors and matrices x(1) yields 3.5 z(3,2) yields -1 result = y(2) + z(3,2) yields 2.1 Size of Matrix: size(z) => [4 3] (4 rows, 3 columns) => means ‘yields’ What is ‘;’? It instructs the interpreter to NOT print the result of the computation.

9 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Language (3) Colon Operator: ‘:’ extracts row or column from matrix. A = z(:,1) extracts first column from z b = z(3:4, 1:2) extracts bottom left 2x2 submatrix. Transpose Operator: ‘ Row vector is not the same as column vector. B = a’ a = 0:4; => a = [0 1 2 3 4]

10 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Language (4) Special Values and Matrices pii,j = sqrt(-1) Inf = infinity NaN = not a number - occurs when dividing by 0 eps = machine epsilon A = zeros(3) B = ones(3) I = eye(4)

11 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Language (5) Operations: Scalars Matrices Addition++ Subtraction-- Multiplication** element by element.* Division/./ (element by element) Exponentiation ^.^

12 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Language - Logic and Control If Statement: if logical expression statements … end if g < 50 cnt = cnt + 1; fprintf (1, ‘Count is now %d\n’, cnt); if b > g b = 0; end Else and elseif: if temp > 100 disp(‘Really hot!’); elseif temp > 65 disp(‘Temp OK’); else disp (‘Too cold’); end

13 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Relational & Logical Operators Relational Operators < less than <= less than or equal > greater than >= greater than or equal == equal ~= not equal Logical Operators ~ not & and | or

14 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Looping - For For Loops for index = expression statements end expression is initial: increment: limit for theta = 0.0: 2.0: 360.0 rth = deg2rad * theta; ctheta = cos (rth); stheta = sin (rth); end Replace ‘for’ loop: theta = 0.0: 2.0: 360.0 rth = theta * deg2rad; ctheta = cos (rth); stheta = sin (rth); Replace ‘for’ loop: theta = 0.0: 2.0: 360.0 rth = theta * deg2rad; ctheta = cos (rth); stheta = sin (rth);

15 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Looping - While While Loops Loop continues executing until “expression” evaluates to false. While expression statements … end found_yet = 0; k = 0; while k < 10 & (~found_yet) k = k + 1; end

16 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Plotting 2D Plots: plot (x,y, ‘abc’) x, y = vectors of #’s to be plotted ‘abc’ = up to 3 characters that control line type, color, and how points are plotted. plot (x,y), title(‘Lab Exp 1’), xlabel(‘Trial’), … ylabel(‘Distance (ft)’), grid; 3D Plots: plot3 (x,y,z, ‘abc’) plot3 (x, y, z, ‘b+-’); plots x, y, z in blue (‘b’), with ‘+’ at the given data points, and using a solid line (‘-’). Rotate3D - command to allow you to rotate the image in the graphics window.

17 ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Matlab Summary How to use Matlab - layout of windows, interpreter, on-line help. Types of programming methods. Matlab language syntax –variables, –scalars, vectors, matrices –if, for, while statements Plotting


Download ppt "ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design."

Similar presentations


Ads by Google