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

Numerically solve the ODE: y\' = 10 - y^2 using ode45. Use a start time of 0, en

ID: 3110280 • Letter: N

Question

Numerically solve the ODE: y' = 10 - y^2 using ode45. Use a start time of 0, end time of 1, and an initial condition of 0. 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, 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). Adjust your modifiedeuler function so that it has adaptive time If h times f(t, y) at any point exceeds 0.5, the value for

Explanation / Answer

1. Define a function called ode1_eqn

function dydt = ode1_eqn(t,y)

dydt = 10 - y*y

------------------------------------------------------------------------

Save the above script as "ode1_eqn.m"

Define tspan = [0 1]; % which defines the start and end times

y0 = 0; % initial condition of 0

% call ode45 matlab function

[t,y] = ode45(@ode1_eqn,tspan,y0,stepsize);

2) Using while loop, write a function to solve ODE using Modified Euler's Method

function [tout, yout] = modifiedeuler(odefunc, tspan, y0)

%odefunc, y' = 10 - y*y

% tspan = [0 1]

% y0 = 0

% stepsize

x1 = tspan(2); x0 = tspan(1);

% calaulating the value of h

h = ( x1 - x0)/stepsize

%loop for calculating values

while k <= n

    X(1,1) = x0; Y (1,1) = y0;

    X( 1, k+1) = X(1,k) + h;

    y_t = Y(1,k) + h* feval( odefunc , X(1,k) , Y(1,k));% Euler's formula

    Y(1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) ) ;

    %improving results obtained by modified Eular's formula

    while abs( Y(1,k+1) - y_t ) > h

        y_t = Y(1,k) + h*feval( df , X(1,k) , Y(1,k+1));

        Y( 1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) );

    end

k = k + 1;

end

tout = X;

yout = Y;

------------------------------------------------------------------------------------------------------

% Now call above function from Matlab terminal

[tout,yout] = modifiedeuler(ode1_eqn, tspan, y0, stepsize)

3.

function [tout, yout] = modifiedeuler(odefunc, tspan, y0)

%odefunc, y' = 10 - y*y

% tspan = [0 1]

% y0 = 0

% stepsize

x1 = tspan(2); x0 = tspan(1);

% calaulating the value of h

h = ( x1 - x0)/stepsize

%loop for calculating values

while k <= n

    X(1,1) = x0; Y (1,1) = y0;

    X( 1, k+1) = X(1,k) + h;

if(X(1,k+1) > x1)

X(1,k+1) = x1;

end

    y_t = Y(1,k) + h* feval( odefunc , X(1,k) , Y(1,k));% Euler's formula

    Y(1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) ) ;

    %improving results obtained by modified Eular's formula

    while abs( Y(1,k+1) - y_t ) > h

        y_t = Y(1,k) + h*feval( df , X(1,k) , Y(1,k+1));

        Y( 1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) );

    end

k = k + 1;

end

tout = X;

yout = Y;

------------------------------------------------------------------------------------------------------

4.

function [tout, yout] = modifiedeuler(odefunc, tspan, y0)

%odefunc, y' = 10 - y*y

% tspan = [0 1]

% y0 = 0

% stepsize

x1 = tspan(2); x0 = tspan(1);

% calaulating the value of h

h = ( x1 - x0)/stepsize

%loop for calculating values

while k <= n

    X(1,1) = x0; Y (1,1) = y0;

    X( 1, k+1) = X(1,k) + h;

    y_t = Y(1,k) + h* feval( odefunc , X(1,k) , Y(1,k));% Euler's formula

    Y(1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) ) ;

    %improving results obtained by modified Eular's formula

    while abs( Y(1,k+1) - y_t ) > h

        y_t = Y(1,k) + h*feval( df , X(1,k) , Y(1,k+1));

        Y( 1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) );

    end

k = k + 1;

end

if(h * Y(1,k+1) > 0.5)

h = h/2;

end

tout = X;

yout = Y;

------------------------------------------------------------------------------------------------------