MATLAB
Create an M-File for this IVP, dy/dt = t^2 - 16*sin(t), y(0) = 0 and create an anonymous function g so that it evaluates the slope field at points of our new ODE.
Ensure you use commands of using a for loop to plot the exact solution for the IVP in this exercise as well as the Euler approximations for Δt=0.5, Δt=0.25, and Δt=0.125 all on the same graph.
% IVP: dy/dt = t^2-16*sin(t), y(0)=0
% it's exact solution is g(t)= t^3/3+16*cos(t)-16
% SOlution by Euler method using N steps
delta_t=0.5; % time step's size
N=10; % number of steps
t(1)=0;
y(1)=0;
g(1)=0;
for n=1:N
y(n+1)= y(n)+delta_t*((t(n)^2/2)-16*sin(n*delta_t));
g(n+1)=(t(n)^3/3)-16*sin(n*delta_t);
t(n+1)=n*delta_t;
end
plot(t,y,'b')
xlabel('t')
ylabel('Numerical and exact values of y')
hold on
plot(t,g,'g')
legend({'numerical ','exact'},'Location','southwest')
hold off
using time stepsize delta_t =0.25, following output
using time stepsize delta_t =0.125, following output
Get Answers For Free
Most questions answered within 1 hours.