MIT6_094IAP10_lec02 - 6.094 Introduction to programming in...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 6.094 Introduction to programming in MATLAB Danilo epanovi IAP 2010 Lecture 2: Visualization and Programming 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 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 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 ) 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 1 2 3 4 5 6 7-1-0.8-0.6-0.4-0.2 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....
View Full Document

This note was uploaded on 08/27/2011 for the course CS 1671 taught by Professor Smith during the Spring '11 term at Georgia Institute of Technology.

Page1 / 45

MIT6_094IAP10_lec02 - 6.094 Introduction to programming in...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online