Consider the initial value problem dx/dt = 3 + t - x(t) and x(0) = 1. a) First c
ID: 2083543 • Letter: C
Question
Consider the initial value problem dx/dt = 3 + t - x(t) and x(0) = 1. a) First check that phi(t) = t + 2 - e^-t is the solution to this initial value problem. b) Using a simple MATLAB code that implements the Euler method with the stepsize h = 0.05 compute approximate values of the solution of the given initial value problem for t = 0.1, 0.2, 0.3 and 0.4. Compare the approximate values you found with the exact values. c) Repeat part b) with stepsize h = 0.025. Since the Euler method is a first order method, halving the stepsize should approximately half the error between the computed values and the exact values. Verify this with your computations. d) Now use a MATLAB implementation of the modified Euler method to repeat part b) and c). This method is a second order method, so halving the step size should lead to approximately a quarter of the error between computed values and the exact values.Explanation / Answer
function [x,y] = forwardEuler(f,a,b,n,y0) h = (b-a)/n; x = [a zeros(1,n)]; y = [y0 zeros(length(y0),n)]; for i = 1:n x(i+1) = x(i)+h; y(i+1) = y(i)+h*f(x(i),y(i)); end end