Question

In: Advanced Math

Plot the Euler’s Method approximate solution on [0,1] for the differential equation y* = 1 +...

Plot the Euler’s Method approximate solution on [0,1] for the differential equation
y* = 1 + y^2 and initial condition (a) y0 = 0 (b) y0 = 1, along with the exact solution (see
Exercise 7). Use step sizes h = 0.1 and 0.05. The exact solution is y = tan(t + c)

Solutions

Expert Solution


%%Matlab code for solving ode using Euler Improved Euler and RK4
clear all
close all

%function for which Solution have to do
fun=@(t,y) 1+y.^2;
%initial guess
tinit=0; yinit=0;

%Answering Question for initial condition y(0)=0.
syms y(t)
eqn = diff(y,t) == 1+y.^2;
cond = y(0) == yinit;
ySol(t) = dsolve(eqn,cond);

%displaying result
fprintf('Exact solution y(t)=')
disp(ySol)

%Answering Question
%All step size
hh=[0.1 0.05];

fprintf('\nSolution Using Euler Method\n')
%loop for all step size and tend
    for jj=1:length(hh)
        h=hh(jj);
        tend=1;
      
        [t_euler,y_euler]=Euler(fun,tinit,yinit,tend,h);
        y_ext=double(ySol(tend));
      
        error = abs(y_ext-y_euler(end));
        fprintf('\tFor h=%2.4f at t=%2.2f value of y=%f.\n ',h,t_euler(end),y_euler(end))
        fprintf('\t Error E = %f.\n\n',error)
        figure(1)
        hold on
        plot(t_euler,y_euler)
    end
plot(t_euler,ySol(t_euler))
legend('Euler h=0.1','Euler h=0.05','Actual Solution')
xlabel('t')
ylabel('y(t)')
title('Solution plot using Euler for y(0)=0')
box on

clear all
%function for which Solution have to do
fun=@(t,y) 1+y.^2;
%initial guess
tinit=0; yinit=1;
%Answering Question for initial condition y(0)=0.
syms y(t)
eqn = diff(y,t) == 1+y.^2;
cond = y(0) == yinit;
ySol(t) = dsolve(eqn,cond);
%displaying result
fprintf('Exact solution y(t)=')
disp(ySol)
%Answering Question
%All step size
hh=[0.1 0.05];
fprintf('\nSolution Using Euler Method\n')
%loop for all step size and tend
    for jj=1:length(hh)
        h=hh(jj);
        tend=1;
        [t_euler,y_euler]=Euler(fun,tinit,yinit,tend,h);
        y_ext=double(ySol(tend));    
        error = abs(y_ext-y_euler(end));
        fprintf('\tFor h=%2.4f at t=%2.2f value of y=%f.\n ',h,t_euler(end),y_euler(end))
        fprintf('\t Error E = %f.\n\n',error)
        figure(2)
        hold on
        plot(t_euler,y_euler)
    end
plot(t_euler,ySol(t_euler))
legend('Euler h=0.1','Euler h=0.05','Actual Solution')
xlabel('t')
ylabel('y(t)')
title('Solution plot using Euler for y(0)=1')
box on

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Euler Method
function [t_euler,y_euler]=Euler(f,tinit,yinit,tend,h)
    %Euler method
    % h amount of intervals
    t=tinit;         % initial t
    y=yinit;         % initial y
    t_eval=tend;     % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t_euler(1)=t;
    y_euler(1)=y;
    for i=1:n
        %Eular Steps
        m=double(f(t,y));
        t=t+h;
        y=y+h*m;
        t_euler(i+1)=t;
        y_euler(i+1)=y;
    end  
end
%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%


Related Solutions

Plot the Trapezoid Method approximate solution on [0,1] for the differential equation y = 1 +...
Plot the Trapezoid Method approximate solution on [0,1] for the differential equation y = 1 + y2 and initial condition (a) y0 = 0 (b) y0 = 1, along with the exact solution (see Exercise 6.1.7). Use step sizes h = 0.1 and 0.05 (Code In Matlab)
Use ten steps in Euler’s method to determine an approximate solution for the differential equation y′...
Use ten steps in Euler’s method to determine an approximate solution for the differential equation y′ = x3, y(0) = 0, using a step size Δx = 0.1.
y'=√2x+y+1 general solution of differential equation
y'=√2x+y+1 general solution of differential equation
Compute, by Euler’s method, an approximate solution to the following initial value problem for h =...
Compute, by Euler’s method, an approximate solution to the following initial value problem for h = 1/8 : y’ = t − y , y(0) = 2 ; y(t) = 3e^(−t) + t − 1 . Find the maximum error over [0, 1] interval.
1. (Euler’s method) First, work out the first three steps by hand. Then approximate y(2) for...
1. (Euler’s method) First, work out the first three steps by hand. Then approximate y(2) for each of the initial value problems using Euler’s method, first with a step size of h = .1 and then with a step size of h = .05 using the Excel spreadsheet. (a) dy dx = 2xy, y(0) = 1 (b) dy dx = x − y x + 2y , y(0) = 1 (c) dy dx = y + x, y(0) = 1...
Find the general solution of the differential equation using the method of undetermined coefficients y" +...
Find the general solution of the differential equation using the method of undetermined coefficients y" + y' - 6y = x^2
Find a general solution to the differential equation using the method of variation of parameters. y''+...
Find a general solution to the differential equation using the method of variation of parameters. y''+ 25y= sec5t The general solution is ​y(t)= ___ y''+9y= csc^2(3t) The general solution is ​y(t)= ___
Euler’s method Consider the initial-value problem y′ = −2y, y(0) = 1. The analytic solution is...
Euler’s method Consider the initial-value problem y′ = −2y, y(0) = 1. The analytic solution is y(x) = e−2x . (a) Approximate y(0.1) using one step of Euler’s method. (b) Find a bound for the local truncation error in y1 . (c) Compare the error in y1 with your error bound. (d) Approximate y(0.1) using two steps of Euler’s method. (e) Verify that the global truncation error for Euler’s method is O(h) by comparing the errors in parts (a) and...
2. Find the general solution to the differential equation x^2y''+ y'+y = 0 using the Method...
2. Find the general solution to the differential equation x^2y''+ y'+y = 0 using the Method of Frobenius and power series techniques.
Use Euler's Method to make a table of values for the approximate solution of the differential...
Use Euler's Method to make a table of values for the approximate solution of the differential equation with the specified initial value. Use n steps of size h. (Round your answers to six decimal places.) y' = 10x – 3y,   y(0) = 7,   n = 10,   h = 0.05 n xn yn 0 1 2 3 4 5 6 7 8 9 10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT