Ch E 310 - Fall 10 - Lecture 3

Ch E 310 - Fall 10 - Lecture 3 - Lecture 3 August 31, 2010...

Info iconThis preview shows pages 1–8. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 3 August 31, 2010 Agenda: Function handles Structure arrays Plots and graphics m-files: script vs. function files for loops In-Class Exercise 3 MATLAB: Function Handles A function handle is a way to store functions for easy use They are a quick method for defining functions in MATLAB Anatomy of a sample function handle: @ (x,y,z) (x + y) * z make handle with variables having the expression >> fun = @(x,y,z) (x + y) * z % define function of xyz fun = @(x,y,z) (x + y) * z >> fun(1,2,3) % evaluate function with chosen xyz ans = 9 >> fun(2,3,5) % re-evaluate with new xyz values ans = 25 Parameters within a function handle only exist locally in the handle MATLAB: Function Handles Function handles may use defined variables in the Workspace >> x = 1; y = 2; z = 3; % define x,y,z in Workspace >> fun = @(x,y) (x+y)*z % define fun = f(x,y) fun = @(x,y) (x+y)*z >> fun(1,1) % evaluate with x,y and Workspace z = 3 ans = 6 Changing z in the workspace will not affect fun after its been defined >> z = 50; >> fun(1,1) % will still use z = 3 ans = 6 Once defined, a function handle can be used with other expressions >> fun2 = @(x) x.^2; % the . allows array math >> fun2(2 + 2) % can do math in the argument as well ans = 16 >> fun2([1 2; 3 4]) % operate on an array ans = 1 4 9 16 >> fun3 = @(x,y) fun2(x) .* (x+y); % calls fun2 >> fun3([1 2; 3 4],3) ans = 4 20 54 112 MATLAB: Function Handles Function handles can call other function handles within them >> A.a = 1; A.b = 2; A.c = 3 % define struct array A A = a: 1 b: 2 c: 3 >> A(2).a = 4; A(2).b = 5; A(2).c = 6 % add more fields to A A = 1x2 struct array with fields: a b c >> A(1) ans = a: 1 b: 2 c: 3 >> A(2) ans = a: 4 b: 5 c: 6 >> A(2).b ans = 5 MATLAB: Structure Arrays Structure (struct) arrays provide convenient organization A A(1) A(2) a = 1 b = 2 c = 3 a = 4 b = 5 c = 6 struct array hierarchy: MATLAB: Plotting Graphs The plot command is the most basic for creating a graph >> x = linspace(0,10,11); % x data >> y = x.^2 - 2.*x + 3; % y data (same length) >> plot(x,y) The plot command uses data stored in arrays Graphs are displayed in a separate Figure Window The default setting is a thin blue line connecting points You have precise control over how graphs are displayed as well see MATLAB: plot Formatting There are numerous options using plot >> plot(x,y, 'ro-' ) % include format specifier string Colors : k black, r red, y yellow, g green, w white, c cyan,...
View Full Document

This note was uploaded on 09/21/2011 for the course CH E 310 taught by Professor Staff during the Spring '08 term at Iowa State.

Page1 / 26

Ch E 310 - Fall 10 - Lecture 3 - Lecture 3 August 31, 2010...

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

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