# Lab10 - Engineering 7 Introduction to Programming for...

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

Engineering 7: Prof. Alexandre Bayen Introduction to Programming for Engineers Spring 2011 Lab 10: Numerical Differentiation and Integration Date Assigned: 5:00pm, Friday – April 15. Date Due: 5:00pm, Friday – April 22. Problem 1 : Write a function with header [dy, X] = myNumDiff(f, a, b, n, option) where f is handle to a function. The function myNumDiff should compute the derivative of f numerical for n evenly spaced points starting at a and ending at b according to the method defined by option . The input argument option is one of the following strings: ‘forward’ , ‘ backward’ , ‘central’ . Note that for the forward and backward method, the output argument, dy , should be 1x(n-1) , and for the central difference method dy should be 1x(n-2). The function should also output a row vector X that is the same size as dy and denotes the x-values for which dy is valid. Test Cases: >> x = linspace(0, 2*pi,100); >> f = @sin; >> [dyf, Xf] = myNumDiff(f, 0, 2*pi, 10, 'forward'); >> [dyb, Xb] = myNumDiff(f, 0, 2*pi, 10, 'backward'); >> [dyc, Xc] = myNumDiff(f, 0, 2*pi, 10, 'central'); >> plot(x, cos(x), Xf, dyf, Xb, dyb, Xc, dyc) >> title('Analytic and Numerical Derivatives of Sine') >> xlabel('x') >> ylabel('y') >> grid on >> legend('analytic', 'forward', 'backward', 'central') 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 Analytic and Numerical Derivatives of Sine x y analytic forward backward central

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

View Full Document
Engineering 7: Prof. Alexandre Bayen Introduction to Programming for Engineers Spring 2011 >> x = linspace(0, pi, 1000); >> f = @(x) sin(exp(x)); >> [dy10, X10] = myNumDiff(f, 0, pi, 10, 'central'); >> [dy20, X20] = myNumDiff(f, 0, pi, 20, 'central'); >> [dy100, X100] = myNumDiff(f, 0, pi, 100, 'central'); >> plot(x, cos(exp(x)).*exp(x), X10, dy10, X20, dy20, X100, dy100) >> xlabel('x') >> ylabel('y') >> title('Numerical Derivative of f(x) = sin(e^{x})') >> legend('Analytic', '10 points', '20 points', '100 points') 0 0.5 1 1.5 2 2.5 3 3.5 -25 -20 -15 -10 -5 0 5 10 15 20 x y Numerical Derivative of f(x) = sin(e x ) Analytic 10 points 20 points 100 points
Engineering 7: Prof. Alexandre Bayen

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 ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern