Matlab Problem: Runge-Kutta Fehlberg

In summary: Remember to include the function f(t,w) in your inputs and to store the values at each step for your desired output.
  • #1
iwonde
31
0

Homework Statement


I need to write a Matlab program that solves the following:

Use the Runge-Kutta Fehlberg Algorithm with tolerance TOL= 10^(-4) to approximate the solution to the initial-value problem:
y'=(y/t)^2 + y/t, 1<=t<=1.2, y(1)=1, with hmax=0.05 and hmin=0.02.


Homework Equations



The Attempt at a Solution



%This is the source code for the function rkf.
% w denotes the approximation to y
% h=step size
function [w,t,h]=rkf(a,b,w0,TOL,hmax,hmin)
%Step 1
h=hmax; t=a; w=w0; FLAG=1;

%Step 2
while(FLAG==1)

%Step 3
F1 = h*f(t,w);
F2 = h*f(t+h/4 ,w+F1/4);
F3 = h*f(t+3*h/8,w+ 3*F1/32+9*F2/32);
F4 = h*f(t+12*h/13,w+932*F1/2197-7200*F2/2197+7296*F3/2197);
F5 = h*f(t+h,w+(439*F1)/216-(8*F2)+3680*F3/513-(845*F4)/4104);
F6 = h*f(t+h/2,w-8*F1/27+2*F2-(3544*F3)/2565+1859*F4/4104-11*F5/40);


%Step 4
R = abs(F1/360-128*F3/4275-2197*F4/75240 + F5/50 + 2*F6/55)/h;

%Step 5
if (R<=TOL)

%Step 6
t= t+h;
w= w + 25*F1/216 + 1408*F3/2565 + 2197*F4/4104 - F5/5;
end

%Step 8
delta=0.84*(TOL/R)^(1/4);

%Step 9
if delta <= 0.1
h=0.1*h;
elseif delta>=4
h=4*h;
else
h=delta*h;
end

%Step 10
if h>hmax
h=hmax;
end

%Step 11
if t>=b
FLAG=0;
elseif (t+h)>b
h=b-t;
elseif h<hmin
FLAG=0;
fprintf('minimum h exceeded\n');
return;
end
end
end
=====================================
%I've tried compiling the code with:

a=1; b=1.2; TOL=10^(-4); w0=1; hmax=0.05; hmin=0.02;
f= @(t,w) (w/t)^2 + w/t;
[w,t,h]=rkf(a,b,w0,f,TOL,hmax,hmin);
w
t
h


======================================

The above code didn't work. I'm attempting at a code that outputs something like:
i t_i w_i h_i
---------------------------------
1 1.05
2 1.10
3 1.15
4 1.20

I'm not really sure of how to add in the index for t,w, h to obtain a value at every step. Any help would be greatly appreciated. Thanks!
 
Physics news on Phys.org
  • #2


Hello!

I took a look at your code and noticed a few things that could be causing issues. First, it seems like you are trying to use the Runge-Kutta Fehlberg method to solve the initial value problem, but your function only takes in the initial values and tolerance. You need to also include the function f(t,w) in your inputs.

Next, it seems like you are trying to output a table with the values at each step, but your function is only outputting the final w, t, and h values. You can try creating a loop inside your function that stores the values at each step and then outputs them at the end.

Lastly, I would recommend checking your implementation of the method itself to make sure it is correct. You can try testing it with a simpler problem first to make sure it is giving the correct results.

I hope this helps and good luck with your code!
 

1. What is the Runge-Kutta Fehlberg method?

The Runge-Kutta Fehlberg method, also known as the RKF method, is a numerical algorithm used for solving ordinary differential equations (ODEs). It is a popular method due to its high accuracy and efficiency in solving a wide range of ODEs.

2. How does the RKF method work?

The RKF method is based on a combination of the fourth-order Runge-Kutta method and the fifth-order Runge-Kutta method. It uses a predictor-corrector approach to estimate the solution of the ODE at each time step, with the corrector step being more accurate than the predictor step. This allows for a higher order of accuracy while still maintaining efficiency.

3. When should the RKF method be used?

The RKF method is best suited for solving ODEs that have a wide range of scales, as it can handle both stiff and non-stiff equations. It is also useful for solving ODEs with variable coefficients or discontinuities, as it can automatically adjust the step size to maintain accuracy.

4. What are the advantages of using the RKF method?

The RKF method has several advantages over other numerical methods for solving ODEs. These include high accuracy, efficiency, and stability. It also has the ability to handle a wide range of ODEs, making it a versatile tool for scientific computing.

5. Are there any limitations to the RKF method?

Like any numerical method, the RKF method has its limitations. It may not be suitable for solving ODEs with discontinuous solutions or those with very high oscillations. It also requires careful selection of the initial step size and tolerance to ensure accurate results. Additionally, the RKF method may not be the most efficient option for solving simple or low-order ODEs.

Similar threads

  • Calculus and Beyond Homework Help
Replies
14
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
Replies
1
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
1
Views
10K
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
2K
Back
Top