MATLAB Script:
close all
clear
clc
f = @(t,y) exp(2/y); % Given ODE
t0 = 0; tf = 2; % Intervals of t
y0 = 2; % Initial condition
n = 4;
h = (tf - t0)/n; % Step Size
t = t0:h:tf;
[t_e, y_e] = ode45(f, [t0, tf], y0); % Exact solution
plot(t_e, y_e), hold on % Plot the exact solution
y = my_euler(t0, y0, tf, h, f);
plot(t, y, 'o-') % Plot Euler's method solution
y = my_imp_euler(t0, y0, tf, h, f);
plot(t, y, '*-') % Plot Improved Euler's method solution
y = my_rk4(t0, y0, tf, h, f);
plot(t, y, '^-'), hold off % Plot RK4 method solution
fprintf('RK4 Method
Solution\n-----------------------------------------------------------\n')
fprintf('%-10s%-20s\n', 't(i)', 'y(i)')
for i = 1:length(t)
fprintf('%-10.4f%-20.4f\n', t(i), y(i))
end
xlabel('t'), ylabel('y(t)')
legend('Exact Solution', 'Euler''s Method', 'Improved Euler''s
Method', 'RK4 Method', 'Location', 'northwest')
title('Solution of ODE')
function y = my_euler(t0, y0, tf, h, f)
y(1) = y0;
t = t0:h:tf;
for i = 1:length(t)-1
y(i+1) = y(i) + h*f(t(i), y(i)); % Euler Update
end
end
function y = my_imp_euler(t0, y0, tf, h, f)
y(1) = y0;
t = t0:h:tf;
for i = 1:length(t)-1
f1 = f(t(i), y(i));
f2 = f(t(i) + h, y(i) + h*f1);
y(i+1) = y(i) + (h/2)*(f1 + f2); % Improved Euler's Update
end
end
function y = my_rk4(t0, y0, tf, h, f)
y(1) = y0;
t = t0:h:tf;
for i = 1:length(t)-1
k1 = f(t(i), y(i));
k2 = f(t(i) + 0.5*h, y(i) + 0.5*h*k1);
k3 = f(t(i) + 0.5*h, y(i) + 0.5*h*k2);
k4 = f(t(i) + h, y(i) + k3*h);
y(i + 1) = y(i) + (1/6)*(k1 + 2*k2 + 2*k3 + k4)*h; % RK4
Update
end
end
Output:
Plot:
In Exercise, use the Runge-Kutta method with the given number n of steps to approximate the...
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...
(3) Consider the expressions (a) Write down the Runge-Kutta method for the numerical solution to a differential equation Oy (b) Show that if f is independent of y, i.e. f(x, y) g(x) for some g, then the Runge-Kutta method on the interval n n + h] becomes Simpson's Rule for the numerical approximation of the integral g(x) dr. In this case, what is the global error, in terms of O(hk) for some k>0? (3) Consider the expressions (a) Write down...
Numerical Methods Consider the following IVP dy=0.01(70-y)(50-y), with y(0)-0 (a) [10 marks Use the Runge-Kutta method of order four to obtain an approximate solution to the ODE at the points t-0.5 and t1 with a step sizeh 0.5. b) [8 marks Find the exact solution analytically. (c) 7 marks] Use MATLAB to plot the graph of the true and approximate solutions in one figure over the interval [.201. Display graphically the true errors after each steps of calculations. Consider the...
Find an approximate solution to the pendulum problem such that d2 theta /dt2 +g/l theta = 0. Use an approximate solver in matlab to find the solution to the exact equation d2 theta/dt2 +g/l * sin( theta) = 0. Compare the two solutions when the initial angle is 10, 30, and 90. Find a way to quantify the difference. One approximate method for solving differential equations is Runge-Kutta, which in Matlab goes by the name ode45. I have made a...
Need help with this MATLAB problem: Using the fourth order Runge-Kutta method (KK4 to solve a first order initial value problem NOTE: This assignment is to be completed using MATLAB, and your final results including the corresponding M- iles shonma ac Given the first order initial value problem with h-time step size (i.e. ti = to + ih), then the following formula computes an approximate solution to (): i vit), where y(ti) - true value (ezact solution), (t)-f(t, v), vto)...
Consider a cylindrical storage tank with surface area A which contains a liquid at depth y:At time t = 0, the tank is empty (y = 0 when t = 0). Liquid is supplied to the tank at a sinusoidal rate Qin =3Qsin2 (t) and withdrawn from the tank as: πππ’π‘ = 3(π¦ β π¦ππ’π‘) 1.5 if π¦ > π¦ππ’π‘ πππ’π‘ = 0 if π¦ β€ π¦ππ’π‘Β Please note that both πππ and πππ’π‘ have units m3 /h.Β The mass...
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....
Numerical Methods for Differential Equations - Please post full correct solution!!! - need to use MATLAB 3. (a) Write Matlab functions to integrate the initial value problem y = f(x,y), y(a) = yo, on an interval [a, b] using: β’ Euler's method β’ Modified Euler β’ Improved Euler β’ Runge Kutta 4 It is suggested that you implement, for example, Improved Euler as [x, y) = eulerimp('f', a, yo, b, stepsize), where (2,y) = (In, Yn) is the computed solution....
Solve Example 6.2 on page 111 of textbook, using (1) the 4th order Runge Kutta method and (2) Simulink method, respectively. Please submit your MATLAB code m file and slx file You can use the results from the code attached below (from the textbook by ODE45 solver) as a reference Example 6.2 ode45 ex.m % This program solves a system of 3 differential equations % by using ode45 function % y1 (0)=0, y2 (0)=1.0, y3 (0)=1.0 clear; clc initial [0.0...