

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
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
1 / 3
This page cannot be seen from the preview
Don't miss anything!


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.
−t
(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
f ′(x) = f (x + h) − f (x − h) 2 h
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:
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,