introduction to matlab

30
A Mathematician’s Introduction to MATLAB

Upload: chanukya-krishna-chama

Post on 14-Sep-2015

27 views

Category:

Documents


2 download

DESCRIPTION

Introduction to Matlab

TRANSCRIPT

  • A Mathematicians Introduction to MATLAB

  • PlanI cant teach you how to use Matlab effectively, but I hope that I can show you enough so that when you need Matlab you will know how to start.

  • Contents BasicsGraphing y=f(x)User defined functions (M-file)Numerical integration

  • MATLAB EnvironmentCommand Window Command History

  • Some Important DetailsEverything is a matrix.Comma or space separates row elements. Semicolon separates rows.Elements are referred to by indices in parentheses.Assignment uses = (not :=)Terminal semicolon (;) suppresses output.Variable names are Case sensitive.Two sets of arithmetic operations vector, element-wise.Previous inputs cant be edited directly.

  • Practice Entering Some Expressions>>4+3*sqrt(2)>>sin(pi/6)>>i^2>>j^2>>x=log(10)>>y=exp(x)

  • Recalculation with MATLAB To edit and/or recalculate a previous line, press the up arrow to scroll back through the worksheet until you find what you want to recalculate. Then press Enter

  • Entering a matrix and assigning it to a variable name Remember that row elements are separated by comma or space and rows by semicolon or enter

  • Try the following.X*B , B*X , pi*X , B^2 , X^2, X.^2 , Y=X*X , Y^2, Y.^2, B.*B , B*B , B.*B , sqrt(X) , exp(X) , cos(X)

  • Two Kinds of Arithmetic OperationsMatrix operations * and ^

    Element-wise operations .* and .^

  • Saving your work If you exit MATLAB and then start it again, the history of your previous work will appear, but none of your variables will be known in the workspace.You can get the variables back either by recalculating them or by Saving the workspace.

  • Help on how to save workspace

  • Warning: MATLAB Workspace If you are working on a computer that is not your own, and if you are using MATLABs default Work folder, be sure to save a copy of your saved workspace in your own file space.

  • The Graph of y = f(x)plot(X,Y) where X is a vector of domain elements and Y is the vector of corresponding values.First example: y = cos2(x) + 1Using X = [2,-3] previously defined, let Y=(cos(X)).^2+1

  • How to make a domain vectorA slick way to make the vector X = [a, a+h, a+2h, , b-h, b] is >> X=a:h:b;

    Lets plot y = cos2(x)+1 on [-p,p] with h=p/18 (or make one of your own)

    >> X=-pi:pi/18:pi; >> Y=(cos(X)).^2+1; >> plot(X,Y)

  • Creating an M-file for a functionUser defined functions are created in M-files.File>New>M-Filefunction y=f(x) y=(cos(x)).^2+1; endSince the function is named f it must be saved with the filename f.m

  • Numerical IntegrationLets compute where f is our previously defined function. Search Help for integration. >> quad(@f,0,pi)

  • Matlab Conditional Structures

  • Final ThoughtsNobody could or would want to know everything about MATLAB.Start with a particular project or problem.Use the Help files.Be willing to try something.Be prepared for frustration, but if you need MATLAB it is worth the frustration.