Question

In Exercise, use the Runge-Kutta method with the given number n of steps to approximate the solution to the initial-value pro

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Command Window RK4 Method Solution t(i) У(i) 0.0000 2.0000 0.5000 3.1046 1.0000 3.9855 4.7755 1.5000 2.0000 5.5135 f > |

Plot:

Solution of ODE Exact Solution Eulers Method -Improved Eulers Method A-RK4 Method 5.5 5 4.5 3.5 3 2.5 0.6 0.8 1.2 1.4 1.8 0

Add a comment
Know the answer?
Add Answer to:
In Exercise, use the Runge-Kutta method with the given number n of steps to approximate the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Improved Euler for first question, Runge- Katta for 2nd one. Thank you In each of...

    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...

  • Both parts please! 1 Runge-Kutta Method The discretization of the spatial derivatives of a PDE often...

    Both parts please! 1 Runge-Kutta Method The discretization of the spatial derivatives of a PDE often results in a system of ODEs of the fornm du Runge-Kutta methods are the most commonly used schemes for numerically integrating in time the ODE system. We will numerically implement the "standard" third-order Runge-Kutta method. To advance the solution u from time t to t + Δ1, three sub-steps, are taken. If the solution at time t is un the following three steps are...

  • 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 an...

    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...

  • Using the fourth order Runge-Kutta method (KK4 to solve a first order initial value problem NOTE:...

    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)...

  • Runge-Kutta method R-K method is given by the following algorithm. o y(xo)- given k2-f(xi5.yi tk,...

    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....

  • use matlab Assignment: 1) Write a function program that implements the 4th Order Runge Kutta Method....

    use matlab Assignment: 1) Write a function program that implements the 4th Order Runge Kutta Method. The program must plot each of the k values for each iteration (one plot per k value), and the approximated solution (approximated solution curve). Use the subplot command. There should be a total of five plots. If a function program found on the internet was used, then please cite the source. Show the original program and then show the program after any modifications. Submission...

  • Find an approximate solution to the pendulum problem such that d2 theta /dt2 +g/l theta =...

    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...

  • Numerical Methods for Differential Equations - Please post full correct solution!!! - need to use MATLAB...

    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...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT