Matlab 1. Numerically solve the ODE: y\' = 10 - y 2 using obe45 . Use a start ti
ID: 3834666 • Letter: M
Question
Matlab
1. Numerically solve the ODE: y' = 10 - y2 using obe45. Use a start time of 0, end time of 1, and an initial condition of 0.
2. Using a while loop, write a function that can solve an ODE using Modified Euler's method. This function will work similarly to ode45, but the step size will have to be specified as an input.
function [tout,yout] = modifiedeuler(odefunc, tspan, y0, h)
3. Add an if statement to your your modifiedeuler function so that it doesn't overshoot the end time (i.e. change h in the final step to exactly reach the end time)
Please ANSWER using Matlab code. Thank you.
Explanation / Answer
1) function f=fun1(t,y)
f=10-y^2;
[t1 f1]=ode45('fun1',[0 1],1);
2)function [tout,yout] = modifiedeuler(odefun,tspan,y0,h)
yout(1) = y0; %setting initial y value
tout(1) = tspan(1); %setting initial t value
i=1;
while tout(i) < tspan(2);
yout(i+1) = yout(i) + h*odefun(tout(i),yout(i)); %need to calculate eulers method to find modified eulers method
tout(i+1) = tout(i) + h;
i = i + 1;
end
yout(i+1) = yout(i) + h*(odefun(tout(i),yout(i)) + odefun(tout(i+1),yout(i+1)))/2;
end