Matlab Basic Tutorial

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

3 Favorites

Matlab Basic Tutorial - Presentation Transcript

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

+ Muhammad RizwanMuhammad Rizwan Nominate

custom

2638 views, 3 favs, 0 embeds more stats

This is matlab tutorial

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 2638
    • 2638 on SlideShare
    • 0 from embeds
  • Comments 1
  • Favorites 3
  • Downloads 153
Most viewed embeds

more

All embeds

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories