In: Advanced Math
Problem 1: Consider the following Initial Value Problem (IVP)
where ? is the dependent variable and ? is the independent
variable: ?′=sin(?)∗(1−?) with ?(0)=?0 and ?≥0
Note: the analytic solution for this IVP is:
?(?)=1+(?_0−1)?^cos(?)−1
Part 1A: Approximate the solution to the IVP using Euler’s method
with the following conditions: Initial condition ?_0=−1/2; time
step ℎ=1/16; and time interval ?∈[0,20]
+ Derive the recursive formula for Euler’s method applied to this
IVP
+ Plot the Euler’s method approximation
+ Plot the absolute error between the approximation and the exact
solution using a semilog plot
Coding errors, please type out.
%%Matlab code for Euler's forward
clear all
close all
%function for Euler equation solution
f=@(t,y) sin(t)*(1-y);
%displaying the function
fprintf('Function for which differential equation have to
solve\n')
disp(f)
%Initial values
t0=0;
y0=-1/2;
%t_end values
t_end=20;
%step size
h=1/16;
%x end values
xn=t0:h:t_end;
% Euler steps
y_r(1)=y0;
t_r(1)=t0;
fprintf(' \nSolving Differential equation using
Euler method for step size h=%f\n',h)
fprintf(' Initial condition t=%f ; y_1=
%f.\n',t_r(1),y_r(1))
for
i=1:length(xn)-1
t_r(i+1)= t_r(i)+h;
y_r(i+1)=
y_r(i)+h*double(f(t_r(i),y_r(i)));
end
%exact solution
syms y(t)
eqn = diff(y,t) == sin(t)*(1-y);
cond = y(0) == y0;
ySol(t) = dsolve(eqn,cond);
fprintf('\n\nExact solution is y(t)= ')
disp(ySol(t))
figure(1)
plot(t_r,y_r)
hold on
plot(t_r,ySol(t_r))
xlabel('t')
ylabel('y(t)')
title('y(t) vs. t plot')
legend('Euler method','Exact solution')
figure(2)
err=abs(ySol(t_r)-y_r);
semilogy(t_r,err)
title('semilog plot of Euler error')
xlabel('t')
ylabel('error')
%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%