P. 1
Matlab Tutorial

Matlab Tutorial

|Views: 15|Likes:
Published by Umar Munawar

More info:

Published by: Umar Munawar on Aug 07, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Design of Mechanical Systems

MATLAB Tutorial Week 1 : 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

MATLAB stands for “Matrix Laboratory”. Unix. etc. it integrates computation. It is very powerful and easy to use. and Apple Macintosh. visualization and programming all together in an easy-to-use environment and can be used on almost all the platforms: windows. 9/10/2003 3 .What is MATLAB? MATLAB is a tool for doing numerical computations with matrices and vectors. In fact.

Analyze their performance etc… 9/10/2003 4 .What can we do with MATLAB? We can use Matlab to design. simulate and visualize Mechanical Systems.

Scroll down and select ‘Tools’ and then ‘Terminal’ Enter ‘matlab’ at the Unix Prompt 9/10/2003 5 .Running Matlab in DSV Switch on to your local machine by pressing the button on the lower left hand corner of the monitor Login to your account on DSV Right Click.

Command History: double click them to evaluate them.) 9/10/2003 6 . Current Directory browser: shows you where you are.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. Editor/Debugger: pops up when you create M-files (click on “New” button to launch it.

9/10/2003 7 .

Matrix MATLAB works with essentially only one kind of object – a rectangular numerical matrix with possible complex entries. 9/10/2003 8 .

Generated by built-in functions.Entering Matrix Matrices can be Entered manually. Loaded from external disk (using “load” command) 9/10/2003 9 .

9] § Use ‘ .An Example A = [1. 7. 8. 2. ’ to indicate the end of each row § Use comma or space to separate elements of a row 9/10/2003 10 . 3.

+ addition . / 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: .subtraction * multiplication ^ power ‘ transpose \ left division.

Subscripts: Subscripts: the element in row i and column j of A is denoted by A(i.1) + A(1. j). A(1. Example: A = magic(3).3) 9/10/2003 12 .2) + A(1.

Subscript expressions involving colons refer to portions of a matrix: A(1:3 .The Colon Operator ‘:’ The colon ‘:’ is one of MATLAB ’s most important operators. 2) is the first to the third elements of the second column of A. 9/10/2003 13 . It has many formats: i = 3 : -2 : -11 is a row vector containing integers from 3 to –11 with a increment of –2.

(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 .Problem Given a 4 x 5 Matrix A = [ 1 2 3 4 5 56783 91232 2 4 1 7 1].

Solution (i) (ii) B = A(2:3.2:4) B = A(:.1:3) 9/10/2003 15 .

A = rand(3.4) Rand: uniformly distributed random elements.5) Eye(N): N x N identity matrix A = eye(4) 9/10/2003 16 . A = ones(2.3) Ones: all ones. A = zeros(1.Working with Matrices: MATLAB provides four functions that generate basic matrices: Zeros: all zeros.

2. A*2 A/4] Deleting rows and columns: B(:.Concatenation: join small (compatible) matrices to make bigger ones: B = [A A-2. 2) 9/10/2003 17 .2) = [ ] Repeating Matrices: repmat(A.

type: help elfun For a list of more advanced mathematical and matrix functions. type help simulink 18 Functions: . exp. and sin. type help datafun For a list of functions in Simulink Toolbox. For a list of the elementary mathematical functions. type help specfun help elmat For a list of data analysis functions. sqrt. including abs.9/10/2003 MATLAB provides a large number of standard elementary mathematical functions.

MATLAB performs the computation but does not display any result. Example: C = randn(5. C = randn(5. 9/10/2003 19 .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 ‘.s.1) v.1).

Programming with MATLAB: Files that contain code in the MATLAB language are called Mfiles. 9/10/2003 20 . There are two types of M-files: Scripts and Functions. You create M-files using the MATLAB editor. then use them as you would any other MATLAB functions or command.

end bar(r) Add the file into search path and type the statement: magicrank 9/10/2003 21 .Scripts Scripts: a bunch of code grouped together. for n = 1:20 r(n) = rank(magic(n)). doesn’t accept argument or return output.20). Practice: create a file called magicrank.m that calculates the rank of magic squares: r = zeros(1.

m function Sum = add(arg1. 9/10/2003 22 . Example : Matlab File add.Functions: Functions are M-files that can accept input arguments and return output arguments. arg2) : : Sum = arg1 + arg2. The name of the M-file and of the function should be the same.

Flow Control: MATLAB has following flow controls: If statement Switch statement For loops While loops Continue statement Break statement 9/10/2003 23 .

if … elseif … else … end If A > B ‘greater’ elseif A < B ‘less’ elseif A = = B ‘equal’ else error(‘Unexpected situation’) end 9/10/2003 24 .

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 : .25) y3 = sin(x-.y3) 9/10/2003 26 .x.y.y2. y = sin(x).y) y2 = sin(x-. plot(x.01 : 2*pi.5) plot(x.x.

2. 3. 4. Simulink Neural Network Optimization Control Systems Virtual Reality …etc 9/10/2003 27 .ToolBoxes MATLAB has toolboxes for specific programming applications: 1. 5.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->