A list of puns related to "Runge–Kutta methods"
I am not familiar with implementing Runge-Kutta methods with code. I was wondering if there are any tutorials out there that go over how to implement Runge-Kutta methods in Julia to solve differential equations? Ideally, the tutorial will go over some examples (that is how I learn best).
The reason I ask is I would like to work on some projects that solve DEs using these methods and I also expect this to be a necessary skill to have for my future research.
I just finished teaching my Numerical Analysis class for the Fall. Grades are in, and so I can sit back for a bit.
I always feel that the class gets a bit rushed at the very end. I spend a lot of time on polynomial approximation when I teach the class, and then I view the rest of the class as applications of polynomial approximations. This includes Newton's method, Integration and Differentiation, and the solution to initial value problems.
I felt that my presentation to my students was a bit slap dash for IVPs. So I wrapped up Runge Kutta methods into this video here, if anyone is curious: https://youtu.be/t48a2M27kjM
If you taught a class this semester, how did it go for you? There was such a shuffle working around COVID and the possibility of shutting down all semester, and I feel it took longer than usual to hit a stride. Did you feel you rushed anything at the end?
If you are a student, how was the flip side of all of this?
I'm asking it out of curiosity. It doesn't need to have any pratical purpose. I know there are 14th order RK methods around, but I'm curious if someone developed a 100th or 1000th order method somewhere
In the wikipedia article for collocation methods (https://en.wikipedia.org/wiki/Collocation_method) it states
> All these collocation methods are in fact implicit Runge–Kutta methods. The coefficients ck in the Butcher tableau of a Runge–Kutta method are the collocation points. However, not all implicit Runge–Kutta methods are collocation methods.
What's an example of a Runge-Kutta method that's not a collocation method? Would it be an RK method where all the 'c' values in the Butcher tableau are 0?
I am modelling dM/dt=gamma*M x B (the simplified version of the Bloch equation) using Runge-Kutta integration and I am struggling to find initial values for B(t) =Bx, By, Bz and M(t)=Mx,My,Mz.
What I tried so far is defining B = (0, 0, B_0) and M=(0,0,1) as my initial values then B_0=1.5 Bxt=B1*cos(w*t), Byt=B1*sin(w*t) , Bzt=B_0, B1=1e-3/sqrt(2) , w=gamma*Bknot but my results do not seem accurate
Assuming my code is right, I need help understanding the physics. Has anyone tried to model this before? If I were to plot Mz vs Mx, and Mz vs My should I be getting the Larmor precession?
I need ideas because every website I find says something different and I am struggling to see how they all come together. Thank you all
Can someone who knows about this take a look at my codes? The values I am getting in the final table seem to be wrong.
Not sure how to upload a picture, but the function code looks as follows:
function Sol=RK4(a,b,alpha,f,N)
h=(b-a)/N;
t=a:h:b;
w=zeros(N+1,1);
w(1)=alpha;
for i=1:N
k1=h*f(t(i),w(i));
k2=h*f(t(i)+h/2,w(i)+k1/2);
k3=h*f(t(i)+h/2,w(i)+k2/2);
k4=h*f(t(i)+h,w(i)+k3);
w(i+1)=w(i)+(k1+2*k2+2*k3+k4)/6;
end
Sol=[t',w];
And the specific problem I am working on is:
a=0;
b=1;
alpha=9/5;
f=@(y,t) (t^7)*y^(8/5)-8*t^(7)*y;
N=10;
Sol=RK4(a,b,alpha,f,N)
Then the output looks like:
0 1.800000000000000
0.100000000000000 0.212985933198134
0.200000000000000 0.212983644715527
0.300000000000000 0.212979886602519
0.400000000000000 0.212974693027118
0.500000000000000 0.212968093926933
0.600000000000000 0.212960116556139
0.700000000000000 0.212950786270819
0.800000000000000 0.212940126995763
0.900000000000000 0.212928161529170
1.000000000000000 0.212914911754464
The jump down to 0.212985933198134 does not seem right here as I believe it should be closer to 1.8. Are there any mistakes I have made?
Thanks to anyone who is able to help.
I see a lot of Predictor correctors applied with adam-bashfort/adam moulton, but I feel RK methods on my PoV appear more suited for prediction correction. Just find one explicit and one implicit RK with the same C points. You can even use embedded methods to get an extra truncation error estimate
Is there any disadvantages I'm not aware about?
hey, guys I just finished my first year as an aerospace engineer and I started trying to learn numerical methods of solving PDEs and ODEs to get a bit ahead of the pack during the summer. and I found two methods that a lot of people seem to use in my field (rk4 and method of characteristics) I have no idea if this is the proper subreddit to ask this question but wish one is worth learning or should I learn both and are there like any pdfs or websites with examples to try on or something?
Thanks alot
Hi, firts of all I'm sorry if this isn't the right sub to post this, but I am sure someone here knows about basic computational methods for physics systems...
In order to complete an assignment for uni I decided to simulate (integrate) the dynamics of a triple pendulum, using methods learnt from previous classes.
Due to the lack of details on how to achieve this, I went brute force, solved the Lagrange equations for the angular accelerations and put those newfound forces in a runge kutta algorithm I wrote on c++.
Now, everything works fine, especially for low energy initial conditions... However for some reasons starting with higher values of the angles (aka higher initial energy) will cause the system's energy to diverge in mere seconds (it jumps 11 orders of magnitude), even if i previously learnt that runge kutta methods are known to keep it's average value constant.
The only way i came up to rectify that is raise the precision, hence the number of cycles the program does, and ultimately computation times up to 20-30ish minutes, and even in those cases it is not guaranteed that the energy remains stable.
I am missing something or triple pendulum is just an evil chaotic bih?
TL;DR: Runge kutta diverges energy, why is that? It's because the "chaoticness" of the system?
could anyone help me with a question i’m stuck on? very very new to coding
I was doing a research paper on the different methods. Among the Heuns method and the midpoint method which is considered more accurate and why?
I googled this but keep getting mixed answers. could someone explain to me why one is better than the other?
% pendul - Program to compute the motion of a simple pendulum
% using the Euler or Verlet method
clear all; help pendul % Clear the memory and print header
% Select the numerical method to use: Euler or Verlet
NumericalMethod = menu('Choose Numerical Method:', 'Euler', 'Verlet','Runge-Kutta');
% Set initial position and velocity of pendulum
theta0 = input('Enter initial angle (in degrees):');
r(1) = theta0 * pi / 180; % Convert angle to radians
v(1) = 0; % Set the inital velocity
state = [r(1) v(1)];
% Set the physical constants and other variables
g_over_L = 1; % The constant g/L
time = 0; % Initial time
irev = 0; % Used to count number of reversals
tau = input('Enter time step:');
% Take on backward step to start Verlet
accel = -g_over_L*sin(r(1)); % Gravitational Acceleration
theta_old = r(1) - v(1) * tau + 0.5 * tau^2 * accel;
% Loop over desired number of steps with given time step
% and numerical method
nstep = input('Enter number of time steps:');
for istep=1:nstep
%* Record angle and time for plotting
t_plot(istep) = time;
th_plot(istep) = r(1) * 180 / pi; %Convert angle to degrees
time = time + tau;
% Compute new position and velocity using
% Euler or Verlet method
accel = -g_over_L * sin(r(1)); % Gravitational Acceleration
if (NumericalMethod == 1)
theta_old = r(1); % Save previous angle
r(1) = r(1) + tau*v(1); %Euler Method
v(1) = v(1) + tau * accel;
elseif (NumericalMethod == 2)
theta_new = 2 * r(1) - theta_old + tau^2*accel;
theta_old = r(1); % Verlet Method
r(1) = theta_new;
else
state = rk4(state,time,tau,'pendrk',g_over_L);
r = [state(1)]; % 4th Order Runge-Kutta
v = [state(2)];
time = time + tau;
end
% Test if the pendulum has passed through theta = 0;
% If yes, use time to estimate period
if( r(1)*theta_old < 0) % Test position for sing change
if (irev == 0) % If this is the first change,
time_old = time; % Just record the time
else
period(irev) = 2 * (time - time_old);
time_old = time;
end
irev = irev + 1; % Increment the number of reversals
end
end
% Estimate period of oscillation, including error bar
%%ErrorBar = std(period) / sqrt(irev);
%fprintf('Average period = %g +/- %g\n', AvePeriod, ErrorBar);
% Graph the oscilations as theta versus time
plot(t_plot, th_plot, '+');
xlabel('Time'); ylabel('\
... keep reading on reddit ➡If a slope field has a bound of K = 5 on a large enough region containing the initial point y(1) = 1, find the error bound for estimating y(4) using n = 20 steps for each of the following methods:
Euler? Improved Euler? Runge-Kutta?
How many steps are needed to guarantee the Runge-Kutta method is accurate to within one billionth?
I am confused how to solve this without an equation
dydt=@(t,y) y*t^2-1.1*y;
[t,y]=rk4sys(dydt,[0 2],1,0.5)
disp([t.',y])
function [tp,yp]=rk4sys(dydt,tspan,y0,h,varargin)
if nargin<4
error('at least 4 input arguments required')
end
if any(diff(tspan)<=0)
error('tspan not in ascending order')
end
n=length(tspan);
ti=tspan(1);
tf=tspan(2);
if n==2
t=(ti:h:tf)';
n=length(t);
if t(n)<tf
t(n+1)=tf;
n=n+1;
end
else t=tspan;
end
tt= ti;
y(1,:)=y0;
np=1;
tp(np)=tt;
yp(np,:)=y(1,:);
i=1;
while(1)
tend=t(np+1);
hh=t(np+1);
if hh>h
hh=h;
end
while(1)
if tt+hh>tend
hh=tend-tt;
end
k1=dydt(tt,y(i,:),varargin{:})';
ymid=y(i,:)+k1.*hh./2;
k2=dydt(tt+hh/2,ymid,varargin{:})';
ymid=y(i,:)+k2*hh/2;
k3=dydt(tt+hh/2,ymid,varargin{:})';
yend=y(i,:)+k3*hh;
k4=dydt(tt+hh,yend,varargin{:})';
phi=(k1+2*(k2+k3)+k4)/6;
y((i+1),:)=y(i,:)+phi*hh;
tt=tt+hh;
i=i+1;
if tt>=tend
break
end
end
np=np+1;
tp(np)=tt;
yp(np,:)=y(i,:);
if tt>=tf
break
end
end
end
I'm trying to solve an ODE with the Runge-Kutta method. This one is a 4th order and can solve for a system of equations
when I press run I do get an answer but I get the t in a row vector and I want a column vector, the only way I can make it into a column vector is by transposing it outside the function. Is there a way to do it inside the function?
I am learning about Euler's method which is also called as 1st order Runge-Kutta method and Standard Runge-Kutta method(4th order) because of the recent paper in neural ode.
Euler's and RK4 method are some of the important ODE solvers which approximates a function given its derivative and a initial value. For simplicity and to understand these methods, I have taken a function with single variable.
Real = y=x^3+x^2
dy/dx = 3x^2+2x
y(0) = 0
h = 0.1
y(1) = ?
RK4 method apprximates to real function with a step size 0.1 but euler's need a step size of 0.01.
import matplotlib.pyplot as plt
def f(x,der=True):
if der==False:
return x*x*x+x*x # y = x^3+x^2
else:
return 3*x*x+2*x # y'= 3x^2+2x
x0 , y0, xn, h,=0 , 0, 1, 0.1 #initial x, initial y, end x, step size
x , y, y_real,y_rk, s=[], [], [], [],[]
r0=y0
while(x0<xn):
x.append(x0)
y.append(y0)
s.append(f(x0))
y_rk.append(r0)
#Real solution
y_real.append(f(x0,False))
#Ruge-Kutta Method
k1=h*f(x0)
k2=h*f(x0+0.5*h)
k3=h*f(x0+0.5*h)
k4=h*f(x0+h)
r0=r0+(k1+2*k2+2*k3+k4)/6
#Euler's Method
y0=y0+h*f(x0)
x0=x0+h
print(x0,y0,f(x0,False),r0)
plt.title('Approximating y = x^3+x^2 with step size='+str(h), fontsize=12)
plt.suptitle('dy/dx = 3x^2+2x', fontsize=10)
plt.plot(x,y_real,'b',label='Real Function')
plt.plot(x,y,'g--',label="Approximated by Euler's Method")
plt.plot(x,y_rk,'r--',label="Approximated by RK4 Method")
plt.legend()
plt.show()
I am solving Lorenz chaos equations and when I use the timestepping formula y(n+1) = y(n) + hk2, I get a completely different answer than when I use y(n+1) = y(n) + ½h*(k1+k2).
Surely they should produce the same results? Or at least very similar. Any ideas?
Thank you
Hello y'all, I've been trying to implement a generalized Runge-Kutta algorithm for ODEs of any order. I've only found literature discussing the use of Runge-Kutta for first- and second-order differential equations, but I don't know if that means there's an inherent limitation.
Ignoring Runge-Kutta for a moment: when solving ODEs, the theoretical step from first-order to second-order seems to be general—that is, you just essentially do the same thing to higher order ODEs. For example, whereas the second-order ODE y'' = f(x,y,y') might be written instead as y' = z and z' = f(x,y,z), the third-order ODE y''' = f(x,y,y',y'') might be written as y' = z, z' = w, and w' = f(x,y,z,w).
Anyway, the questions I'm trying to get to are these:
I'm somewhat new to the world of numerical analysis, and want to keep in best practices—lest the MATLAB program I wrote turn out to have a glaring theoretical issue
When you all took Diff Eq, did your professors sort of completely bypass the details of how this works? I was reminded of how little I actually know about this process when some friends of mine asked me to help them study. My professor told us “you won’t need to understand this stuff until Graduate School” when we were learning it and so we only took his word that when calculating error it was 4th order and that “things get weird after 5th order” which is why the approximations use 5th order (like ??). I mean it’s all fine and good that I know how and when to use the “ode45” command in MatLab, but it is admittedly frustrating to not have any context or deeper understanding of how I’m actually getting answers.
You don't have to ELI5, more like EL I'm a college student in advanced calculus that hasn't touched a single bit of differential equations and is asked to give a very brief explanation to what is the runge kutta method and how it works.
Until now, all I know is that it gives an approximation to the general solution of a differential equation (if this is even right), because we had to use it to compute a single differential equation at one point during a big project.
Basically, what is it? How does it work? When do I use it. This should be good enough to give a good enough answer to my teach.
Thanks!
Forewarning, I have posted the same thing at r/processing, in case they may help. But I feel like people over here will more likely help explain the maths behind RK4.
Hello!
As a project, I am comparing and contrasting the effectiveness (and efficiency, I guess) of methods for numerical analysis (Specifically Euler's Method and the Runge Kutta RK4 Method) and am a little stumped on the implementation of the equations of motion with the RK4. I decided to code this in Processing, as I already know Java and have experience with p5.js. Could some brave soul please either check my code to see if I'm using this correctly, or help explain how I would program the double pendulum with the RK4? I'm very lost.
What I have so far (below) is very messy, so please bear with me... In this, a1
and a2
represent the angles of the pendulum, av1
and av2
represent the angular velocities, & aa1
and aa2
represent the dy/dx of the angular velocities. Also, dt
represents the time per step:
float[] k1 = new float[6];
k1[0] = finda1(av1 + aa1 * dt, av2 + aa1 * dt, a1 + av1 * dt, a2 + av2 * dt);
k1[1] = finda2(av1 + aa1 * dt, av2 + aa1 * dt, a1 + av1 * dt, a2 + av2 * dt);
k1[2] = findAllElse(av1,k1[0],dt);
k1[3] = findAllElse(av2,k1[1],dt);
k1[4] = findAllElse(a1, k1[2],dt);
k1[5] = findAllElse(a2, k1[3],dt);
float[] kSetup = new float[4];
for(int i = 0; i<kSetup.length;i++){
kSetup[i] = prevVals[i] + (dt/2) * k1[i+2];
}
float[] k2 = new float[6];
k2[0] = finda1(kSetup[0], kSetup[1], kSetup[2], kSetup[3]);
k2[1] = finda2(kSetup[0], kSetup[1], kSetup[2], kSetup[3]);
for(int i = 2; i<k2.length;i++){
k2[i] = findAllElse(prevVals[i-2],k2[i-2],dt/2);
}
kSetup = new float[4];
for(int i = 0; i<kSetup.length;i++){
kSetup[i] = prevVals[i] + (dt/2) * k2[i+2];
}
float[] k3 = new float[6];
k3[0] = finda1(kSetup[0], kSetup[1], kSetup[2], kSetup[3]);
k3[1] = finda2(kSetup[0], kSetup[1], kSetup[2], kSetup[3]);
for(int i = 2; i<k2.length;i++){
k3[i] = findAllElse(prevVals[i-2],k3[i-2],dt/2);
}
kSetup = new float[4];
for(int i = 0; i<kSetup.length;i++){
kSetup[i] = prevVals[i] + dt * k3[i+2];
}
float[] k4 = new float[6];
k4[0] = finda1(kSetup[0], kSetup[1], kSetup[2], kSetup[3]);
k4[1] = finda2(kSetup[0],
... keep reading on reddit ➡Recently I did my first simulation that consists of a particle orbiting the origin (0,0), taking gravity variatons into account. I used for it matplotlib.pyplot, numpy and euler's integration method. Then I posted here that first situation and a very kind user recommended me I looked into other methods, runge-kutta and verlet.
Here's my code involving runge-kutta's method:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from itertools import count
#Physical variables
F = 6000 #G * M, abreviated.
#Position
r0 = 10#Starting distance
r_y = r0 #Updatable starting distance
r_x = 0 #Horizontal position
r_t = r_y - 35 #Earth's radius
r = np.array([r_x, r0])
r_norm = np.linalg.norm(r)
#Speed
v_x = 17 #Horizontal speed
v_y = 0 #Inicial speed
v = np.array([v_x, v_y])
mu = 0.05 #Air ressistance coefficient
dt = 1 #Time step
#Phyics f0unctions
def get_g(r, F = F):
return -F * np.linalg.norm(r)**-3 * r
def get_air_ressistance(v, mu=mu):
return mu * v
def get_acceleration(r, v, F=F):
return get_g(r) - get_air_ressistance(v)
def update_v(r, v):
acc = get_g(r)
return v + acc * dt
def update_r(r, v, h = dt):
#Runge kutta method
k1 = update_v(r, v)
k2 = update_v(r + 0.5 * dt, v + 0.5 * k1)
k3 = update_v(r + 0.5 * dt, v + 0.5 * k2)
k4 = update_v(r + 0.5 * dt, v + k3)
r = r + h*(k1 + 2*k2 + 2*k3 + k4)/6
return r
#Simulation
cont = 0
xdata, ydata = [], []
fig, ax = plt.subplots()
plt.axis([-2*r_norm, 2*r_norm, -2*r_norm, 2*r_norm])
plt.scatter(0,0)
ln, = plt.plot([], [], 'ro')
def init():
ax.set_xlim(-2*r_norm, 2*r_norm)
ax.set_ylim(-2*r_norm, 2*r_norm)
return ln,
j = np.array([r_x, r_y])
def update(frame):
global j
print(j)
j = update_r(j, v)
xdata.append(r[0])
ydata.append(r[1])
ln.set_data(xdata, ydata)
return ln,
def main(r = r, v = v):
while True:
v = update_v(r, v)
r = update_r(r, v)
plt.scatter(r[0], r[1], s = 1)
plt.pause(0.05)
ani = FuncAnimation(fig, func = update, interval = 10,
init_func = init, blit = True)
plt.
... keep reading on reddit ➡hey, guys I just finished my first year as an aerospace engineer and I started trying to learn numerical methods of solving PDEs and ODEs to get a bit ahead of the pack during the summer. and I found two methods that a lot of people seem to use in my field (rk4 and method of characteristics) I have no idea if this is the proper subreddit to ask this question but wish one is worth learning or should I learn both and are there like any pdfs or websites with examples to try on or something?
Thanks alot
I googled this but keep getting mixed answers. could someone explain to me why one is better than the other?
I googled this but keep getting mixed answers. could someone explain to me why one is better than the other?
Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.