MATLAB Homework: Numerical Methods and Taylor Expansions, Assignments of Computer Science

Information about a matlab homework assignment focusing on numerical methods, specifically euler's method and one-sided and centered differences for approximating derivatives. The assignment also includes problems on taylor expansions and finding the nested form of polynomials.

Typology: Assignments

Pre 2010

Uploaded on 09/17/2009

koofers-user-anb
koofers-user-anb 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Homework 4
Due on Thursday, February 14
There are five problems in this assignment. The first two problems require MATLAB implementation, and the last
three problems are the usual exercise problems. You should hand in the programming assignment online: go to the
course webpage and click on the CourseTool link on the left. You should submit a zip file containing two MATLAB
m-files with names q1.m, q2.m. Obviously, q1.m is the code for problem 1 and q2.m is the code for Problem 2. The
written solutions to the last three problems should be handed in as before (i.e., in class).
1 (Problem 10 on page 47) Use Euler’s method to compute approximate solutions to each of the initial value problems
below, using h1= 2,4,8,16.Compute the maximum error over the interval [0 1] for each value of h. Plot your
approximate solutions for the h=1
16 case.
1. y0=yln y, y(0) = 3; y(t) = e(ln 3)et
2. y0=ty, y(0) = 2; y(t) = 3et+t1
3. y0+ 4y= 1, y(0) = 1; y(t) = 1
4(3e4t+ 1).
(For this problem, you need to implement the Euler’s method using MATLAB. You code should plot the three approx-
imate solutions in one figure with different colors (say, red, blue and green).)
2We know that for approximating derivatives, the one-sided difference and centered difference have accuracies that
are linear and quadratic, respectively:
f0(x) = f(x+h)f(x)
h+O(h)
f0(x) = f(x+h)f(xh)
2h+O(h2)
Therefore, if you halve the step size h, the error will decrease by a factor of 2and 4, respectively. However, since the
computer uses floating point numbers in its computations, rounding errors will eventually become significant for small
hsuch that the expected decrease in error will no longer be observed when his sufficiently small. For this problem,
you will run a simple experiment to witness this.
Let f(x) = ex, and of course f0(x) = ex. We will numerically compute the derivative of f(x)at x= 1 using
both one-sided and centered differences. Take hk= (1
2)k, for k= 0,1,2,3,· · · ,65. Let Dhkdenote the derivative
numerically computed (using either method) with h=hk. Denote E(hk)the error |f0(1) Dhk|and E R(k)the ratio
ER(k) = E(hk1)
E(hk).
For each approximation method, your program should produce a figure that contains (a) the plot of ER(k)(in blue)
for k= 1,2,3,· · · ,65 and (b) the plot of Dhk(in red). See Figure 1. For centered difference, you should see that
ER(k)is approximately 4for k < 20 and its values become somewhat random for k > 20. This happens because the
error due to rounding is now roughly in the same order as the error due to approximation (see page 46). The computed
derivative Dhkbecomes zero when k > 53.
1
pf3

Partial preview of the text

Download MATLAB Homework: Numerical Methods and Taylor Expansions and more Assignments Computer Science in PDF only on Docsity!

Homework 4

Due on Thursday, February 14

There are five problems in this assignment. The first two problems require MATLAB implementation, and the last three problems are the usual exercise problems. You should hand in the programming assignment online: go to the course webpage and click on the CourseTool link on the left. You should submit a zip file containing two MATLAB m-files with names q1.m, q2.m. Obviously, q1.m is the code for problem 1 and q2.m is the code for Problem 2. The written solutions to the last three problems should be handed in as before (i.e., in class).

1 (Problem 10 on page 47) Use Euler’s method to compute approximate solutions to each of the initial value problems below, using h−^1 = 2, 4 , 8 , 16. Compute the maximum error over the interval [0 1] for each value of h. Plot your approximate solutions for the h = 161 case.

  1. y′^ = −y ln y, y(0) = 3; y(t) = e(ln 3)e

−t

  1. y′^ = t − y, y(0) = 2; y(t) = 3e−t^ + t − 1
  2. y′^ + 4y = 1, y(0) = 1; y(t) = 14 (3e−^4 t^ + 1).

(For this problem, you need to implement the Euler’s method using MATLAB. You code should plot the three approx- imate solutions in one figure with different colors (say, red, blue and green).)

2 We know that for approximating derivatives, the one-sided difference and centered difference have accuracies that are linear and quadratic, respectively:

f ′(x) = f (x + h) − f (x) h

  • O(h)

f ′(x) = f (x + h) − f (x − h) 2 h

  • O(h^2 )

Therefore, if you halve the step size h, the error will decrease by a factor of 2 and 4 , respectively. However, since the computer uses floating point numbers in its computations, rounding errors will eventually become significant for small h such that the expected decrease in error will no longer be observed when h is sufficiently small. For this problem, you will run a simple experiment to witness this.

Let f (x) = ex, and of course f ′(x) = ex. We will numerically compute the derivative of f (x) at x = 1 using both one-sided and centered differences. Take hk = ( 12 )k, for k = 0, 1 , 2 , 3 , · · · , 65. Let Dhk denote the derivative numerically computed (using either method) with h = hk. Denote E(hk) the error |f ′(1) − Dhk | and ER(k) the ratio

ER(k) =

E(hk− 1 ) E(hk)

For each approximation method, your program should produce a figure that contains (a) the plot of ER(k) (in blue) for k = 1, 2 , 3 , · · · , 65 and (b) the plot of Dhk (in red). See Figure 1. For centered difference, you should see that ER(k) is approximately 4 for k < 20 and its values become somewhat random for k > 20. This happens because the error due to rounding is now roughly in the same order as the error due to approximation (see page 46). The computed derivative Dhk becomes zero when k > 53.

Figure 1: Left: Centered difference. Right: One-sided difference.

You may find the following short code sample useful in figuring out MATLAB’s graphics commands. The code produces two figures: the first figure contains the plots of the function y = x^2 (in red) and its derivative (in blue), and the second figure contains the plot of the function y = ex^ (in green). Notice that the horizontal axes in the two figures are labelled differently.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function matlabHint

Y1=[]; DY1=[]; Y2=[]; for x=0:0.01: Y1=[Y1 xˆ2]; DY1=[DY1 2*x]; Y2=[Y2 exp(x)]; end

H1=figure; plot(x, Y1, ’r’); hold on; plot(x, DY1, ’b’); hold off; H2=figure; plot(1:length(Y2), Y2, ’g’); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

3 (Problem 1 on page 42) Write each of the following polynomials in nested form:

  1. x^6 + 2x^4 + 4x^2 + 1;
  2. 5 x^6 + x^5 + 3x^4 + 3x^3 + x^2 + 1;
  3. x^2 + 5x + 6;
  4. x^3 + 3x + 2.

4 (Problem 12 on page 50) Use Taylor expansions for f (x ± h) to derive an O(h^2 ) accurate approximation to f ′′ using f (x) and f (x ± h). Provide all the details of the error estimate. Hint: Go out as far as the fourth derivative term,