
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Professor: Ginting; Class: Numerical Analysis; Subject: Mathematics; University: University of Wyoming; Term: Spring 2009;
Typology: Lab Reports
1 / 1
This page cannot be seen from the preview
Don't miss anything!

Computer Laboratory Assignment 01
Due Date: Thursday, 01/22/
Problem 1. We have briefly discussed in class how to quantify an error associated with approximating the derivative f ′( x ) of a given function f ( x ) by
D ( x ) =
f ( x + ∆ x ) − f ( x ) ∆ x
Using the Taylor’s Theorem we concluded that the error is | f ′( x ) − D ( x )| ≈ C ∆ x , where ∆ x is the step-size parameter, and C is a constant independent of ∆ x. In fact, we have found that this C depends on f ′′. This assessment says that as ∆ x is decreased , the approximation (0.1) is expected to be more accurate (hence the error is smaller ). Your task in this assignment is to present a numerical observation of this theoretical finding. Write down the following MATLAB function and save and name it as fprime.m.
function fprime(p, func, exactdfunc, N) % % for i=1:N dx(i) = 4ˆ(-i); % step-size parameter. approx = (func(p+dx(i)) - func(p))/dx(i); % approximate derivative. exact = exactdfunc(p); error(i) = abs(exact - approx); % relative error end
loglog(dx,error); % A logarithmic plot of error against dx. xlabel(’step-size’); ylabel(’error’);
This MATLAB function takes four arguments: p is the point at which the derivative is computed, func is a MATLAB inline function whose derivative is to be computed, exactdfunc is a MATLAB inline function containing the exact derivative of the function, and N is the number of step-size parameter. The output of the MATLAB function above is a logarithmic plot of the error against ∆ x (or dx). To invoke an inline function, use the MATLAB command inline. For example, if you want to create inline function f ( x ) = sin( x ) and save it in a variable f, then from MATLAB prompt, you should invoke f=inline(’sin(x)’). Run the function for p=0.1, and N=10. Plot the error for computing derivative of f ( x ) = x^2 and f ( x ) = sin ( x ). In your report show both error plots and discuss what you observe. Estimate the slope of the line in the plot and compare it with the theoretical finding above.
Problem 2. Redo Problem 1 above for another approximation of derivative
D 2 ( x ) =
f ( x + ∆ x ) − f ( x − ∆ x ) 2 ∆ x
What do you observe compared to Problem 1?