Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.

Similar presentations


Presentation on theme: "A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University."— Presentation transcript:

1 A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University

2 What is Matlab MATLAB® is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. 1 1 Matlab Help

3 Matlab as a calculator Matlab incorporates simple arithmetic operators + - * / ^ Matlab includes basic functions such as: sin(), log(), log10(), exp(), cosh() Matlab also includes predefined constants: pi, e Values may be assigned to variables: >>U=6; (semicolon suppresses screen output) >>V=5; (Matlab is case- sensitive) >>U*V ans= 30(If no name is specified, “ans” is used by default) >>W=log(U);

4 Some Basic Commands Who:Lists all variables in workspace Whos:Provides more detailed information on variables in workspace Clear:Clears variables and functions from memory… Clear a, bClears only a and b cd:Change directory ls:Lists files in current directory load filename:Loads file into memory

5 Matrices You can enter matrices into MATLAB in several different ways: Enter an explicit list of elements. Load matrices from external data files. Generate matrices using built-in functions. Create matrices with your own functions in M-files. An NxM matrix has N rows and M columns Rows may be separated by “enter” >>A=[1 2 3 4] gives A = 1 2 3 4 Rows may also be separated by semi- colons: >>A=[1 2;3 4]; Columns are separated by a space or comma >>A= [1,2;3,4]

6 Matrix elements The element located in the i th row and j th column of matrix A can be accessed as: >>A(i,j) The value of an element can easily be changed >>A(2,1)=5; This changes the previous matrix to A= 1 2 5 4 Formulas can be used >>A(2,2)=cos(pi)+sin(pi) A = 1.0000 2.0000 5.0000 -1.0000 The elements of an entire row or column can be accessed or changed: >>A(:,2)=6; This changes A to: A= 1 6 5 6

7 Matrix elements (Contd) A column can be deleted using: >>A(:,2)=[ ] (This erases the 2 nd column) A row can be deleted in a similar way: >>A(4,:)=[ ] The transpose of a matrix can be obtained using ‘: If A is 1 2 5 4 Then, A’ is 1 5 2 4 Special arrays: Zeros: creates a matrix of all zeros; >>zeros(2,2) yields 0 Ones: creates an array of all ones; >>ones(2,1) yields 1 Eye: creates the identity matrix; >>eye(2,2) yields 1 0 0 1

8 For-Loops For loops allow you to repeat statements a certain number of times, and are especially useful for assigning values to array elements. Each for loop ends with an “end” statement For i=1:N For j=1:M A(i,j)=sin(x(i))+b*cos(pi*y(j)) end

9 Matrix operations If A and B are arrays of the same size: C=A+B; generates an array of the same size as A and B whose elements are the sum of the elements of A and B Alternatively, use For i=1:N For j=1:M C(i,j)=A(i,j)+B(i,j); end Inv(A) generates the matrix inverse of A, i.e., inv(A)*A yields the identity matrix C=A*B; product of matrices A and B

10 If-statement If (i==2) A(i)=1; end If ((i>2) & (i<=6)) A(i)=3*i; else A(i)=0; end If ((i~=2) & (i<5))Note: ~= means “not equal to” A(i)=3 elseif (i>6) A(i)=1; end

11 While statement While repeats a statement until the expression between parentheses is no longer true. i=1 While(i<10) i=i+1; End At the end of this while loop, i will be equal to 10

12 Basic Plotting Suppose x, U and V are 1-D arrays plot(x,U) plots U vs. x plot(x,U,’o’) plots U vs. x and uses an ‘o’ marker xlabel(‘this is my x title’) ylabel(‘this is my y title’) title(‘This is my title’) plot(x,U,’o’,x,V,’+’) Plots U vs x and V vs x on same plot legend(‘this is my U legend’,’this is my V legend’) Figure(1) opens the first figure window

13 Basic plotting (cont’d) You can also use the GUI to input titles, legends, etc.

14 More plotting If x, y, and T are NxM arrays where x and y arrays hold the x and y coordinate info: contour(x,y,T) generates a contour plot of T in the x-y plane colorbar inserts a colorbar showing the scale used for the contour plot If x, y, U and V are NxM arrays and U and V represent the x and y components of velocity: quiver(x,y,U,V) generates a velocity vector plot in the x-y plane

15 A few more tricks Suppose x and A are NxM arrays and you want to plot the data in the k’th row plot(x(k,:),A(k,:)) the colon indicates that you want to go over the entire j-index range If you wish to have 4 plots on the same page, use the “subplot” command If you save a figure in a.fig format, you can open it again in Matlab at a later time and edit it. To copy and paste Matlab plot in Word, either use “edit – copy figure” in Matlab, then paste in document or save figure in Matlab as.jpg file.


Download ppt "A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University."

Similar presentations


Ads by Google