Numerical Analysis - Assignment 1 Questions | MATH 4340, Lab Reports of Mathematical Methods for Numerical Analysis and Optimization

Material Type: Lab; Professor: Ginting; Class: Numerical Analysis; Subject: Mathematics; University: University of Wyoming; Term: Spring 2009;

Typology: Lab Reports

Pre 2010

Uploaded on 08/19/2009

koofers-user-ifg
koofers-user-ifg 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHE/COSC/MATH 4340-01 Numerical Analysis
Computer Laboratory Assignment 01
Due Date: Thursday, 01/22/09
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.(0.1)
Using the Taylor’s Theorem we concluded that the error is |f(x)D(x)| Cx, where xis the step-size
parameter, and Cis a constant independent of x. In fact, we have found that this Cdepends on f′′. This
assessment says that as xis 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: pis 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 Nis 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) = x2and
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
D2(x) = f(x+x)f(xx)
2x.(0.2)
What do you observe compared to Problem 1?
1

Partial preview of the text

Download Numerical Analysis - Assignment 1 Questions | MATH 4340 and more Lab Reports Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

CHE/COSC/MATH 4340-01 Numerical Analysis

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 )| ≈ Cx , 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?