## Main.DynamicEstimation History

January 15, 2021, at 04:00 AM by 10.35.117.248 -
plt.show()
(:sourceend:)
(:divend:)

The approach can also be extended to multiple data sets and when the experimental values are at different times.

(:toggle hide solution3b button show="Show GEKKO Solution":)
(:div id=solution3b:)

%width=550px%Attach:estimation3b.png

(:source lang=python:)
from gekko import GEKKO
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# data set 1
t_data1 = [0.0,  0.1,  0.2, 0.4, 0.8, 1.00]
x_data1 = [2.0,  1.6,  1.2, 0.7, 0.3, 0.15]

# data set 2
t_data2 = [0.0,  0.15, 0.25, 0.45, 0.85, 0.95]
x_data2 = [3.6,  2.25, 1.75, 1.00, 0.35, 0.20]

# combine with dataframe join
data1 = pd.DataFrame({'Time':t_data1,'x1':x_data1})
data2 = pd.DataFrame({'Time':t_data2,'x2':x_data2})
data1.set_index('Time', inplace=True)
data2.set_index('Time', inplace=True)
data = data1.join(data2,how='outer')

# indicate which points are measured
z1 = (data['x1']==data['x1']).astype(int) # 0 if NaN
z2 = (data['x2']==data['x2']).astype(int) # 1 if number

# replace NaN with any number (0)
data.fillna(0,inplace=True)

m = GEKKO(remote=False)

# measurements
xm = m.Array(m.Param,2)
xm.value = data['x1'].values
xm.value = data['x2'].values

# index for objective (0=not measured, 1=measured)
zm = m.Array(m.Param,2)
zm.value=z1
zm.value=z2

m.time = data.index
x = m.Array(m.Var,2)                  # fit to measurement
x.value=x_data1; x.value=x_data2

k = m.FV(); k.STATUS = 1              # adjustable parameter
for i in range(2):
m.free_initial(x[i])              # calculate initial condition
m.Equation(x[i].dt()== -k * x[i])  # differential equations
m.Minimize(zm[i]*(x[i]-xm[i])**2)  # objectives

m.options.IMODE = 5  # dynamic estimation
m.options.NODES = 2  # collocation nodes
m.solve(disp=True)    # solve
k = k.value
print('k = '+str(k))

# plot solution
plt.plot(m.time,x.value,'b.--',label='Predicted 1')
plt.plot(m.time,x.value,'r.--',label='Predicted 2')
plt.plot(t_data1,x_data1,'bx',label='Measured 1')
plt.plot(t_data2,x_data2,'rx',label='Measured 2')
plt.legend(); plt.xlabel('Time'); plt.ylabel('Value')
plt.xlabel('Time');
June 06, 2020, at 02:02 PM by 136.36.211.159 -
Changed lines 94-95 from:
x_data = [2.0,1.6,1.2,0.7,0.3,0.15,0.1,\

0.05,0.03,0.02,0.015,0.01]
to:
x_data = [2.0,1.6,1.2,0.7,0.3,0.15,0.1,0.05,0.03,0.02,0.015,0.01]
Changed lines 97-101 from:
Use an initial condition of {x=2} that matches the data.
to:
Use an initial condition of {x=2} that matches the data. Create new states {y=dx/dt} and {z=dy/dt} for the [[https://apmonitor.com/wiki/index.php/Apps/2ndOrderDifferential|higher order derivative terms]].

{$\frac{dx}{dt} = y$}
{$\frac{dy}{dt} = z$}
{$\frac{dz}{dt} = az+by+cx+d$}
June 06, 2020, at 01:57 PM by 136.36.211.159 -
plt.legend()
plt.xlabel('Time'), plt.ylabel('Value')
plt.show()
(:sourceend:)
(:divend:)

!!!!Dynamic Parameter Estimation Example 4

Estimate the parameter {a,b,c,d} in the differential equation:

{$\frac{d^3x}{dt^3} = a\frac{d^2x}{dt^2}+b\frac{dx}{dt}+c x+d$}

by minimizing the error between the predicted and measured {x} values. The {x} values are measured at the following time intervals.

(:source lang=python:)
t_data = [0,0.1,0.2,0.4,0.8,1,1.5,2,2.5,3,3.5,4]
x_data = [2.0,1.6,1.2,0.7,0.3,0.15,0.1,\
0.05,0.03,0.02,0.015,0.01]
(:sourceend:)

Use an initial condition of {x=2} that matches the data.

(:toggle hide solution4 button show="Show GEKKO Solution":)
(:div id=solution4:)

%width=550px%Attach:estimation4.png

(:source lang=python:)
from gekko import GEKKO

t_data = [0,0.1,0.2,0.4,0.8,1,1.5,2,2.5,3,3.5,4]
x_data = [2.0,1.6,1.2,0.7,0.3,0.15,0.1,\
0.05,0.03,0.02,0.015,0.01]

m = GEKKO()
m.time = t_data

# states
x = m.CV(value=x_data); x.FSTATUS = 1  # fit to measurement
y,z = m.Array(m.Var,2,value=0)

a,b,c,d = m.Array(m.FV,4)
a.STATUS=1; b.STATUS=1; c.STATUS=1; d.STATUS=1

# differential equation
#      Original:  x''' = a*x'' + b x' + c x + d
#      Transform: y = x'
#                z = y'
#                z' = a*z + b*y + c*x + d
m.Equations([y==x.dt(),z==y.dt()])
m.Equation(z.dt()==a*z+b*y+c*x+d) # differential equation

m.options.IMODE = 5  # dynamic estimation
m.options.NODES = 3  # collocation nodes
m.solve(disp=False)  # display solver output
print(a.value,b.value,c.value,d.value)

import matplotlib.pyplot as plt  # plot solution
plt.plot(m.time,x.value,'bo',label='Predicted')
plt.plot(m.time,x_data,'rx',label='Measured')
March 10, 2020, at 01:30 PM by 136.36.211.159 -
March 10, 2020, at 01:29 PM by 136.36.211.159 -
Changed lines 37-38 from:
{$\frac{x}{dt} = -k\,x$}
to:
{$\frac{dx}{dt} = -k\,x$}
Changed line 46 from:
Use an initial condition of {x=2} that matches the data. Verify the solution of {x} with the analytic expression {x(t)=2 exp(-k\,t)}.
to:
Use an initial condition of {x=2} that matches the data. Verify the solution of {x} with the analytic expression {x(t)=2 exp(-k t)}.
March 10, 2020, at 01:28 PM by 136.36.211.159 -
Changed lines 39-40 from:
by matching the predicted {x} to the measured {x} data:
to:
by minimizing the error between the predicted and measured {x} values. The {x} values are measured at the following time intervals.
Changed lines 42-43 from:
time = [0, 0.1, 0.2, 0.4, 0.8, 1]
x_data = [2.0081,  1.5512,  1.1903, 0.7160,  0.2562,  0.1495]
to:
t_data = [0, 0.1, 0.2, 0.4, 0.8, 1]
x_data = [2.0,  1.6,  1.2, 0.7,  0.3,  0.15]
Changed lines 46-50 from:
Verify the solution of {x} with the analytic expression {x(t)=exp(-k\,t)}.

to:
Use an initial condition of {x=2} that matches the data. Verify the solution of {x} with the analytic expression {x(t)=2 exp(-k\,t)}.

(:toggle hide solution3 button show="Show GEKKO Solution":)
(:div id=solution3:)

%width=550px%Attach:estimation3.png

(:source lang=python:)
from gekko import GEKKO

t_data = [0, 0.1, 0.2, 0.4, 0.8, 1]
x_data = [2.0,  1.6,  1.2, 0.7,  0.3,  0.15]

m = GEKKO(remote=False)
m.time = t_data
x = m.CV(value=x_data); x.FSTATUS = 1  # fit to measurement
k = m.FV(); k.STATUS = 1              # adjustable parameter
m.Equation(x.dt()== -k * x)            # differential equation

m.options.IMODE = 5  # dynamic estimation
m.options.NODES = 5  # collocation nodes
m.solve(disp=False)  # display solver output
k = k.value

import numpy as np
import matplotlib.pyplot as plt  # plot solution
plt.plot(m.time,x.value,'bo',\
label='Predicted (k='+str(np.round(k,2))+')')
plt.plot(m.time,x_data,'rx',label='Measured')
# plot exact solution
t = np.linspace(0,1); xe = 2*np.exp(-k*t)
plt.plot(t,xe,'k:',label='Exact Solution')
plt.legend()
plt.xlabel('Time'), plt.ylabel('Value')
plt.show()
(:sourceend:)
(:divend:)
March 10, 2020, at 01:12 PM by 136.36.211.159 -

!!!!Dynamic Parameter Estimation Example 3

Estimate the parameter {k} in the exponential decay equation:

{$\frac{x}{dt} = -k\,x$}

by matching the predicted {x} to the measured {x} data:

(:source lang=python:)
time = [0, 0.1, 0.2, 0.4, 0.8, 1]
x_data = [2.0081,  1.5512,  1.1903, 0.7160,  0.2562,  0.1495]
(:sourceend:)

Verify the solution of {x} with the analytic expression {x(t)=exp(-k\,t)}.

January 04, 2017, at 05:57 PM by 10.10.144.119 -
Changed lines 17-23 from:
!!!!Dynamic Parameter Estimation Example
to:
!!!!Dynamic Parameter Estimation Example 1

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/eXco8_3MmjI" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

!!!!Dynamic Parameter Estimation Example 2
April 04, 2015, at 02:03 PM by 45.56.12.124 -
Changed line 21 from:
* [[Attach:estimate_parameters.zip|Estimate parameters in Excel, MATLAB, Python, and Simulink]]
to:
April 03, 2015, at 01:39 AM by 10.24.17.95 -

* [[Attach:Intro_Dynamic_Esimation.pdf|Introduction to Dynamic Estimation (pdf)]]
April 02, 2015, at 04:03 PM by 45.56.12.124 -
Changed line 22 from:
<iframe width="560" height="315" src="https://www.youtube.com/embed/y0ERNz5Kms8?rel=0" frameborder="0" allowfullscreen></iframe>
to:
<iframe width="560" height="315" src="https://www.youtube.com/embed/umxAfu44kWo?rel=0" frameborder="0" allowfullscreen></iframe>
April 02, 2015, at 03:37 PM by 45.56.12.124 -
Changed line 15 from:
!!!!Numerical Solution Tutorial for Dynamic Parameter Estimation
to:
!!!!Dynamic Parameter Estimation Example
April 02, 2015, at 03:37 PM by 45.56.12.124 -
Changed line 15 from:
!!!!Numerical Estimation Introduction
to:
!!!!Numerical Solution Tutorial for Dynamic Parameter Estimation
April 02, 2015, at 03:30 PM by 45.56.12.124 -

!!!!Numerical Estimation Introduction

A method to solve dynamic estimation is by numerically integrating the dynamic model at discrete time intervals, much like measuring a physical system at particular time points. The numerical solution is compared to measured values and the difference is minimized by adjusting parameters in the model. Excel, MATLAB, Python, and Simulink are used in the following example to both solve the differential equations that describe the velocity of a vehicle as well as minimize an objective function.

* [[Attach:estimate_parameters.zip|Estimate parameters in Excel, MATLAB, Python, and Simulink]]

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/y0ERNz5Kms8?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

April 02, 2015, at 06:09 AM by 45.56.12.124 -

(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/UTxYp0VTe-E?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)
Changed line 1 from:
(:title Estimation Introduction:)
to:
(:title Dynamic Estimation Introduction:)