{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

4 - Function inputs may be functions If fplot was not part...

Info icon This 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 % N O (lik C id f i ) % 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.
Image of page 1

Info icon This 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); f i f ('%12f%16f\ ' ) 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
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
Image of page 3

Info icon This 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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern