Question

In: Advanced Math

Solve the following initial value problem over the interval from t = 0 to 2 where...

Solve the following initial value problem over the interval from t = 0 to 2 where y(0) = 1 using the following methods.

dy/dt=y*t^2−1.1y

a) Analytical method

b) Euler's method with h=0.5 at t=2

c) Euler's method with h=0.25 at t=2

d) Midpoint method with h=0.5 at t=2

e) Fourth-order Runge-Kutta method with h=0.5 at t=2

f) Display all yor results obtained above on the same graph

Solutions

Expert Solution


%%Matlab code for Euler, Mid Point, RK2 and RK4 method
clear all
close all
%Function for which solution have to do
f=@(t,y) y.*t.^2-1.1.*y;


    h=0.5; % amount of intervals
    fprintf('\nSolution for Euler method using step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at t=0.\n')
    %Euler method
    %%%%%%%%%%%%%%%        
    t=0;             % initial t
    y=1;             % initial y
    t_eval=2;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t2(1)=t;
    y2(1)=y;
    for i=1:n
        %Eular Steps
        m=double(f(t,y));
        t=t+h;
        y=y+h*m;
        t2(i+1)=t;
        y2(i+1)=y;
        fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t2(i+1),t2(i+1),y2(i+1))
    end

  
    %Euler method
    %%%%%%%%%%%%%%%
    h=0.25;
    fprintf('\nSolution for Euler method using step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at t=0.\n')
    t=0;             % initial t
    y=1;             % initial y
    t_eval=2;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t3(1)=t;
    y3(1)=y;
    for i=1:n
    %Euler steps
       m=double(f(t,y));
       t=t+h;
       y=y+h*m;
       y3(i+1)=y;
       t3(i+1)=t;
       fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t3(i+1),t3(i+1),y3(i+1))
    end

  
    %RK4 method
    %%%%%%%%%%%%%%%
    h=0.5; % amount of intervals
    fprintf('\nSolution for RK4 method using step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at t=0.\n')
    t=0;             % initial t
    y=1;             % initial y
    t_eval=2;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t4(1)=t;
    y4(1)=y;
    for i=1:n
    %RK4 Steps
       k1=h*double(f(t,y));
       k2=h*double(f((t+h/2),(y+k1/2)));
       k3=h*double(f((t+h/2),(y+k2/2)));
       k4=h*double(f((t+h),(y+k3)));
       dx=(1/6)*(k1+2*k2+2*k3+k4);
       t=t+h;
       y=y+dx;
       t4(i+1)=t;
       y4(i+1)=y;
       fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t4(i+1),t4(i+1),y4(i+1))
    end
  
    %Midpoint method
    h=0.5;
    %%%%%%%%%%%%%%%
    fprintf('\nSolution for Midpoint method using step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at t=0.\n')
    t=0;             % initial t
    y=1;             % initial y
    t_eval=2;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t5(1)=t;
    y5(1)=y;
    for i=1:n
    %Midpoint Steps
       k1=h*double(f(t,y));
       k2=h*double(f((t+h),(y+k1)));
     
       dx=(1/2)*(k1+k2);
       t=t+h;
       y=y+dx;
       t5(i+1)=t;
       y5(i+1)=y;
       fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t5(i+1),t5(i+1),y5(i+1))
    end

  
     %%Exact solution
     syms y(t)
        eqn = diff(y,t) == y*t^2-1.1*y;
        cond = y(0) == 1;
        ySol(t) = dsolve(eqn,cond);
        fprintf('Exact solution for given ode is y(t)=')
        disp(ySol)
        yy_ext(t)=ySol;
        fprintf('Initial condition y(0)=1 at t=0.\n')
        for ii=1:length(t4)
            y6(ii)=double(yy_ext(t4(ii)));
            fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t4(ii),t4(ii),y6(ii))
        end

%%Plotting solution using Euler method
figure(1)
hold on
plot(t2,y2,'Linewidth',2)
plot(t3,y3,'Linewidth',2)
plot(t4,y4,'Linewidth',2)
plot(t5,y5,'linewidth',2)
plot(t4,y6,'linewidth',2)

xlabel('t')
ylabel('y(t)')
title('Solution plot y(t) vs. t')
legend('Euler Method','Euler Method','RK4 Method','Midpoint Method','Exact solution','Location','northwest')
grid on

            %%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%


Related Solutions

Solve the following initial value problem over the interval from t = 0 to 1 where...
Solve the following initial value problem over the interval from t = 0 to 1 where y(0) = 1 using the following methods with a step size of 0.25. dy/dt=(1+4t)*sqrt(y) a) Analytical method b) Euler's method c) Heun's method without iteration d) Ralston's method e) Fourth-order Runge-Kutta method f) Display all your results obtained above on the same graph
Solve the initial value problem: Y''-4y'+4y=f(t) y(0)=-2, y'(0)=1 where f(t) { t if 0<=t<3 , t+2...
Solve the initial value problem: Y''-4y'+4y=f(t) y(0)=-2, y'(0)=1 where f(t) { t if 0<=t<3 , t+2 if t>=3 }
1. solve the initial value problem. (t^(2)+1)y'+2ty=tant , y(0)=2 2.find the solution to this initial value...
1. solve the initial value problem. (t^(2)+1)y'+2ty=tant , y(0)=2 2.find the solution to this initial value problem. yy'=e^x+x , y(0)=y_0 y_0 is a nonzero constant.
Use Laplace transforms to solve the initial value problem: y''' −y' + t = 0, y(0)...
Use Laplace transforms to solve the initial value problem: y''' −y' + t = 0, y(0) = 0, y'(0) = 0, y''(0) = 0.
Solve the laplace transform to solve the initial value problem. y"-6y'+9y=t. Y(0)=0, y'(0)=1
Solve the laplace transform to solve the initial value problem. y"-6y'+9y=t. Y(0)=0, y'(0)=1
solve the inital value problem ?′′ + 6?′ + 9? = 2?^(-t) ; ?(0) =4, ?’(0)...
solve the inital value problem ?′′ + 6?′ + 9? = 2?^(-t) ; ?(0) =4, ?’(0) = −6
solve the following initial value problem y''+4y'=g(t),y(0)=0,y' (0)=1 if g(t) is the function which is 1...
solve the following initial value problem y''+4y'=g(t),y(0)=0,y' (0)=1 if g(t) is the function which is 1 on [0,1) and zero elsewhere
Solve the initial value problem z(5)+ 2z'''- 8z' = 0, where z(0) = 4, z'(0) =...
Solve the initial value problem z(5)+ 2z'''- 8z' = 0, where z(0) = 4, z'(0) = 2, z"(0) = 8, z"'(0) = 12, and z(4)(0) = 8
Use the Laplace transform to solve the following initial value problem: y′′−3y′−28y=δ(t−7) y(0)=0, y′(0)=0 y(t)=
Use the Laplace transform to solve the following initial value problem: y′′−3y′−28y=δ(t−7) y(0)=0, y′(0)=0 y(t)=
Solve the initial value problem: y''+2y'+y = x^2 , y(0)=0 , y'(0) = 0
Solve the initial value problem: y''+2y'+y = x^2 , y(0)=0 , y'(0) = 0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT