


%%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...
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
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
- 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...
Use the modified Euler method to find approximate solution of the following initial- value problem y' -Sy + 16t + 2, ost-1, y(0)-2. Write down the scheme and find the approximate values for h 0.2. Don't use the code.
Apply Euler-trapezoidal predictor-corrector method to the IVP in
problem 1 to approximate y(2), by choosing two values of h, for
which the iteration converges. (Don't really need to show work or
do by hand, MATLAB code will work just as well).
1. For the IVP: y' =ty, y(0) = ) 0t 4 Compare the true solution with the approximate solutions from t = 0 to t 4, with the step size h 0.5, obtained by each of the following methods....
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...
Use Improved Euler for first question, Runge- Katta for 2nd one.
Thank you
In each of Problems 7 through 12, find approximate values of the solution of the given initial value problem at t-0.5,1.0, 1.5, and 2.0 (a) Use the improved Euler method with h 0.025 (b) Use the improved Euler method with h-0.0125 In each of Problems 7 through 12, find approximate values of the solution of the given initial value problem at0.5,1.0, 1.5, and 2.0. Compare the results...