# Question (Leap Frog Method, implicit Euler's Method, etc)

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello I wanted to know that how I can implement Leap Frog Method, implicit Euler's Method and Runge-Kutta fourth-Order Method). I found this code:
double* ImplicitEuler (double theta0, double dtheta0, double h, int n)
{
int c = 1;
const int maxIterations = 32;
double* theta = new double[n];

for (int i = 0;i < n; i++){

double theta1 = theta0;

for (int j = 0; j < maxIterations; j++){
double g = theta1 +h * h * c * sin(theta1) - theta0 - h * dtheta0;
double gder = 1.0 + h * h * c * cos(theta1);
theta1 -= g/gder;
}//end for inner

double dtheta1 = dtheta0 - h * c * sin(theta1);
theta = theta1;
theta0 = theta1;
dtheta0 = dtheta1;

}//end for outer

return theta;
}


The above code is implicit Euler but I couldn’t figure out where I have to apply the 3D vector forces? Do any of you know? Apparently Leap Frog is a Verlet method that considers Velocity and for my simulation I need the velocity to calculate the frictional forces (it is important). Is it possible to adopt the Velocity less verlet to approximate the velocity in each time step ? Or if it is not possible could you please help me with Leap Frog? The Verlet method that I use is:
	Vector x1 = force * oneOverMass * dt2;
vel= (pos - m_prevPos) + x1;
vel.y-=9.81f*dt2;
m_prevPos = pos;
pos+=vel;


vel in here is not the real velocity it is the change of position. I just use this variable because I declare it somewhere else. Thank you in advance

##### Share on other sites
The first example doesn't really resemble implicit Euler, and isn't a general purpose solver. I would say it is a poorly named method, at least, without the context of its application. Where did you get it? I don't see that it is suitable for what you want to use it for.

Also, I do not recommend Leap Frog. Its stability region makes it a poor choice for general simulation.

##### Share on other sites
Quote:
 Original post by grhodes_at_workThe first example doesn't really resemble implicit Euler, and isn't a general purpose solver. I would say it is a poorly named method, at least, without the context of its application. Where did you get it? I don't see that it is suitable for what you want to use it for.

What do you define as "implicit Euler's method"? The OP's code appears to be for a simple pendulum with friction at the joint.

##### Share on other sites
The implicit euler method is that x(t+Δt) = x(t) + x'(t+Δt)Δt. In other words, the derivative used as a uniform d/dt for the timestep is taken from the end of the timestep.

##### Share on other sites
Quote:
 Original post by grhodes_at_workThe first example doesn't really resemble implicit Euler, and isn't a general purpose solver. I would say it is a poorly named method, at least, without the context of its application. Where did you get it? I don't see that it is suitable for what you want to use it for.Also, I do not recommend Leap Frog. Its stability region makes it a poor choice for general simulation.

Yes, you are correct the function I posted only draw a Sin diagram to show how stable this solver is.
I got it from GAME PHYSICS book.

How about my question about Velocity Verlet ? I need the velocity to do the friction, dont I?

Could some one explain a little bit more.

##### Share on other sites
Quote:
 Original post by SneftelThe implicit euler method is that x(t+Δt) = x(t) + x'(t+Δt)Δt. In other words, the derivative used as a uniform d/dt for the timestep is taken from the end of the timestep.

Okay, so the code posted by bargasteh is an implementation of the implicit Euler method for the simple pendulum with friction at the joint. If dx/dt = f(t,x), then you use the approximation dx/dt = (x(t+h) - x(t))/h and the numerical method (x(t+h) - x(t))/h = f(t,x(t+h)), or rewritten x(t+h) = x(t) + h*f(t,x(t+h)). If you define z = x(t+h) and g(z) = z - x(t) - h*f(t,z), knowing t, h, and x(t), you need to solve the equation g(z) = 0 for z. The inner loop of what bargasteh posted is Newton's method applied to g(z) = 0. The numerically computed z is what you use for x(t+h).

##### Share on other sites
Quote:
 Original post by Anonymous PosterOkay, so the code posted by bargasteh is an implementation of the implicit Euler method for the simple pendulum with friction at the joint.

True implicit euler uses an exact solution for computing the derivative at the end of the timestep, rather than approaching it via Newton's Method. (Because of this, it has no general closed-form solution.) If you're going to do several iterations per timestep, you may as well do Runge-Kutta, which'll give you a lower error bound.

##### Share on other sites
Quote:
 Original post by SneftelTrue implicit euler uses an exact solution for computing the derivative at the end of the timestep, rather than approaching it via Newton's Method.(Because of this, it has no general closed-form solution.)

I think we agree on the definition for "implicit Euler", but the choices for implementing this on a computer are many. I do not understand what you mean by "uses an exact solution for computing the derivative". You do not know the exact solution to the differential equation, so how can you use it?

Quote:
 If you're going to do several iterations per timestep, you may as well do Runge-Kutta, which'll give you a lower error bound.

The stability regions in the complex plane are different between Runge-Kutta and Implicit Euler, so the effectiveness of one method versus the other can vary with nonlinear f(t,x).

##### Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
 Original post by grhodes_at_workThe first example doesn't really resemble implicit Euler, and isn't a general purpose solver. I would say it is a poorly named method, at least, without the context of its application. Where did you get it? I don't see that it is suitable for what you want to use it for.

What do you define as "implicit Euler's method"? The OP's code appears to be for a simple pendulum with friction at the joint.

Yes, but "simple pendulum with friction" != "implicit Euler"! The function solves a very specific problem, and isn't a generic solver as the function name implies. This was my point, :).

##### Share on other sites
Quote:
 Original post by Anonymous PosterI think we agree on the definition for "implicit Euler", but the choices for implementing this on a computer are many. I do not understand what you mean by "uses an exact solution for computing the derivative". You do not know the exact solution to the differential equation, so how can you use it?

Though there's no closed-form solution, you can exactly compute the derivative for certain situations, such as damped springs. Unfortunately, I don't have my notes here with me, or I'd post the derived equations.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 11
• 28
• 15
• 39
• ### Forum Statistics

• Total Topics
634838
• Total Posts
3019547
×