Matlab Introduction & Graphics Presentation

Embed Size (px)

Citation preview

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    1/74

    MATLAB Basics &MATLAB Basics &Graph icsGraph ics

    Dr. R. UppaluriDr. R. UppaluriDepa r t m en t o f Chem ica lDepa r t m en t o f Ch em ica l

    Eng ineer ing ,Eng ineer ing ,

    I n d i an I n st i t u t e o f Tech n o lo g y ,I n d i an I n st i t u t e o f Tech n o lo g y ,

    G u w a h a t iG u w a h a t i ..Emai l :Emai l : r a m g o p a l ur a m g o p a l u @@i i t gi i t g ..e rne te rne t .i n.i n

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    2/74

    Conten tsCon ten ts1 .1 . Qu ick Over v iewQu ick Over v iew

    2 .2 . Dat a Typ es & Var iab lesDat a Typ es & Var iab les

    3 .3 . Ope ra to rsOpera to rs

    4 .4 . Flow Con t r o lFlow Con t r o l

    5 .5 . Func t i onsFunc t i ons

    6 .6 . I n p u t / Ou t pu tI n pu t / Ou t pu t

    7 .7 . A r r ay Man ipu lat i onAr r ay Man ipu lat i on

    8 .8 . W r i t i n g & Ru n n i n g Pr o g r a m sW r i t i n g & Ru n n in g Pr o g r a m s

    9 .9 . P lo t t i ngP lo t t i ng

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    3/74

    1 . Qu ick Ov er v iew1 . Qu ick Ov er v iewMATLAB is termed as MATRIX LABORATORY

    High Level Computer Language

    Scientific Computing

    Data Visualization

    Interactive Programming EnvironmentQuick Testing and Debugging

    Allows one to focus more on principles of programming ratherthan on programming itself

    Programs can be developed in a Shorter Time than equivalentFORTRAN or C Programs

    Negative: No Stand-alone applications i.e., Programs have to be

    run on MATLAB environment only.

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    4/74

    1 . Qu ick Ov er v iew1 . Qu ick Ov er v iewAdvantages

    Large number of functions involving numerical libraries

    LINPACK, ODE Solver, PDE Solver, Optimization routines etc.

    Many common tasks accomplished in single function call

    Solution of Simultaneous Equations

    Extensive Graphics Support

    Plotting results with few statements

    All Numerical Objects treated as Double-Precision Arrays

    No need to declare data types and carry out type conversion

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    5/74

    1 . Qu ick Ov er v iew1 . Qu ick Ov er v iewIllustration of Advantages Gauss Elimination Code in

    Fortran 90Use prec_mod conveys toload module prec_m module.

    DP used for defining floatingpoint numbers

    Array a(k, k+1:n) a feature notavailable in previous versions of

    fortran

    Code is lengthy

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    6/74

    1 . Qu ick Ov er v iew1 . Qu ick Ov er v iewIllustration of Advantages

    Equivalent MATLAB function(No Subroutines in MATLAB)

    No Definitions for floating

    numbers Arrays dynamically defined

    Built in MATLAB

    CODE

    Code is shorter and simpler than equivalent FORTRAN code

    http://illust1.m/http://illust1.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    7/74

    1 . Qu ick Ov er v iew1 . Qu ick Ov er v iewSolution of simultaneous equations

    code

    Unlike FORTRAN & C Compilers, MATLAB can be operatedin the interactive mode through its command window (>>)

    http://illust2.m/http://illust2.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    8/74

    1 . Qu ick Ov er v iew1 . Qu ick Ov er v iewCommand Window Each command executed upon entry

    In this mode, MATLAB behaves as an electronic calculator

    Symbol >> is MATLABs prompt for input

    Percent Sign (%) marks the beginning of comment.

    Semicolon (;)

    Separates the rows of a matrix

    Supresses printout of intermediate results

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    9/74

    1 . Qu ick Ov er v iew1 . Qu ick Ov er v iewFunctions/Programs execution at Command Window Create functions/programs with .m extension

    File name identical to name of the function

    For example, if Gauss Elimination routine is saved as gauss.m, then thefollowing steps can be used in the command window:

    2 D T &2 D t T &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    10/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesData Types Common data types (All regarded by MATLAB as arrays)

    double (Numerical objects)

    Char (Strings)

    Logical (Logical Statements)

    Numerical Objects

    Class Double

    Scalar is treated as 1 x 1 array

    StringsSequence of characters

    Logical

    True

    False

    2 D T &2 D t T &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    11/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesFunction_handle: Important Data class Consists of the character @ followed by name of function

    E.g., @sin

    Function handle uses as input argument in function calls

    Say we have MATLAB function plot(func, x1, x2) that plotsspecified function func from x1 and x2. Here func is a function of avariable x.

    Function call to plot sinx from 0 to pi would be plot(@sin, 0, pi)

    2 D t T &2 D t T &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    12/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesVariables Case sensitive

    Xstart and xstart are different variables

    Length of variable unlimited

    First N characters are significantFor this purpose, use namelengthmax to know what is N

    2 D t T &2 D t T &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    13/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesVariables Variables are local in purpose.

    If Variables are shared between local function and calling program,then use global statement.

    Recommended practice: Capital letters for Global variables such as

    Global X Y

    2 D t T &2 D t T &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    14/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesBuilt-in constants & Special Variables

    2 D t T &2 Dat a T p es &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    15/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesFew examples for Special constants

    2 D t T &2 Dat a Ty p es &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    16/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesArrays: Can be created in several ways

    Can be entered in singleline to separate rows with

    semi-colon

    Can be entered in threelines as 3 x 3 matrix

    2 Dat a Ty p es &2 Dat a Ty p es &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    17/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesBe careful with the semicolon ! MATLAB differentiates between row and column vectors with the

    semicolon

    MISPLACED SEMICOLON IMPLIES INPUT ERROR DURINGCODING.

    Examples

    2 Dat a Ty p es &2 Dat a Ty p es &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    18/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesSingle quote Operator () For Matrix transpose

    Therefore, one has to be careful with evenSingle Quote Operator while coding

    2 Dat a Ty p es &2 Dat a Ty p es &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    19/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesAccess to matrix elements

    2 Dat a Ty p es &2 Dat a Ty p es &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    20/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesCells A Cell is a sequence of arbitrary objects

    Cells created by enclosing objects in { }

    For displaying contents, use celldisp command

    2 Dat a Ty p es &2 Dat a Ty p es &

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    21/74

    2 . Dat a Ty p es &2 . Dat a Ty p es &

    Var iab lesVar iab lesStrings A string is a sequence of characters

    Treated as Character Array in MATLABConcatenated (adding) using strcat command and extracted using colon(:) as shown in this example

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    22/74

    3 . Oper at o r s3 . Oper at o r sArthimetic Operators

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    23/74

    3 . Oper at o r s3 . Oper at o r sArthimetic Operators: Matrices

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    24/74

    3 . Oper at o r s3 . Oper at o r sDivision Operators

    Scalars Matrices

    Right division means a/b

    Left division means b/a

    Right division: Solution of X*A = B

    Left division: Solution of A*X = B

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    25/74

    3 . Oper at o r s3 . Oper at o r sElemental Division

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    26/74

    3 . Oper at o r s3 . Oper at o r sComparison Operators

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    27/74

    3 . Oper at o r s3 . Oper at o r sComparison Operators: Example

    Logical Operators

    Used for buildingcompoundingexpressions

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    28/74

    3 . Oper at o r s3 . Oper at o r sLogical Operators: Example

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    29/74

    4 . Flow Con t r o l4 . Flow Con t r o lConditionals

    Single condition Multiple Conditions

    {Follows with an elsestatement in the last}

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    30/74

    4 . Flow Con t r o l4 . Flow Con t r o lConditionals: Example

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    31/74

    4 . Flow Con t r o l4 . Flow Con t r o lConditionals: Switch Statement Here Expression isevaluated, control is passedto the case that matches thevalue

    Multiple Conditions

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    32/74

    4 . Flow Con t r o l4 . Flow Con t r o lSwitch Statement: Example

    CODE

    http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    33/74

    4 . Flow Con t r o l4 . Flow Con t r o lLoops: While Statement

    Program for calculating number ofyears for compoundnig $ 1,000 with 6

    % annual interest to $ 10,000

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    34/74

    4 . Flow Con t r o l4 . Flow Con t r o lLoops: for Statement

    Program for computing cos x from 0 to pi/2 with 5intervals

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    35/74

    4 . Flow Con t r o l4 . Flow Con t r o lTip: Loops be avoided when Vectors can be used

    This is because Vectors execute faster in MATLAB than loops

    Recall MATLAB is MATRIX Laboratory!

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    36/74

    4 . Flow Con t r o l4 . Flow Con t r o lBreak statement Any loop can be terminated by the break statement

    Upon encountering a break statement, control is passed to the firststatement outside the look.

    buildvec constructs a rowvector of arbitrary length byprompting its elements.

    When an empty element isencountered, break statementallows ignoring the statement

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    37/74

    4 . Flow Con t r o l4 . Flow Con t r o lContinue StatementWhen continue statement is encountered in a loop, control is passed tonext iteration without executing the statements in the current iteration.

    Strips all blanks from string s1

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    38/74

    4 . Flow Con t r o l4 . Flow Con t r o lReturn StatementNormally, function returns to the calling program when it runsout of the statements

    Return command forces to exit.

    If the error is less than 10-6,then the programme isterminated with in thestipulated number ofiterations.

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    39/74

    4 . Flow Con t r o l4 . Flow Con t r o lError StatementProgram execution can be terminated with the error function

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    40/74

    5 . Fu n ct ion5 . Fu n ct ionFunction Definition

    Arguments separated by commas

    Number of arguments may be zero

    For only one output argument, enclosing brackets can beommitted.

    Function should be saved with the file name function_name.m

    Then only function can be accessible to other program units

    File may have other functions subfunctions

    Subfunctions called only by primary function function_name

    or other subroutines in the fileSubfunctions not accessible to other program units

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    41/74

    5 . Fu n ct ion5 . Fu n ct ionCalling Functions Function can be called with fewer arguments than appearing inthe function definition

    Number of input & output arguments used in the function call canbe determined by the functions nargin & nargout respectively

    Above function has two inputs: x and epsilon

    Also has two outputs: x and numIter

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    42/74

    5 . Fu n ct ion5 . Fu n ct ionCalling FunctionsError tolerance epsilon is an optional input that may be used tooverride the default value 10-6

    code

    http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://trig.m/http://solve1.m/http://solve1.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    43/74

    5 . Fu n ct ion5 . Fu n ct ionCalling FunctionsOutput argument numIter which contains number of iterationsmay also be omitted from the function call

    http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    44/74

    5 . Fu n ct ion5 . Fu n ct ionEvaluating FunctionsHere we write the dx expression as a function in a different filenamed myfunc.m and link it to the source code.

    In this code, if dx isdefined as yourfunc(x),

    then unless the functionis renamed as y =yourfunc(x) and the file

    name changed asyourfunc.m things will

    not work

    Solve.m

    myfunc.m

    http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve1.m/http://solve2.m/http://codes/myfunc.mhttp://codes/myfunc.mhttp://solve2.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    45/74

    5 . Fu n ct ion5 . Fu n ct ionUse of function handle

    Here, call solve(@myfunc, 2) creates a function handle to myfuncand passes it to solve as an argument

    Hence, the variable func in solve contains the handle to myfunc

    Function passed to another function by its handle is evaluated byfeval function.

    code

    http://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://codes/myfunc.mhttp://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    46/74

    5 . Fu n ct ion5 . Fu n ct ionInline functions Not complicated functions can be expressed in inline function

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    47/74

    6 . I n p u t / Ou t p u t6 . I n p u t / Ou t p u tReading input

    http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    48/74

    6 . I n p u t / Ou t p u t6 . I n p u t / Ou t p u tPrinting Output

    http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    49/74

    6 . I n p u t / Ou t p u t6 . I n p u t / Ou t p u tPrinting Output

    7 . Ar r ay7 . Ar r ay

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    50/74

    Man ipu la t i onMan ipu la t i onCreating Arrays

    Colon operator

    7 . Ar r ay7 . Ar r ay

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    51/74

    Man ipu la t i onMan ipu la t i onLinspace command

    Logspace command

    7 . Ar r ay7 . Ar r ay

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    52/74

    Man ipu la t i onMan ipu la t i onzeros

    ones

    rand

    eye

    8 . W r i t in g an d8 . W r i t in g an d

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    53/74

    Ru n n in g Pr og r am sRu n n in g Pr og r am sMATLAB has two windows

    Command Window

    Interactive mode

    Any statement that entered gets processed

    Can be used for learning syntax and trying out basicideas.

    Editor/Debugger

    Type & Save Programs & Functions

    MATLAB editor has colour coding and automaticindentation

    8 . W r i t in g an d8 . W r i t in g an d

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    54/74

    Ru n n in g Pr og r am sRu n n in g Pr og r am sHow to Run ProgrammesFile must be saved as M file first

    A program can be run by invoking the run command fromeditors debug menu.

    Variables created during MATLAB session are saved in MATLAB

    workspace until they are cleared.

    Variables are cleared using clear statement.

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    55/74

    9 . Plo t t i n g9 . Plo t t i n gExample 1: Plot sin x and cos x on a two dimensionalplot

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    56/74

    9 . Plo t t i n g9 . Plo t t i n gExample 1: Plot sin x and cos x on a two dimensionalplot

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    57/74

    9 . Plo t t i n g9 . Plo t t i n gExample 2: Semilog plot

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    58/74

    9 . Plo t t i n g9 . Plo t t i n gExample 2: Semilog plot

    CODE

    http://solve3.m/http://solve3.m/http://solve3.m/http://solve3.m/http://semilog%20plot%20.m/http://semilog%20plot%20.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    59/74

    9 . Plo t t i n g9 . Plo t t i n gExample 3: Polar co-ordinatesy =

    linspace(0,2*pi,100);

    x = sin(2*x);

    polar(x,y);

    CODE

    http://semilog%20plot%20.m/http://semilog%20plot%20.m/http://semilog%20plot%20.m/http://semilog%20plot%20.m/http://semilog%20plot%20.m/http://semilog%20plot%20.m/http://semilog%20plot%20.m/http://semilog%20plot%20.m/http://semilog%20plot%20.m/http://polar.m/http://polar.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    60/74

    9 . Plo t t i n g9 . Plo t t i n gExample 4: Pie chartpie(X) draws a pie chart using the data in X. Each

    element in X is represented as a slice in the pie chart.

    pie(X,explode) offsets a slice from the pie. explode is avector or matrix of zeros and nonzeros that correspond to

    X.

    h = pie(...) returns a vector of handles to patch and text

    graphics objects.

    http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    61/74

    9 . Plo t t i n g9 . Plo t t i n gExample 4: Pie chartx = [1 3 0.5 2.5 2];

    explode = [0 1 0 0 0];pie(x,explode)

    colormap jet

    CODE

    http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://polar.m/http://pie.m/http://pie.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    62/74

    9 . Plo t t i n g9 . Plo t t i n gExample 5: Bar ChartA bar graph displays the values in a vector or matrix as

    horizontal or vertical barsbar(Y) draws one bar for each element in Y. If Y is a

    matrix, bar groups the bars produced by the elements ineach row.

    bar(x,Y) draws a bar for each element in Y at locationsspecified in x, where x is a monotonically increasing vectordefining the x-axis intervals for the vertical bars.

    http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    63/74

    9 . Plo t t i n g9 . Plo t t i n gExample 5: Bar Chartx = -2.9:0.2:2.9;

    bar(x,exp(-x.*x),'r')

    CODE

    http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://pie.m/http://barm.m/http://barm.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    64/74

    33--Dimensional graph plottingDimensional graph plotting3-Dimensional graphs include

    Normal 3-D graphMeshes

    SurfacesContour

    http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    65/74

    33--Dimensional graph plottingDimensional graph plotting3- D graphsplot3(x,y,t)- used to plot a 3- Dgraph of a point in space with co-

    ordinates of arrays x,y,t.h = plot3(...) returns a column

    vector of handles to lineseriesgraphics objects, with one handle perobject.

    http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    66/74

    33--Dimensional graph plottingDimensional graph plottingExample 1: using plot3 commandt = -10*pi:pi/100:10*pi;

    x = t.*cos(t);y = t.*sin(t);h = plot3(x,y,t);

    title('Curve u(t) = ')xlabel('x')

    ylabel('y')zlabel('z')grid

    CODE

    http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://barm.m/http://plota3d.m/http://plota3d.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    67/74

    33--Dimensional graph plottingDimensional graph plottingMesh command

    It is used to create a

    meshmeshgrid This

    function generatestwo 2-dimensional

    arrays for 3-D plots.

    x=[012];y=[101214];[xi,yi]=meshgrid(x,y)output

    >>xi=012012012

    >>yi =101010121212141414

    http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    68/74

    33--Dimensional graph plottingDimensional graph plottingExample 2: using mesh commandx = -1:0.05:1;

    y = x;

    [xi, yi] =

    meshgrid(x,y);

    zi = yi.^2 - xi.^2;

    mesh(xi, yi, zi)

    axis off

    CODE

    http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://plota3d.m/http://meshplot.m/http://meshplot.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    69/74

    33--Dimensional graph plottingDimensional graph plotting To plot the graph of the mesh

    surface together with thecontour plot beneath theplotted surface use functionm e s h c

    x = -1:0.05:1;y = x;

    [xi, yi] = meshgrid(x,y);

    zi = yi.^2 - xi.^2;

    meshc(xi, yi, zi)

    axis off

    Example 3: using meshc command

    CODE

    http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshplot.m/http://meshcplot.m/http://meshcplot.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    70/74

    33--Dimensional graph plottingDimensional graph plotting surf is used to

    visualize data as ashaded surface.

    surfc is used to

    visualize data as ashaded surface with thecontour plot beneath

    the plotted surface usefunction m e s h c

    http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    71/74

    33--Dimensional graph plottingDimensional graph plottingcontour is used to create

    the contour surface ofthe graph

    [X,Y] = meshgrid(-2:.2:2,-2:.2:3);

    Z = X.*exp(-X.^2-Y.^2);

    contour(X,Y,Z)

    CODE

    Example 4: drawing contours of a 3D graph

    http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://meshcplot.m/http://contourplot.m/http://contourplot.m/
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    72/74

    33--Dimensional graph plottingDimensional graph plottingSet t i ng t he Vi ew po i n t

    MATLAB enables user to control the orientation ofthe graphics displayed in an axes. user can specify the viewpoint, view target,

    orientation, and extent of the view displayed in afigure window. Theview command specifies the viewpoint by

    defining azimuth and elevation with respect to theaxis origin.

    3-Dimensional graph plotting

    http://www-ccs.ucsd.edu/matlab/techdoc/ref/view.htmlhttp://www-ccs.ucsd.edu/matlab/techdoc/ref/view.htmlhttp://www-ccs.ucsd.edu/matlab/techdoc/ref/view.html
  • 8/2/2019 Matlab Introduction & Graphics Presentation

    73/74

    33-Dimensional graph plottingDimensional graph plotting

    MATLAB automatically

    selects a viewpoint thatis determined bywhether the plot is 2-Dor 3-D.

    For 2-D plots, thedefault is azimuth =0 and elevation =90.

    For 3-D plots, thedefault is azimuth = -

    37.5 and elevation =30.

    3-Dimensional graph plotting

  • 8/2/2019 Matlab Introduction & Graphics Presentation

    74/74

    33-Dimensional graph plottingDimensional graph plotting

    [X,Y] = meshgrid([-

    2:.25:2]); Z =X.*exp(-X.^2 -Y.^2);

    surf(X,Y,Z)

    view([180 0])

    Example 5: using view command

    CODE

    http://viewcomm.m/http://viewcomm.m/