Ch3 Programming with MATLAB - Spring 2011

# Ch3 Programming with MATLAB - Spring 2011 - PGE 310...

This preview shows pages 1–6. Sign up to view the full content.

MATLAB Programming Chapter 3 of Numerical Methods with MATLAB, Gerald Recktenwald PGE 310: Formulation and Solution in Geosystems Engineering Dr. Balhoff Spring 2011 1 Review of Plotting in MATLAB Matlab has some really powerful graphics 2D plots can be made easily in MATLAB –p l o t ( x , y ) – Annotating helps make a professional plot – subplot(x,y,thisplot) command allows for multiple plots – Log plots also common 3D plots (surface and contour) are also useful – surf(x,y,Z)-or surfc, surfl, mesh- uses vectors – surf(X,Y,Z) uses matrices for X and Y – contour(X,Y,Z) – ‘meshgrid(x,y)’ converts vector data of x, y to matrices 0 100 200 300 400 500 600 700 800 900 10 1 10 2 10 3 Semilog Plot 0 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1 Linear Plot -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 -2 0 2 -0.5 0 0.5 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Today…We learn some real programming! Script files and function-files – Two types of ‘.m’ files – Code written in MATLAB function files require inputs and send back outputs – We have already been using functions! – Any number of inputs and outputs allowed Input and Output (1/O) – input –d i s p –f p r i n t f 3 Scripts: Sequences of Interactive Statements Stored in a File How are they used? – A lot like what we have been doing in the command window (but now we can save our work and make changes later) – Have no input or output parameters (which we will find different than a function file) Side Effects and Comments – All variables are added to the workspace (i.e., command window) – Old variables in workspace will be replaced by those of same name – Remember you can include comments by preceding them with a “ % 4
Opening an ‘.m’ file How to open: File menu or white sheet OR 5 An Example of a Script ‘.m’ file 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Function m-Files: Code that communicates with the command window and other functions script via predefined list of input and output parameters Similar to “subroutines” in other languages function [ outputParameterList ] = functionName( inputParameterList ) List of outputs (separated by commas) List of inputs (separated by commas) • First word is always function • Output list is optional and included in square brackets • functionName must also be the same as the file name ! • Variables may be strings, matrices, vectors, etc. Important Notes 7 function [output parameter List] = functionName(input parameter List) Function File Input parameters output parameters 8
Creating and calling a function file You can “call” a function from a script file, another function file, or the interactive mode In my m- file called addmult.m function [s,p]=addmult(x,y) s=x+y; p=x*y In the interactive node or script file >> a=4; b=5; >> [u,v]=addmult(a,b) >> u=9 >> v=20 Note that input and output parameters can be different variable names!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 19

Ch3 Programming with MATLAB - Spring 2011 - PGE 310...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online