%%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) (2-2.*t.*y)./(t.^2+1);
%initial guess
tinit=0; yinit=1;
%Answering Question 1.
syms y(t)
eqn = diff(y,t) == (2-2.*t.*y)./(t.^2+1);
cond = y(0) == yinit;
ySol(t) = dsolve(eqn,cond);
%displaying result
fprintf('Exact solution phi(t)=')
disp(ySol)
%Answering Question 1.
%all t values
tt=[0.4 1.0];
%All step size
h=0.1;
fprintf('\nSolution Using Euler Method\n')
%loop for all step size and tend
for jj=1:length(tt)
tend=tt(jj);
[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)
end
%Answering Question 2.
fprintf('\nSolution Using Improved Euler Method\n')
%loop for all step size and tend
for jj=1:length(tt)
tend=tt(jj);
[t_euler,y_euler]=ImpEuler(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,tend,y_euler(end))
fprintf('\t Error E =
%f.\n\n',error)
end
%Answering Question 4.
fprintf('\nSolution Using RK4 Method\n')
%All step size
%loop for all step size and tend
for jj=1:length(tt)
tend=tt(jj);
[t_euler,y_euler]=RK4(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,tend,y_euler(end))
fprintf('\t Error E =
%f.\n\n',error)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Improved Euler Method
function [t_imp,y_imp]=ImpEuler(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_imp(1)=t;
y_imp(1)=y;
for i=1:n
%improved Euler steps
m1=double(f(t,y));
m2=double(f((t+h),(y+h*m1)));
y=y+double(h*((m1+m2)/2));
t=t+h;
y_imp(i+1)=y;
t_imp(i+1)=t;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Runge Kutta Method
function [t_rk,y_rk]=RK4(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_rk(1)=t;
y_rk(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)));
dy=(1/6)*(k1+2*k2+2*k3+k4);
t=t+h;
y=y+dy;
t_rk(i+1)=t;
y_rk(i+1)=y;
end
end
%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%
Question 1: Given the initial-value problem 12-21 0 <1 <1, y(0) = 1, 12+10 with exact...
[7] 1. Consider the initial value problem (IVP) y′(t) = −y(t), y(0) = 1 The solution to this IVP is y(t) = e−t [1] i) Implement Euler’s method and generate an approximate solution of this IVP over the interval [0,2], using stepsize h = 0.1. (The Google sheet posted on LEARN is set up to carry out precisely this task.) Report the resulting approximation of the value y(2). [1] ii) Repeat part (ii), but use stepsize h = 0.05. Describe...
1 st s2, y(1)1 The exact solution is given by yo) - = . 1+Int Write a MATLAB code to approximate the solution of the IVP using Midpoint (RK2) and Modified Euler methods when h [0.5 0.1 0.0s 0.01 0.005 0.001]. A) Find the vector w mid and w mod that approximates the solution of the IVP for different values of h. B) Plot the step-size h versus the relative error of both in the same figure using the LOGLOG...
Given the initial-value problem y'=2-2tyt2+1, 0 ≤t≤1, y0=1 With exact solution yt= 2t+1t2+1 Using MATLAB use Euler’s method with h = 0.1 to approximate the solution of y
YOUR TEACHER Consider the initial-value problem y = (x + y - 1)?.Y(0) - 2. Use the improved Euler's method with h = 0.1 and h = 0.05 to obtain approximate values of the solution at x = 0.5. At each step compare the approximate value with the actual value of the analytic solution (Round your answers to four decimal places.) h 0.1 Y(0.5) h 0.05 Y(0.5) actual value Y(0.5) = Need Help? Tuto Tutor
YOUR TEACHER Consider the initial-value problem y = (x + y - 1)?.Y(0) - 2. Use the improved Euler's method with h = 0.1 and h = 0.05 to obtain approximate values of the solution at x = 0.5. At each step compare the approximate value with the actual value of the analytic solution. (Round your answers to four decimal places.) 0.1 y(0.5) h 0.05 (0.5) actual value Y(0.5) - Need Help? Tuto Tutor
1. Consider the IVP y = 1 - 100(y-t), y(0) = 0.5. (a) Find the exact solution. (b) Use the Forward Euler, Heun, and Backward Euler methods to find approximate solu- tions ont € 0, 0.5], using h = 0.25. Plot all four solutions (exact and three approxima- tions) on the same graph. (c) Maple's approximation is plotted, along with the direction field, in Figure 1. Use it, and the exact solution, to explain the behaviours observed in your numerical...
Solve using Matlab Use the forward Euler method, Vi+,-Vi+(4+1-tinti ,Vi) for i= 0,1,2, , taking yo y(to) to be the initial condition, to approximate the solution at t-2 of the IVP y'=y-t2 + 1, 0-t-2, y(0) = 0.5. Use N = 2k, k = 1, 2, , 20 equispaced time steps (so to = 0 and tN-1 = 2). Make a convergence plot, computing the error by comparing with the exact solution, y: t1)2 -exp(t)/2, and plotting the error as...
Consider the following initial value problem y = -5y + 5++ 2t, Ostsi, y(0) = 1/3, with h = 0.1. The exact solution of this problem is y(t) = {2 + e-5t. (2) If you use 2-step Adams-Moulton method (AM2) to solve this problem, what is the specific formula expressing Yi+1 in terms of Yi, Yi-1, të+1, ti, and ti-1 for solving this problem? (3) Use 2-step Adams-Moulton method (AM2) to solve this problem and plot the results to compare...
Consider the initial value problem i. Find approximate value of the solution of the initial value problem at using the Euler method with . ii. Obtain a formula for the local truncation error for the Euler method in terms of t and the exact solution . 2,,2 5 0.1 y = o(t) 2,,2 5 0.1 y = o(t)
- 2y²,y(0) =0. 1+x² 4) Consider the IVP y'= х a) Verify that y= is the solution of this IVP. 1+x? b) Use Euler's method to numerically approximate the solution to this IVP over the interval [0,2] in x. Set the mesh width h=0.1. Calculate the true values of y atthe appropriate values of x as well as the error in your numerical approximation. Report your results in the table given. Report answers to four decimal places. Numerical Actual y...