# Th order implementation of Runge Kutta in AMPL language

**URL:** https://discuss.ampl.com/t/th-order-implementation-of-runge-kutta-in-ampl-language/489
**Category:** Support
**Tags:** modeling
**Created:** [April 11, 2023, 5:01pm UTC](https://discuss.ampl.com/t/th-order-implementation-of-runge-kutta-in-ampl-language/489 "2023-04-11T17:01:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Emad\_Abdullah](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/emad_abdullah/32/151_2.png) [@Emad\_Abdullah](https://discuss.ampl.com/u/Emad_Abdullah)
#### Post date: [April 11, 2023, 5:01pm UTC](https://discuss.ampl.com/t/th-order-implementation-of-runge-kutta-in-ampl-language/489/1 "2023-04-11T17:01:27Z")

</div>

Hi

i want to use 4th order implementation of Runge Kutta in AMPL language . where the second order is given

s.t. lambda\_x {i in 0…n-1} : x[i+1] = x[i] + 0.5_h_( fx[i]+fx[i+1]) ;

Kind Regards,  
Emad

---

<div class="post-metadata">

### Author: ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)
#### Post date: [April 13, 2023, 9:56pm UTC](https://discuss.ampl.com/t/th-order-implementation-of-runge-kutta-in-ampl-language/489/2 "2023-04-13T21:56:09Z")

</div>

Can you explain what _h_ is? The expression `h(fx[i]+fx[i+1])` suggests that _h_ is some kind of function, but AMPL doesn’t know about any functions having that name.

---

<div class="post-metadata">

### Author: ![marcos](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/marcos/32/154_2.png) [@marcos](https://discuss.ampl.com/u/marcos)
#### Post date: [April 15, 2023, 4:12pm UTC](https://discuss.ampl.com/t/th-order-implementation-of-runge-kutta-in-ampl-language/489/3 "2023-04-15T16:12:50Z")

</div>

Hi,

@4er I think h parameter is the step in Runge Kutta’s method.

@Emad_Abdullah how are you planning to implement Runge Kutta with AMPL? I guess you want AMPL to fix the values for every iteration (n steps), and you are using the x’s, fx’s and perhaps some y’s as variables, but which function are you going to minimize? Could you tell us more about your model?

---

<div class="post-metadata">

### Author: ![Emad\_Abdullah](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/emad_abdullah/32/151_2.png) [@Emad\_Abdullah](https://discuss.ampl.com/u/Emad_Abdullah)
#### Post date: [April 15, 2023, 7:09pm UTC](https://discuss.ampl.com/t/th-order-implementation-of-runge-kutta-in-ampl-language/489/4 "2023-04-15T19:09:55Z")

</div>

sorry  
“h” is actually is step size  
and the equation should be like the following  
s.t. lambda\_x {i in 0…n-1} : x[i+1] = x[i] + 0.5 \* h \* ( fx[i]+fx[i+1]) ;

---

<div class="post-metadata">

### Author: ![Emad\_Abdullah](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/emad_abdullah/32/151_2.png) [@Emad\_Abdullah](https://discuss.ampl.com/u/Emad_Abdullah)
#### Post date: [April 15, 2023, 7:13pm UTC](https://discuss.ampl.com/t/th-order-implementation-of-runge-kutta-in-ampl-language/489/5 "2023-04-15T19:13:31Z")

</div>

I solving an optimal control problem using IPOPT  
and the x equation is like the following  
var fx {i in 0…N} = -lambda \* x[i] + w \* (x2[i]-myu) \* H[i] ;  
where the second order is given  
s.t. lambda\_x {i in 0…n-1} : x[i+1] = x[i] + 0.5_h_( fx[i]+fx[i+1]) ;

---

<div class="post-metadata">

### Author: ![4er](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.ampl.com/4er/32/530_2.png) [@4er](https://discuss.ampl.com/u/4er)
#### Post date: [April 16, 2023, 6:03pm UTC](https://discuss.ampl.com/t/th-order-implementation-of-runge-kutta-in-ampl-language/489/6 "2023-04-16T18:03:54Z")

</div>

You can write these AMPL statements:

```auto
param h;
var x {i in 1..N};
var fx {i in 0..N} = -lambda*x[i] + w*(x2[i]-myu)*H[i] ;
s.t. lambda_x {i in 0..n-1}: x[i+1] = x[i] + 0.5*h*( fx[i]+fx[i+1]) ;

```

However, to make this work in AMPL, you first have to define lambda, w, x2, myu, and H. Which of these are variables (to be optimized by the solver) and which are parameters (given as data)?

As @marcos has mentioned, if this is to be an optimal control problem, then you will also need to give an expression for the objective function in a `minimize` or `maximize` statement.
