Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB (euler) If you haven’t already done so, enter the following commands: >>

ID: 3143197 • Letter: M

Question

MATLAB (euler)

If you haven’t already done so, enter the following commands:

>> f=@(t,y)(3*y);

>> t=linspace(0,.5,100);

y=2*exp(3*t); % defines the exact solution of the ODE

>> [t50,y50]=euler(f,[0,.5],2, 50); % solves the ODE using Euler with 50 steps

Determine the Euler’s approximation for N = 500 and N = 5000 and fill in the following table with the values of the approximations, errors, and ratios of consecutive errors at t = 0.5. Include table, as well as the MATLAB commands used to for entries.

approximation

error

ratio

function [t,y] = euler(f,tspan,y0,N)

% Solves the IVP y' = f(t,y), y(t0) = y0 in the time interval tspan = [t0,tf]
% using Euler's method with N time steps.
% Input:
% f = name of inline function or function M-file that evaluates the ODE
% (if not an inline function, use: euler(@f,tspan,y0,N))
% For a system, the f must be given as column vector.
% tspan = [t0, tf] where t0 = initial time value and tf = final time value
% y0 = initial value of the dependent variable. If solving a system,
% initial conditions must be given as a vector.
% N = number of steps used.
% Output:
% t = vector of time values where the solution was computed
% y = vector of computed solution values.

m = length(y0);
t0 = tspan(1);
tf = tspan(2);
h = (tf-t0)/N; % evaluate the time step size
t = linspace(t0,tf,N+1); % create the vector of t values
y = zeros(m,N+1); % allocate memory for the output y
y(:,1) = y0'; % set initial condition
for n=1:N
y(:,n+1) = y(:,n) + h*f(t(n),y(:,n)); % implement Euler's method
end
t = t'; y = y'; % change t and y from row to column vectors
end

n

approximation

error

ratio

5 50 500

Explanation / Answer

exact solution= 8.9634

exact solution= 8.9634

n Approx error ratio 5 7.4259 1.5344 0.1712 50 8.7678 0.1956 0.0218 500 8.9439 0.021 0.0023