%%Matlab code for 2nd order Runge Kutta and Modified Euler
clear all
close all
%function for solving ODE using rk2
fun=@(t,y) (y./t)-(y./t).^2;
%function for exact solution
fun_ext=@(t,y) t./(1+log(t));
%all step size
hh=[0.5 0.1 0.05 0.01 0.005 0.001];
for ii=1:length(hh)
t(1)=1;yA(1)=1; %initial
conditions
t_in=t(1); %Initial
t
t_max=2;
%Final t
%Runge Kutta 2 iterations
h=hh(ii);
%number of steps
n=(t_max-t_in)/h;
tic;
for i=1:n
k1=h*fun(t(i),yA(i));
k2=h*fun(t(i)+(1/2)*h,yA(i)+(1/2)*k1);
t(i+1)=t_in+i*h;
yA(i+1)=double(yA(i)+k2);
end
time_mid(ii)=toc;
%plotting the solution using RK2
figure(1)
hold on
plot(t,yA)
legendinfo{ii}=sprintf('RK2 for step length
%2.3f',h);
RE_mid(ii)=norm(yA-fun_ext(t));
clear t; clear yA;
end
xlabel('t')
ylabel('Y(t)')
title('y(t) vs t plot using RK2 method')
legend(legendinfo,'location','northwest')
for ii=1:length(hh)
t=1;
yH=1; %initial conditions
t_in=1; %Initial t
t_max=2;
%Final t
%Modified Euler step size
h=hh(ii);
%number of steps
n=(t_max-t_in)/h;
t3(1)=1;
y3(1)=yH;
tic;
for i=1:n
%improved Eular steps
m1=double(fun(t,yH));
m2=double(fun((t+h),(yH+h*m1)));
yH=yH+double(h*((m1+m2)/2));
t=t+h;
y3(i+1)=yH;
t3(i+1)=t;
end
time_mod(ii)=toc;
%plotting the solution using Modified
Euler
figure(2)
hold on
plot(t3,y3)
legendinfo{ii}=sprintf('Improved Euler for step
length %2.3f',h);
RE_mod(ii)=norm(y3-fun_ext(t3));
clear t3; clear y3;
end
xlabel('t')
ylabel('Y(t)')
title('y(t) vs t plot using improved Euler
method')
legend(legendinfo,'location','northwest')
figure(3)
hold on
loglog(hh,RE_mid,'-+',hh,RE_mod,'-o')
legend('Midpoint=RK2','Modified Euler')
xlabel('h')
ylabel('Relative error')
title('Loglog plot of stepsize vs. rel error')
figure(4)
hold on
loglog(hh,time_mid,'-+',hh,time_mod,'-o')
legend('Midpoint=RK2','Modified Euler')
xlabel('h')
ylabel('time')
title('Loglog plot of stepsize vs. time')
for ii=1:length(hh)
t=1;
yR=1; %initial conditions
t_in=1; %Initial t
t_max=2;
%Final t
%Modified Euler step size
h=hh(ii);
%number of steps
n=(t_max-t_in)/h;
t4(1)=1;
y4(1)=yR;
tic;
for i=1:n
%RK4 STEPS
k1=h*double(fun(t,yR));
k2=h*double(fun((t+h/2),(yR+k1/2)));
k3=h*double(fun((t+h/2),(yR+k2/2)));
k4=h*double(fun((t+h),(yR+k3)));
dy=(1/6)*(k1+2*k2+2*k3+k4);
t=t+h;
yR=yR+dy;
t4(i+1)=t;
y4(i+1)=yR;
end
time_rk4(ii)=toc;
%plotting the solution using Modified
Euler
figure(5)
hold on
plot(t4,y4)
legendinfo{ii}=sprintf('rk4 solution for step
length %2.3f',h);
RE_rk4(ii)=norm(y4-fun_ext(t4));
clear t4; clear y4;
end
xlabel('t')
ylabel('Y(t)')
title('y(t) vs t plot using rk4 method')
legend(legendinfo,'location','northwest')
%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%
1 st s2, y(1)1 The exact solution is given by yo) - = . 1+Int Write a MATLAB code to approximate ...
MATLAB HELP 3. Consider the equation y′ = y2 − 3x, where y(0) = 1. USE THE EULER AND RUNGE-KUTTA APPROXIMATION SCRIPTS PROVIDED IN THE PICTURES a. Use a Euler approximation with a step size of 0.25 to approximate y(2). b. Use a Runge-Kutta approximation with a step size of 0.25 to approximate y(2). c. Graph both approximation functions in the same window as a slope field for the differential equation. d. Find a formula for the actual solution (not...
Question 1: Given the initial-value problem 12-21 0 <1 <1, y(0) = 1, 12+10 with exact solution v(t) = 2t +1 t2 + 1 a. Use Euler's method with h = 0.1 to approximate the solution of y b. Calculate the error bound and compare the actual error at each step to the error bound. c. Use the answers generated in part (a) and linear interpolation to approximate the following values of y, and compare them to the actual value...
6. The differential equation: y 4y 2x y(0) 1/16 has the exact solution given by the following equation: v = (1 /2)s, + (14)s +1.16 Calculate y (2.0) using a step size h-0.5 using the following methods: (a) Euler (b) Euler P-c (c)4h order Runge-Kutta (d) Compare the errors for each method. (e) Solve using Matlab's ode45.m function. Include your code and a print of the solution.
The Program for the code should be matlab 5. [25 pointsl Given the initial value problem with the initial conditions y(0) 2 and y'(0)10, (a) Solve analytically to obtain the exact solution y(x) (b) Solve numerically using the forward Euler, backward Euler, and fourth-order Runge Kutta methods. Please implement all three methods yourselves do not use any built- in integrators (i.e., ode45)). Integrate over 0 3 r < 4, and compare the methods with the exact solution. (For example, using...
///MATLAB/// Consider the differential equation over the interval [0,4] with initial condition y(0)=0. 3. Consider the differential equation n y' = (t3 - t2 -7t - 5)e over the interval [0,4 with initial condition y(0) = 0. (a) Plot the approximate solutions obtained using the methods of Euler, midpoint and the classic fourth order Runge Kutta with n 40 superimposed over the exact solution in the same figure. To plot multiple curves in the same figure, make use of the...
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...
Complete using MatLab 1. Consider the following initial value problem 3t2-y, y(0) = 1 Using Euler's Method and the second order Runge-Kutta method, for t E [0, 1] with a step size of h 0.05, approximate the solution to the initial value problem. Plot the true solution and the approximate solutions on the same figure. Be sure to label your axis and include an a. appropriate legend b. Verify that the analytic solution to the differential equation is given by...
Runge-Kutta method R-K method is given by the following algorithm. Yo = y(xo) = given. k1-f(xy) k4-f(xi +h,yi + k3) 6 For i = 0, 1, 2, , n, where h = (b-a)/n. Consider the same IVP given in problem 2 and answer the following a) Write a MATLAB script file to find y(2) using h = 0.1 and call the file odeRK 19.m b) Generate the following table now using both ode Euler and odeRK19 only for h -0.01....
MATLAB help please!!!!! 1. Use the forward Euler method Vi+,-Vi + (ti+1-tinti , yi) for i=0.1, 2, , taking yo-y(to) to be the initial condition, to approximate the solution at 2 of the IVP y'=y-t2 + 1, 0 2, y(0) = 0.5. t Use N 2k, k2,...,20 equispaced timesteps so to 0 and t-1 2) Make a convergence plot computing the error by comparing with the exact solution, y: t (t+1)2 exp(t)/2, and plotting the error as a function of...
Problem Thre: 125 points) Consider the following initial value problem: dy-2y+ t The y(0) -1 ea dt ical solution of the differential equation is: y(O)(2-2t+3e-2+1)y fr exoc the differential equation numerically over the interval 0 s i s 2.0 and a step size h At 0.5.A Apply the following Runge-Kutta methods for each of the step. (show your calculations) i. [0.0 0.5: Euler method ii. [0.5 1.0]: Heun method. ii. [1.0 1.5): Midpoint method. iv. [1.5 2.0): 4h RK method...