Programming Assignment 4
1.
Backward Euler method with Newton’s method
2.
Implement a MATLAB function backeuler.m
My code for backeuler.m: function [ti,wi] = backeuler(f,dfdy,a,b,alpha,N,maxiter,tol)
h = (b - a)/N;
y = zeros(N,1);
t = zeros(N,1);
y(1) = alpha; %initial value
t(1) = a;
for i=1:N
th = t(i) + h;
w0 = y(i);
% Newton method
n_f = @(x) x - w0 - h*f(th,x);
n_df = @(x) 1 - h*dfdy(th,x);
y(i+1) = newton(n_f,n_df,w0,tol,maxiter);
t(i+1) = th;
end
ti = t;
wi = y;
end
Compare results using Backward Euler and using RK4
The two lines are very close.
3.
A combustion model equation:
(a) Number of steps of N required
N = 707
(b) Verify estimate of N and plot the solutions
Code is provided as below:
format long
f = @(t,y) y*y*(1-y);
df = @(t,y) 2*y -3*y*y;
a=0; b=2000; alpha=0.9;
N = 707
N1 = 1.1* N
N2 = 0.9* N
N3 = 1.2* N
N4 = N + 5
[t1,w1] = rk4(f,a,b,alpha,N);
[t2,w2] = rk4(f,a,b,alpha,N1);
[t3,w3] = rk4(f,a,b,alpha,N2);
[t4,w4] = rk4(f,a,b,alpha,N3);
[t5,w5] = rk4(f,a,b,alpha,N4);
w2
plot(t1,w1,t2,w2,t3,w3,t4,w4,t5,w5);
legend('N', '+10%','-10%',’+20%’,’N+5’, 'location', 'southeast');
% Save plot to file
print -dpng quiz3b.png
The results are as below
(c) Show that the backward Euler method is A-stable
The region R of absolute stability for a one-step method is
We obtain region R of absolute stability contains the entire left half-plane,
∴ backward Euler method is A-stable.
(d) Solve (4) using your backeuler with N=1, N=5, and N=10. Plot the results.
Code is provided as below: f = @(t,y) y*y*(1-y); df = @(t,y) 2*y -3*y*y; a=0; b=2000; alpha=0.9; maxiter=20; tol=1e-12;
[t1,w1] = backeuler(f,df,a,b,alpha,1,maxiter,tol);
[t2,w2] = backeuler(f,df,a,b,alpha,5,maxiter,tol);
[t3,w3] = backeuler(f,df,a,b,alpha,10,maxiter,tol); plot(t1,w1,t2,w2,t3,w3); legend('N=1','N=5', 'N=10', 'location', 'southeast');
With N=1,5 or 10, the value approaches to 1, so the method appears to be stable.