Design of Mechanical Systems: MATLAB Tutorial Week 1: Introduction
Design of Mechanical Systems: MATLAB Tutorial Week 1: Introduction
Design of Mechanical Systems: MATLAB Tutorial Week 1: Introduction
9/10/2003
Introduction
Useful links: http://www.mathworks.com/products/edu cation/teaching_mech.shtml This link is the official Mathworks page and gives useful information regarding Modeling of Mechanical Systems using MATLAB and Simulink. Book : Mastering MATLAB 6 By: D. Hanselman & B Littlefield
9/10/2003 2
What is MATLAB?
MATLAB is a tool for doing numerical computations with matrices and vectors. It is very powerful and easy to use. In fact, it integrates computation, visualization and programming all together in an easy-to-use environment and can be used on almost all the platforms: windows, Unix, and Apple Macintosh, etc. MATLAB stands for Matrix Laboratory.
9/10/2003 3
9/10/2003
9/10/2003
Getting started:
MATLAB Desktop: Launch Pad: displays all the tools and applications associated with MATLAB; Workspace: consists of the variables you create during a MATLAB session; Command History: double click them to evaluate them; Current Directory browser: shows you where you are. Editor/Debugger: pops up when you create M-files (click on New button to launch it.)
9/10/2003 6
9/10/2003
Matrix
MATLAB works with essentially only one kind of object a rectangular numerical matrix with possible complex entries.
9/10/2003
Entering Matrix
Matrices can be Entered manually; Generated by built-in functions; Loaded from external disk (using load command)
9/10/2003
An Example
A = [1, 2, 3; 7, 8, 9] Use ; to indicate the end of each row Use comma or space to separate elements of a row
9/10/2003
10
+ addition - subtraction * multiplication ^ power transpose \ left division, / division x = A \ b is the solution of A * x = b x = b / A is the solution of x * A = b inv inverse of a matrix
9/10/2003 11
Matrix operations:
Subscripts:
Subscripts: the element in row i and column j of A is denoted by A(i, j). Example: A = magic(3); A(1,1) + A(1,2) + A(1,3)
9/10/2003
12
9/10/2003
13
Problem
Given a 4 x 5 Matrix A = [ 1 2 3 4 5 56783 91232 2 4 1 7 1]; (i) Write a command to extract the Matrix shown inside the Box (ii) Write a command to extract the first 3 columns of the matrix A
9/10/2003
14
Solution
(i) (ii)
B = A(2:3,2:4) B = A(:,1:3)
9/10/2003
15
Concatenation: join small (compatible) matrices to make bigger ones: B = [A A-2; A*2 A/4] Deleting rows and columns: B(:,2) = [ ] Repeating Matrices: repmat(A, 2, 2)
9/10/2003 17
9/10/2003
MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, and sin. For a list of the elementary mathematical functions, type: help elfun For a list of more advanced mathematical and matrix functions, type help specfun help elmat For a list of data analysis functions, type help datafun For a list of functions in Simulink Toolbox, type help simulink 18
Functions:
Suppressing Output:
If you simply type a statement and press Enter, MATLAB automatically displays the results on screen. If you end the line with a semicolon ;, MATLAB performs the computation but does not display any result. Example: C = randn(5,1) v.s. C = randn(5,1);
9/10/2003 19
9/10/2003
20
Scripts
Scripts: a bunch of code grouped together; doesnt accept argument or return output. Practice: create a file called magicrank.m that calculates the rank of magic squares: r = zeros(1,20); for n = 1:20 r(n) = rank(magic(n)); end bar(r) Add the file into search path and type the statement: magicrank
9/10/2003 21
Functions:
Functions are M-files that can accept input arguments and return output arguments. The name of the M-file and of the function should be the same. Example : Matlab File add.m
function Sum = add(arg1, arg2)
: :
Sum = arg1 + arg2;
9/10/2003
22
Flow Control:
MATLAB has following flow controls: If statement Switch statement For loops While loops Continue statement Break statement
9/10/2003 23
for end
for i = 1:m for j = 1:n H(i,j) = 1/(i+j) end end
9/10/2003
25
Graphics: plot
x = 0 : .01 : 2*pi; y = sin(x); plot(x,y) y2 = sin(x-.25) y3 = sin(x-.5) plot(x,y,x,y2,x,y3)
9/10/2003 26
ToolBoxes
MATLAB has toolboxes for specific programming applications:
1. 2. 3. 4. 5.
9/10/2003
27