Upcoming SlideShare
×

# Matlab Basic Tutorial

18,646 views

Published on

This is matlab tutorial

Published in: Education, Technology
19 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• MATLAB For Dummies --- http://amzn.to/1T2L7tq

Are you sure you want to  Yes  No
• MATLAB and Simulink Student Suite R2016a --- http://amzn.to/1R99vXP

Are you sure you want to  Yes  No
• Matlab: A Practical Introduction to Programming and Problem Solving --- http://amzn.to/1TYfLW3

Are you sure you want to  Yes  No
• good work

Are you sure you want to  Yes  No
• sip

Are you sure you want to  Yes  No
Views
Total views
18,646
On SlideShare
0
From Embeds
0
Number of Embeds
156
Actions
Shares
0
1,974
5
Likes
19
Embeds 0
No embeds

No notes for slide
• ### Matlab Basic Tutorial

1. 1. By Muhammad Rizwan I060388 For Section A&B C&D
2. 2. <ul><li>INTRODUCTION TO MATLAB </li></ul>
3. 3. MATLAB <ul><li>It stands for MATrix LABoratory </li></ul><ul><li>It is developed by The Mathworks, Inc . (http://www.mathworks.com) </li></ul><ul><li>It is widely used in mathematical computation </li></ul><ul><li>Numerical Computation </li></ul><ul><li>Linear Algebra </li></ul><ul><li>Matrix Computations </li></ul><ul><li>Simple Calculations </li></ul><ul><li>Scripting </li></ul><ul><li>Graphing </li></ul>
4. 4. Typical Uses <ul><li>• Math and computation </li></ul><ul><li>• Algorithm development </li></ul><ul><li>• Modeling, simulation, and prototyping </li></ul><ul><li>• Data analysis, exploration, and visualization </li></ul><ul><li>• Scientific and engineering graphics </li></ul><ul><li>• Application development, including graphical user interface building </li></ul>
5. 7. Starting MATLAB <ul><li>To start MATLAB, double-click the MATLAB shortcut </li></ul><ul><li>or </li></ul><ul><li>Click on “Start” and then on “Run” and write ‘matlab’ and then press OK </li></ul><ul><li>It will take some seconds and then matlab starts </li></ul>
6. 8. Quitting MATLAB <ul><li>To end your MATLAB session, select Exit MATLAB from the File menu. </li></ul><ul><li>or type quit or exit in the Command Window. </li></ul><ul><li>There are different type of windows </li></ul><ul><li>Command Window </li></ul><ul><li>Editor Window (M-File) </li></ul><ul><li>Figure Window </li></ul><ul><li>GUI (Graphical User Interface) </li></ul>
7. 10. Operations Performed <ul><li>A+B </li></ul><ul><li>A-B </li></ul><ul><li>A*B </li></ul><ul><li>C’ </li></ul><ul><li>B=C </li></ul><ul><li>A ,B ,C are variables </li></ul>
8. 11. MATLAB Variables <ul><li>Scalar </li></ul><ul><li>A = 4 </li></ul><ul><li>Vector </li></ul><ul><li>A = [2 3 4] </li></ul><ul><li>Matrices </li></ul><ul><li>A= </li></ul>
9. 12. Numbers & Formats <ul><li>Matlab recognizes several dierent kinds of numbers </li></ul>
10. 13. Format Set display format for output
11. 14. Format <ul><li>Syntax </li></ul><ul><li>format type </li></ul><ul><li>format('type') </li></ul><ul><li>Both types can be used for same purpose </li></ul><ul><li>To see current format write get(0,'format') </li></ul>
12. 15. Some Basics <ul><li>Can act as a calculator </li></ul><ul><li>x = 3-2^4 </li></ul><ul><li>Variable names </li></ul><ul><li>Legal names consist of any combination of letters and digits, starting with a letter. </li></ul><ul><li>These are allowable: </li></ul><ul><li>NetCost, Left2Pay, x3, X3, z25c5 </li></ul><ul><li>These are not allowable: </li></ul><ul><li>Net-Cost, 2pay, %x, @sign </li></ul>
13. 16. Input and Output (Contd) <ul><li>V=[1 1/2 1/6 1/12] </li></ul><ul><li>[1 3 5] row vector </li></ul><ul><li>[1 3 5]’ </li></ul><ul><li>[1;3;5] column vector </li></ul><ul><li>[1;3;5]’ </li></ul><ul><li>[1 3 5;7 8 4] Matrix </li></ul>
14. 17. Input and Output <ul><li>size(A) //return size of a matrix </li></ul><ul><li>Seeing a matrix </li></ul><ul><li>A(2,3) //returns element at 2,3 position </li></ul><ul><li>A(2,:) // returns 2 nd row </li></ul><ul><li>A(:,3) //returns 3 rd coloumn </li></ul>
15. 18. Matrix Operations <ul><li>A+B </li></ul><ul><li>A-B </li></ul><ul><li>A*B </li></ul><ul><li>b*A // b is a vector either row or column vector </li></ul><ul><li>Transpose of a matrix </li></ul><ul><li>C’ </li></ul><ul><li>B=C’ </li></ul>
16. 19. Matrix Operations (Contd) <ul><li>Augmented matrix </li></ul><ul><li>[B b] </li></ul><ul><li>[A;C] </li></ul><ul><li>[C A;A C] </li></ul><ul><li>Diagonal matrix </li></ul><ul><li>D=diag([1 2 3]) // create diagonal matrix </li></ul><ul><li>Diag(D) // return diagonal </li></ul><ul><li>diag(diag(D)) what it can do? </li></ul>
17. 20. Matrix Powers <ul><li>Product of A with itself k </li></ul><ul><li>If A is square matrix and k is positive integer </li></ul><ul><li>>>A^k </li></ul><ul><li>Displaying an Identity Matrix having the same size as A </li></ul><ul><li>>>A^0 </li></ul><ul><li>Identity Matrix </li></ul><ul><li>>>eye(3) </li></ul><ul><li>>>t=10; eye(t) </li></ul><ul><li>>>eye(size(A)) display an Identity Matrix the same size as A </li></ul>
18. 21. Some Basics (Contd) <ul><li>shortcut for producing row vectors: </li></ul><ul><li>1:4 </li></ul><ul><li>1:3:20 </li></ul><ul><li>Plotting a graph </li></ul><ul><li>x = linspace (0,1,10); // from 0 to 1 divide in 10 parts </li></ul><ul><li>y = sin(3*pi*x); </li></ul><ul><li>plot(x,y) </li></ul>
19. 22. Script M-Files <ul><li>typing at the Matlab becomes tedious, when </li></ul><ul><ul><li>number of commands increases, </li></ul></ul><ul><ul><li>want to change the value of one or more variables, </li></ul></ul><ul><ul><li>re evaluate a number of commands, </li></ul></ul><ul><ul><li>typing at the Matlab becomes tedious. </li></ul></ul>
20. 23. Script M-Files <ul><li>Script File: Group of Matlab commands placed in a text file with a text editor. </li></ul><ul><li>Matlab can open and execute the commands exactly as if they were entered at the Matlab prompt. </li></ul><ul><li>The term “script” indicates that Matlab reads from the “script” found in the file. Also called “M-files,” as the filenames must end with the extension ‘.m’, e.g. example1.m. </li></ul>
21. 24. Script M-Files <ul><li>choosing New from the File menu in the Matlab Command window and selecting M-file . </li></ul><ul><li>Create the file named add.m in your present working directory using a text editor: </li></ul>
23. 26. add.m <ul><li>Save as add.m </li></ul><ul><li>To execute the script M-file, simply type the name of the script file add at the Matlab prompt: </li></ul><ul><li>• % allows a comment to be added </li></ul><ul><li>Open your script file </li></ul><ul><li>Why result of d is not in the screen ? </li></ul><ul><li>c Compute and display add </li></ul>
24. 27. Matlab functions useful in M-files: <ul><li>Take input from user and show result </li></ul><ul><li>disp(ans) Display results without identifying variable names </li></ul><ul><li>input(’prompt’) Prompt user with text in quotes, accept input until “Enter” is typed </li></ul>
25. 28. Matlab functions useful in M-files: <ul><li>The input command is used for user input of data in a script and it is used in the form of an assignment statement. For example: </li></ul><ul><li>a = input(‘Enter quadratic coefficient a: ‘); </li></ul><ul><li>disp('Value of first quadratic root: ') </li></ul>
26. 29. M-Files <ul><li>M-files can be either </li></ul><ul><ul><li>scripts or </li></ul></ul><ul><ul><li>functions. </li></ul></ul><ul><li>Scripts are simply files containing a sequence of MATLAB statements. </li></ul><ul><li>Functions make use of their own local variables and accept input arguments. </li></ul>
27. 30. Functions Format <ul><li>function [x, y] = myfun(a, b, c) % Function definition </li></ul><ul><li>a , b , c are input parameters and x , y are output parameters </li></ul>
28. 31. Functions <ul><li>function [output_parameter_list] = function_name(input_parameter_list) </li></ul><ul><li>The first word must always be ``function''. </li></ul><ul><li>Following that, the (optional) output parameters are enclosed in square brackets [ ]. </li></ul><ul><li>If the function has no output_parameter_list the square brackets and the equal sign are also omitted. </li></ul><ul><li>The function_name is a character string that will be used to call the function. </li></ul><ul><li>The function_name must also be the same as the file name (without the ``.m'') in which the function is stored. </li></ul><ul><li>In other words the MATLAB function, ``foo'', must be stored in the file, ``foo.m''. </li></ul>
29. 32. <ul><li>addtwo.m // without output parameters </li></ul><ul><li>function addtwo(x,y) </li></ul><ul><li>% addtwo(x,y) Adds two numbers, vectors, whatever, and </li></ul><ul><li>% print the result = x + y </li></ul><ul><li>x+y </li></ul>
30. 33. Elementary Math Functions <ul><li>>> abs(x) % absolute value of x </li></ul><ul><li>>> exp(x) % e to the x-th power </li></ul><ul><li>>> fix(x) % rounds x to integer towards 0 </li></ul><ul><li>>> log10(x) % common logarithm of x to the base 10 </li></ul><ul><li>>> rem(x,y) % remainder of x/y </li></ul><ul><li>>> sqrt(x) % square root of x </li></ul><ul><li>>> sin(x) % sine of x; x in radians </li></ul><ul><li>>> acoth(x) % inversion hyperbolic cotangent of x </li></ul><ul><li>>> help elfun % get a list of all available elementary functions </li></ul>
31. 34. Flow Control <ul><li>If </li></ul><ul><li>Conditionally execute statements </li></ul><ul><li>Syntax: </li></ul><ul><li>if expression </li></ul><ul><li>statements </li></ul><ul><li>end </li></ul><ul><li>expression is a MATLAB expression, usually consisting of variables or smaller expressions joined by relational operators (e.g., count < limit), or logical functions (e.g., isreal(A)). </li></ul><ul><li>statements is one or more MATLAB statements to be executed only if the expression is true or nonzero. </li></ul>
32. 35. Logical comparisons
33. 36. <ul><li>MATLAB supports these variants of the ``if'' construct </li></ul><ul><li>if ... end </li></ul><ul><li>if ... else ... end </li></ul><ul><li>if ... elseif ... else ... end </li></ul>
34. 37. if ... end <ul><li>>> d = b^2 - 4*a*c; </li></ul><ul><li>>> if d<0 </li></ul><ul><li>>> disp('warning: discriminant is </li></ul><ul><li>negative, roots are imaginary'); </li></ul><ul><li>>> end </li></ul>
35. 38. if ... else ... end <ul><li>>> d = b^2 - 4*a*c; </li></ul><ul><li>>> if d<0 </li></ul><ul><li>>> disp('warning: discriminant is </li></ul><ul><li>negative, roots are imaginary'); </li></ul><ul><li>>> else </li></ul><ul><li>>> disp('OK: roots are real, but may be </li></ul><ul><li>repeated'); </li></ul><ul><li>>> end </li></ul>
36. 39. if ... elseif ... else ... end <ul><li>>> d = b^2 - 4*a*c; </li></ul><ul><li>>> if d<0 </li></ul><ul><li>>> disp('warning: discriminant is negative, </li></ul><ul><li>roots are imaginary'); </li></ul><ul><li>>> elseif d==0 </li></ul><ul><li>>> disp('discriminant is zero, roots are </li></ul><ul><li>repeated'); </li></ul><ul><li>>> else </li></ul><ul><li>>> disp('OK: roots are real and distinct'); </li></ul><ul><li>>> end </li></ul>
37. 40. for <ul><li>for variable = expression </li></ul><ul><li>statement </li></ul><ul><li>... </li></ul><ul><li>statement </li></ul><ul><li>End </li></ul><ul><li>Example </li></ul><ul><li>for i = 1:m </li></ul><ul><li>for j = 1:n </li></ul><ul><li>H(i,j) = 1/(i+j); </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
38. 41. Note <ul><li>no semicolon is needed to suppress output at the end of lines containing if, else, elseif or endif </li></ul><ul><li>elseif has no space between ``else'' and ``if'' </li></ul><ul><li>the end statement is required </li></ul><ul><li>the ``is equal to'' operator has two equals signs </li></ul><ul><li>Indentation of ``if'' blocks is not required, but considered good style. </li></ul>
39. 42. while constructs <ul><li>Repeat statements an indefinite number of times </li></ul><ul><li>Syntax: </li></ul><ul><li>while expression </li></ul><ul><li>do something </li></ul><ul><li>end </li></ul><ul><li>where ``expression'' is a logical expression. The ``do something'' block of code is repeated until the expression in the while statement becomes false. </li></ul>
40. 43. Example while <ul><li>i = 2; </li></ul><ul><li>while i<8 </li></ul><ul><li>i = i + 2 </li></ul><ul><li>end </li></ul><ul><li>Output: </li></ul><ul><li>i = 4 </li></ul><ul><li>i = 6 </li></ul><ul><li>i = 8 </li></ul>
41. 44. Graphics <ul><li>MATLAB is an interactive environment in which you can program as well as visualize your computations. </li></ul><ul><li>It includes a set of high-level graphical functions for: Line plots (plot, plot3, polar) </li></ul><ul><li>Bar graphs (bar, barh, bar3, bar3h, hist, rose, pie, pie3) Surface plots (surf, surfc) </li></ul><ul><li>Mesh plots (mesh, meshc, meshgrid) </li></ul><ul><li>Contour plots (contour, contourc, contourf) </li></ul><ul><li>Animation (moviein, movie) </li></ul>
42. 45. Line Plots <ul><li>Note: </li></ul><ul><li>You can also set properties means line size, color, text show ,grid , axis etc. </li></ul><ul><li>For this I will place some reference code observe it </li></ul><ul><li>>> t = 0:pi/100:2*pi; </li></ul><ul><li>>> y = sin(t); >> </li></ul><ul><li>plot(t,y) </li></ul>
43. 46. Bar Graphs <ul><li>>> x = magic(3) // creat 3x3 matrix </li></ul><ul><li>>>x = 8 1 6 3 5 7 4 9 2 </li></ul><ul><li>>> bar(x) </li></ul><ul><li>>> grid </li></ul>
44. 47. Surface Plots <ul><li>>> Z = peaks; // already implemented function </li></ul><ul><li>>> surf(Z) </li></ul>
45. 48. Explore it yourself
46. 49. See Mat lab libraries in C:Program FilesMATLABR2006b oolbox See Help Menu
47. 50. Thanks for your patience
48. 51. <ul><li>Write a script file which have any mathematical function and write an other script file in which you use this function and apply Newton-Raphson method on it , also draw it. </li></ul>
49. 52. <ul><li>Write a script file which have any mathematical function and write an other script file in which you use this function and apply Regula-falsi method on it , also draw it. </li></ul>