{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

MIT6_094IAP10_lec02

# MIT6_094IAP10_lec02 - 6.094 Introduction to programming in...

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

6.094 Introduction to programming in MATLAB Danilo Š ć epanovi ć IAP 2010 Lecture 2: Visualization and Programming

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

View Full Document
Homework 1 Recap How long did it take to do required problems? Did anyone do optional problems? Was level of guidance appropriate? Unanswered Questions? Some things that came up: Use of semicolon – never required if one command per line. You can also put multiple commands on one line; in this case a semicolon is necessary to separate commands: » x=1:10; y=(x-5).^2; plot(x,y); Assignment using indices – remember that you can index into matrices to either look up values or to assign value: » x=rand(50,1); inds=find(x<0.1); y=x(inds); x(inds)=-x(inds); x(inds)=3;
Outline (1) Functions (2) Flow Control (3) Line Plots (4) Image/Surface Plots (5) Vectorization

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

View Full Document
User-defined Functions Functions look exactly like scripts, but for ONE difference ¾ Functions must have a function declaration Help file Function declaration Inputs Outputs Courtesy of The MathWorks, Inc. Used with permission.
User-defined Functions Some comments about the function declaration No need for return: MATLAB 'returns' the variables whose names match those in the function declaration Variable scope: Any variables created within the function but not returned disappear after the function stops running function [x, y, z] = funName ( in1 , in2 ) Must have the reserved word: function Function name should match MATLAB file name If more than one output, must be in brackets Inputs must be specified

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

View Full Document
Functions: overloading We're familiar with » zeros » size » length » sum Look at the help file for size by typing » help size The help file describes several ways to invoke the function ¾ D = SIZE(X) ¾ [M,N] = SIZE(X) ¾ [M1,M2,M3,...,MN] = SIZE(X) ¾ M = SIZE(X,DIM)
Functions: overloading MATLAB functions are generally overloaded ¾ Can take a variable number of inputs ¾ Can return a variable number of outputs What would the following commands return: » a=zeros(2,4,8); %n-dimensional matrices are OK » D=size(a) » [m,n]=size(a) » [x,y,z]=size(a) » m2=size(a,2) You can overload your own functions by having variable input and output arguments (see varargin , nargin , varargout , nargout )

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

View Full Document
Functions: Excercise Write a function with the following declaration: function plotSin(f1) In the function, plot a sin wave with frequency f1, on the range [0,2 π ]: To get good sampling, use 16 points per period. ( ) 1 sin f x 0 1 2 3 4 5 6 7 -1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
Functions: Excercise Write a function with the following declaration: function plotSin(f1) In the function, plot a sin wave with frequency f1, on the range [0,2 π ]: To get good sampling, use 16 points per period. In an MATLAB file saved as plotSin.m, write the following: » function plotSin(f1) x=linspace(0,2*pi,f1*16+1); figure plot(x,sin(f1*x)) ( ) 1 sin f x

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

View Full Document
Outline (1) Functions (2) Flow Control (3) Line Plots (4) Image/Surface Plots (5) Vectorization
Relational Operators MATLAB uses mostly standard relational operators ¾ equal == ¾ not equal ~= ¾ greater than > ¾ less than < ¾ greater or equal >= ¾

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 / 45

MIT6_094IAP10_lec02 - 6.094 Introduction to programming in...

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

View Full Document
Ask a homework question - tutors are online