## Solve 2nd Order Differential Equations

## Apps.2ndOrderDifferential History

Hide minor edits - Show changes to output

Added lines 39-62:

(:toggle hide gekko button show="Show GEKKO (Python) Code":)

(:div id=gekko:)

(:source lang=python:)

import numpy as np

from gekko import GEKKO

import matplotlib.pyplot as plt

m = GEKKO(remote=False)

m.time = np.arange(0,2.01,0.05)

K = 30

y = m.Var(2.0)

z = m.Var(-1.0)

t = m.Var(0.0)

m.Equation(t.dt()==1)

m.Equation(y.dt()==z)

m.Equation(z.dt()+(0.9+0.7*t)*z+K*y==0)

m.options.IMODE = 4; m.options.NODES = 3

m.solve(disp=False)

plt.plot(m.time,y.value,'r-',label='y')

plt.plot(m.time,z.value,'b--',label='z')

plt.legend()

plt.xlabel('Time')

plt.show()

(:sourceend:)

(:divend:)

(:div id=gekko:)

(:source lang=python:)

import numpy as np

from gekko import GEKKO

import matplotlib.pyplot as plt

m = GEKKO(remote=False)

m.time = np.arange(0,2.01,0.05)

K = 30

y = m.Var(2.0)

z = m.Var(-1.0)

t = m.Var(0.0)

m.Equation(t.dt()==1)

m.Equation(y.dt()==z)

m.Equation(z.dt()+(0.9+0.7*t)*z+K*y==0)

m.options.IMODE = 4; m.options.NODES = 3

m.solve(disp=False)

plt.plot(m.time,y.value,'r-',label='y')

plt.plot(m.time,z.value,'b--',label='z')

plt.legend()

plt.xlabel('Time')

plt.show()

(:sourceend:)

(:divend:)

Changed lines 17-18 from:

to:

'''Second Order ODE Example'''

Changed line 27 from:

to:

'''Modified Form for Solution'''

Changed lines 17-18 from:

to:

**Second Order ODE Example**

An example of a second-order differential equation with constant {`K`} and variable {`y`} with first derivative {`y'`} and second derivative {`y''`}:

An example of a second-order differential equation with constant {`K`} and variable {`y`} with first derivative {`y'`} and second derivative {`y''`}:

Added lines 26-27:

**Modified Form for Solution**

Changed lines 33-36 from:

A numerical solution to this equation can be computed with a variety of different solvers and programming environments. Solution files are available in MATLAB, Python, and Julia below or through a web-interface. ~~Each ~~of these example problems can be~~ easily~~ modified for solutions to other second-order differential equations as well.

to:

A numerical solution to this equation can be computed with a variety of different solvers and programming environments. Solution files are available in MATLAB, Python, and Julia below or through a web-interface.

Each of these example problems can be modified for solutions to other second-order differential equations as well.

Each of these example problems can be modified for solutions to other second-order differential equations as well.

Changed lines 17-23 from:

Below is an example of a second-order differential equation~~.~~

-> Attach:2nd_order_ode.png

To numerically solve a differential equation with ~~higher-order terms, it can be broken into multiple first-order differential equations as shown below.~~

-> Attach:~~2nd_~~order~~_ode_modified~~.~~png~~

-> Attach:2nd_order_ode.png

To numerically solve a differential equation

-> Attach

to:

Below is an example of a second-order differential equation with constant {`K`} and variable {`y`} with first derivative {`y'`} and second derivative {`y''`}:

{$y''+(0.9+0.7t)y' + K\,y = 0$}

and with initial conditions:

{$y(0)=2 \quad y'(0)=-1$}

To numerically solve a differential equation with higher-order (such as 2nd derivative) terms, it can be broken into multiple first-order differential equations by declaring a new variable {`z`} and equation {`z=y'`}. The modified problem is then:

{$z'+(0.9+0.7t)z + K\,y = 0$}

and with initial conditions:

{$y(0)=2 \quad z(0)=-1$}

{$y''+(0.9+0.7t)y' + K\,y = 0$}

and with initial conditions:

{$y(0)=2 \quad y'(0)=-1$}

To numerically solve a differential equation with higher-order (such as 2nd derivative) terms, it can be broken into multiple first-order differential equations by declaring a new variable {`z`} and equation {`z=y'`}. The modified problem is then:

{$z'+(0.9+0.7t)z + K\,y = 0$}

and with initial conditions:

{$y(0)=2 \quad z(0)=-1$}

Changed lines 32-34 from:

Another scenario is when the damping coefficient ''c = (0.9 + 0.7 t)'' is not known but must be estimated from data. The value of ''c'' is allowed to change every 0.5 seconds. The true and estimated values of ''c'' are shown on the plot below. Predicted and actual values of ''y'' ~~give excellent~~ agreement.

Attach:2nd_order_differential_estimated.~~png~~

Attach:2nd_order_differential_estimated.

to:

Another scenario is when the damping coefficient ''c = (0.9 + 0.7 t)'' is not known but must be estimated from data. The value of ''c'' is allowed to change every 0.5 seconds. The true and estimated values of ''c'' are shown on the plot below. Predicted and actual values of ''y'' are in agreement even though the estimate is not continuous but only changes at discrete time points.

Attach:2nd_order_differential_estimated.png

Attach:download.png [[Attach:2nd_order_estimation_Matlab.zip|Solution to 2nd-Order Differential Estimation in Matlab]]

Attach:download.png [[Attach:2nd_order_estimation_Python.zip|Solution to 2nd-Order Differential Estimation in Python]]

Attach:2nd_order_differential_estimated.png

Attach:download.png [[Attach:2nd_order_estimation_Matlab.zip|Solution to 2nd-Order Differential Estimation in Matlab]]

Attach:download.png [[Attach:2nd_order_estimation_Python.zip|Solution to 2nd-Order Differential Estimation in Python]]

Changed lines 30-34 from:

Attach:download.png [[https://apmonitor.com/online/view_pass.php?f=2ode.apm|Solution to 2nd-Order Differential Equation in a Web Browser]]

to:

Attach:download.png [[https://apmonitor.com/online/view_pass.php?f=2ode.apm|Solution to 2nd-Order Differential Equation in a Web Browser]]

Another scenario is when the damping coefficient ''c = (0.9 + 0.7 t)'' is not known but must be estimated from data. The value of ''c'' is allowed to change every 0.5 seconds. The true and estimated values of ''c'' are shown on the plot below. Predicted and actual values of ''y'' give excellent agreement.

Attach:2nd_order_differential_estimated.png

Another scenario is when the damping coefficient ''c = (0.9 + 0.7 t)'' is not known but must be estimated from data. The value of ''c'' is allowed to change every 0.5 seconds. The true and estimated values of ''c'' are shown on the plot below. Predicted and actual values of ''y'' give excellent agreement.

Attach:2nd_order_differential_estimated.png

Changed line 1 from:

(:title Solve 2nd Order ~~ODE~~:)

to:

(:title Solve 2nd Order Differential Equations:)

Added lines 12-15:

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/JLtRK4Z1tEg" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

Changed line 19 from:

-> Attach:2nd_order_ode.png

to:

-> Attach:2nd_order_ode_modified.png

Changed lines 17-19 from:

Attach:download.png [[Attach:2nd_order_ode_Julia.zip|Solution to 2nd-Order Differential Equation in Julia]]

to:

To numerically solve a differential equation with higher-order terms, it can be broken into multiple first-order differential equations as shown below.

-> Attach:2nd_order_ode.png

A numerical solution to this equation can be computed with a variety of different solvers and programming environments. Solution files are available in MATLAB, Python, and Julia below or through a web-interface. Each of these example problems can be easily modified for solutions to other second-order differential equations as well.

-> Attach:2nd_order_ode.png

A numerical solution to this equation can be computed with a variety of different solvers and programming environments. Solution files are available in MATLAB, Python, and Julia below or through a web-interface. Each of these example problems can be easily modified for solutions to other second-order differential equations as well.

Added line 25:

Attach:download.png [[Attach:2nd_order_ode_Julia.zip|Solution to 2nd-Order Differential Equation in Julia]]

Changed line 15 from:

Attach:2nd_order_ode.png

to:

-> Attach:2nd_order_ode.png

Added lines 5-22:

A differential equation relates some function with the derivatives of the function. Functions typically represent physical quantities and the derivatives represent a rate of change. The differential equation defines a relationship between the quantity and the derivative. Differential equations are very common in fields such as biology, engineering, economics, and physics.

Attach:2nd_order_ode_solution.png

Differential equations may be studied from several different perspectives. Only simple differential equations are solvable by explicit formulas while more complex systems are typically solved with numerical methods. Numerical methods have been developed to determine solutions with a given degree of accuracy.

The term with highest number of derivatives describes the order of the differential equation. A first-order differential equation only contains single derivatives. A second-order differential equation has at least one term with a double derivative. Higher order differential equations are also possible.

Below is an example of a second-order differential equation.

Attach:2nd_order_ode.png

A numerical solution to this equation can be computed with a variety of different solvers and programming environments. Solution files are available in Julia, MATLAB, and Python below or through a web-interface. Each of these example problems can be easily modified for solutions to other differential equations as well.

Attach:download.png [[Attach:2nd_order_ode_Julia.zip|Solution to 2nd-Order Differential Equation in Julia]]

Attach:download.png [[Attach:2nd_order_ode_Matlab.zip|Solution to 2nd-Order Differential Equation in Matlab]]

Attach:download.png [[Attach:2nd_order_ode_Python.zip|Solution to 2nd-Order Differential Equation in Python]]

Attach:download.png [[https://apmonitor.com/online/view_pass.php?f=2ode.apm|Solution to 2nd-Order Differential Equation in a Web Browser]]

Attach:2nd_order_ode_solution.png

Differential equations may be studied from several different perspectives. Only simple differential equations are solvable by explicit formulas while more complex systems are typically solved with numerical methods. Numerical methods have been developed to determine solutions with a given degree of accuracy.

The term with highest number of derivatives describes the order of the differential equation. A first-order differential equation only contains single derivatives. A second-order differential equation has at least one term with a double derivative. Higher order differential equations are also possible.

Below is an example of a second-order differential equation.

Attach:2nd_order_ode.png

A numerical solution to this equation can be computed with a variety of different solvers and programming environments. Solution files are available in Julia, MATLAB, and Python below or through a web-interface. Each of these example problems can be easily modified for solutions to other differential equations as well.

Attach:download.png [[Attach:2nd_order_ode_Julia.zip|Solution to 2nd-Order Differential Equation in Julia]]

Attach:download.png [[Attach:2nd_order_ode_Matlab.zip|Solution to 2nd-Order Differential Equation in Matlab]]

Attach:download.png [[Attach:2nd_order_ode_Python.zip|Solution to 2nd-Order Differential Equation in Python]]

Attach:download.png [[https://apmonitor.com/online/view_pass.php?f=2ode.apm|Solution to 2nd-Order Differential Equation in a Web Browser]]

Added lines 1-4:

(:title Solve 2nd Order ODE:)

(:keywords nonlinear, linear, differential equation, ODE:)

(:description APMonitor Documentation: Simulation, optimization, estimation, and control of differential equations with solutions in Python, MATLAB, and Julia:)

(:keywords nonlinear, linear, differential equation, ODE:)

(:description APMonitor Documentation: Simulation, optimization, estimation, and control of differential equations with solutions in Python, MATLAB, and Julia:)