MATLAB: How do you use Euler’s Method to approximate the solution

differential equationseuler's methodhomeworkivp

I need to use Euler's method to approximate the solution to the this IVP over some t range which is chosen.
Below is all the code I have so far. My initial condition (as used before) is y(0)=1 and I want to find y(1) using Euler's Method.
I'm not sure what my next step is…can you help?
I am using R2019a
%% Task 1: Solve the ODE using the Symbolic Math Package and dsolve()
%Define the variables using syms - syms creates symbolic variables
syms y(t) y0 F t0 h tfinal
%define the ordinary differential equation
ode = diff(y,t) == cos(t^2);
%solve the differential equation using dsolve
ysol = dsolve(ode,y(0)==y0);
%Create a figure called Task 1
figure ('Name','Task 1')
%Pick 3 different initial conditions for which the solution exists
%Conditions are the y values (y values are 1, 1.5, and 3 at varying t
%values. See the t values below.
conds = [1 1.5 2]'; %some values of y0
f = matlabFunction(subs(ysol,y0,conds));
t = linspace(0,5);
y = f(t);
%plotting the 3 equations
plot(t,y,'linewidth',2)
title('Symbolic Solutions')
xlabel('t')
ylabel('y(t)')
grid on
lgndc = sprintfc('IC = %.1f',conds);
legend(lgndc, 'Location','E')
%% Task 2: Euler's Method
%Pick a single initial condition from Task 1
%The initial condition we will use is y(0)=1
%Use Euler's method to approximate the solution to this IVP over some t
%range which you choose
%Create a figure called Task 2
figure ('Name','Task 2')
%The range of t is from 0 to 1
%We are using the initial condition that y(0)=1, but we want to know y(t)
%when t=1
%In other words we want to find y(1)
y = y0;
yout = y;
t0 = 0;
h = 0.5;
tfinal = 1;
for t= t0 : h : tfinal-h
y = y + h*ode;
yout = [yout;y]
end

Best Answer

Euler's method approximates the area under a curve by using rectangular segments. The figure illustrates this process:
Matlab Answers 20190731a.JPG
You specify the curve, in this case (dY/dT), and pick a starting value (Y0) and a step size, h. (Note that in this figure Y0 = 0)
Starting with the initial value, Y0, the next value is obtained by adding the rectangular area defined by (dY/dT) at T=0, and h.
This process is repeated at each step in h.
The illustration uses a large value of h to illustrate the process. The more rectangles you use (i.e. as h becomes smaller) the smaller the error becomes, so you want to use a large number of rectangles (a small value for h).