{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ECOR 2606 - Lecture 5 (functions and plotting)

ECOR 2606 - Lecture 5 (functions and plotting) - .Iffplot...

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

View Full Document Right Arrow Icon
1/18/2010 1 Function inputs may be functions. If fplot was not part of Matlab, we could easily create something very similar. function [ ] = DIYfplot( f, range ) %DIYfplot Do it yourself version of fplot. Plots a function over a range. % Inputs: f = function to be plotted % range = two element vector containing start and end of range % No Outputs (like a C++ void function) n = 100; % number of points x = linspace (range(1), range(2), n); % generate x values % generate y values y = zeros ([1 n]); % preallocate for efficiency for k = 1 : n y(k) = f(x(k)); end plot (x, y); end The function provided may be a function m file (@ required before name); DIYfplot( @PR, [0 7]) >> grid on >> xlabel ('Mach number'); >> ylabel ('Pressure Ratio'); Built in functions are also acceptable (@ required before name): >> DIYfplot( @sin, [0 4*pi]) And so are anonymous functions (no @ required–the @ is in the definition). >> f = @(x) x^3 + 2* x^2 + 7; >> DIYfplot( f, [ 5 5]) Note: We could also create our own version of fzero and will soon be doing exactly this.
Background image of page 1

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

View Full Document Right Arrow Icon
1/18/2010 2 Another potentially useful function is shown below. It produces a table of function values. function [ ] = ftable( f, vector ) %ftable Produces table of function values. % Inputs: f = function to be tabulated % vector = vector of values for the independent variable % No Outputs (like a C++ void function) fprintf (' Independent Dependent\n'); for k = 1 : length(vector) x = vector(k); y = f(x); fprintf ('%12f%16f\n', x, y); end end Sample usage: >> ftable (@PR, 1:0.5:6) Independent Dependent 1.000000 1.892929 1.500000 3.413275 2.000000 5.640441 2.500000 8.526136 3.000000 12.060965 3.500000 16.242001 4.000000 21.068081 4.500000 26.538665 5.000000 32.653474 5.500000 39.412352 6.000000 46.815206 Improved Loop: The loop in the function can be reduced to: for x = vector fprintf ('%12f%16f\n', x, f(x)); end
Background image of page 2
1/18/2010 3 op Array op Scalar Scalar op Array Array op Array +, OK [1 2 3] + 4 = [5 6 7] OK 9 – [1 2 3] = [8 7 6] Array operation assumed Sizes must match
Background image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

ECOR 2606 - Lecture 5 (functions and plotting) - .Iffplot...

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

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