## Parameter Fit

## Main.ParameterFit History

Hide minor edits - Show changes to output

Changed line 76 from:

m.~~Obj~~(((ypred-ymeas)/ymeas)**2)

to:

m.Minimize(((ypred-ymeas)/ymeas)**2)

Changed lines 101-103 from:

(:sourceend:)

to:

(:sourceend:)

See also [[https://apmonitor.com/me575/index.php/Main/NonlinearRegression|Energy Price Nonlinear Regression]]

See also [[https://apmonitor.com/me575/index.php/Main/NonlinearRegression|Energy Price Nonlinear Regression]]

Changed line 7 from:

% MATLAB example

to:

% APM MATLAB example

Changed line 10 from:

# Python example

to:

# APM Python example

Added lines 12-14:

# Python Gekko example

m.options.IMODE = 2

Added lines 37-40:

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/3ZVRstDL9A4" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

(:htmlend:)

Changed lines 34-96 from:

to:

Once the base-case is converged, a select number of parameters can be set free for the optimization problem. The parameter is set to be fixed or calculated based on the DBS parameter ''STATUS''. A value of ''1'' indicates that the parameter is to be calculated by the optimizer while a value of ''0'' indicates that the parameter is to remained fixed.

!!! Example: Nonlinear Regression

%width=550px%Attach:gekko_imode2_regression.png

(:source lang=python:)

import numpy as np

from gekko import GEKKO

# load data

xm = np.array([18.3447,79.86538,85.09788,10.5211,44.4556, \

69.567,8.960,86.197,66.857,16.875, \

52.2697,93.917,24.35,5.118,25.126, \

34.037,61.4445,42.704,39.531,29.988])

ym = np.array([5.072,7.1588,7.263,4.255,6.282, \

6.9118,4.044,7.2595,6.898,4.8744, \

6.5179,7.3434,5.4316,3.38,5.464, \

5.90,6.80,6.193,6.070,5.737])

# define GEKKO model

m = GEKKO()

# parameters and variables

a = m.FV(value=0)

b = m.FV(value=0)

c = m.FV(value=0,lb=-100,ub=100)

x = m.Param(value=xm)

ymeas = m.Param(value=ym)

ypred = m.Var()

# parameter and variable options

a.STATUS = 1 # available to optimizer

b.STATUS = 1 # to minimize objective

c.STATUS = 1

# equation

m.Equation(ypred == a + b/x + c*m.log(x))

# objective

m.Obj(((ypred-ymeas)/ymeas)**2)

# application options

m.options.IMODE = 2 # regression mode

# solve

m.solve() # remote=False for local solve

# show final objective

print('Final SSE Objective: ' + str(m.options.objfcnval))

# print solution

print('Solution')

print('a = ' + str(a.value[0]))

print('b = ' + str(b.value[0]))

print('c = ' + str(c.value[0]))

# plot solution

import matplotlib.pyplot as plt

plt.figure(1)

plt.plot(x,ymeas,'ro')

plt.plot(x,ypred,'bx');

plt.xlabel('x')

plt.ylabel('y')

plt.legend(['Measured','Predicted'],loc='best')

plt.savefig('results.png')

plt.show()

(:sourceend:)

!!! Example: Nonlinear Regression

%width=550px%Attach:gekko_imode2_regression.png

(:source lang=python:)

import numpy as np

from gekko import GEKKO

# load data

xm = np.array([18.3447,79.86538,85.09788,10.5211,44.4556, \

69.567,8.960,86.197,66.857,16.875, \

52.2697,93.917,24.35,5.118,25.126, \

34.037,61.4445,42.704,39.531,29.988])

ym = np.array([5.072,7.1588,7.263,4.255,6.282, \

6.9118,4.044,7.2595,6.898,4.8744, \

6.5179,7.3434,5.4316,3.38,5.464, \

5.90,6.80,6.193,6.070,5.737])

# define GEKKO model

m = GEKKO()

# parameters and variables

a = m.FV(value=0)

b = m.FV(value=0)

c = m.FV(value=0,lb=-100,ub=100)

x = m.Param(value=xm)

ymeas = m.Param(value=ym)

ypred = m.Var()

# parameter and variable options

a.STATUS = 1 # available to optimizer

b.STATUS = 1 # to minimize objective

c.STATUS = 1

# equation

m.Equation(ypred == a + b/x + c*m.log(x))

# objective

m.Obj(((ypred-ymeas)/ymeas)**2)

# application options

m.options.IMODE = 2 # regression mode

# solve

m.solve() # remote=False for local solve

# show final objective

print('Final SSE Objective: ' + str(m.options.objfcnval))

# print solution

print('Solution')

print('a = ' + str(a.value[0]))

print('b = ' + str(b.value[0]))

print('c = ' + str(c.value[0]))

# plot solution

import matplotlib.pyplot as plt

plt.figure(1)

plt.plot(x,ymeas,'ro')

plt.plot(x,ypred,'bx');

plt.xlabel('x')

plt.ylabel('y')

plt.legend(['Measured','Predicted'],loc='best')

plt.savefig('results.png')

plt.show()

(:sourceend:)

Changed lines 5-6 from:

to:

apm.imode = 2

Changed lines 8-9 from:

apm_option(server,app,'~~nlc~~.imode',2);

to:

apm_option(server,app,'apm.imode',2);

Changed line 11 from:

apm_option(server,app,'~~nlc~~.imode',2)

to:

apm_option(server,app,'apm.imode',2)

Changed lines 5-9 from:

Parameter estimation with model parameter update

''objective =

to:

nlc.imode = 2

% MATLAB example

apm_option(server,app,'nlc.imode',2);

# Python example

apm_option(server,app,'nlc.imode',2)

Parameter estimation with model parameter update (MPU) is accomplished by solving a number of steady state conditions simultaneously. The MPU cases are selected from a number of steady state operating conditions. The objective is to minimize the difference between measured values and model states such as a squared error.

''objective = minimize (meas-model)'^2^'''

% MATLAB example

apm_option(server,app,'nlc.imode',2);

# Python example

apm_option(server,app,'nlc.imode',2)

Parameter estimation with model parameter update (MPU) is accomplished by solving a number of steady state conditions simultaneously. The MPU cases are selected from a number of steady state operating conditions. The objective is to minimize the difference between measured values and model states such as a squared error.

''objective = minimize (meas-model)'^2^'''

Changed lines 11-12 from:

to:

!!! CSV File Input

Measurements are brought into the optimization problem via a Comma Separate Value (CSV) file. The file must have the same name as the model file, but with the ''.csv'' extension.

The CSV file consists of columns of data for a unique measurement. The column headings are the global variable names. The rows of the CSV file are the measurement at various times that correspond to steady-state conditions. If the data is not at steady-state, dynamic parameter estimation should be used instead.

Measurements are brought into the optimization problem via a Comma Separate Value (CSV) file. The file must have the same name as the model file, but with the ''.csv'' extension.

The CSV file consists of columns of data for a unique measurement. The column headings are the global variable names. The rows of the CSV file are the measurement at various times that correspond to steady-state conditions. If the data is not at steady-state, dynamic parameter estimation should be used instead.

Added lines 21-28:

There is no limit to the number of data sets that may be included for MPU. The only limitation is the amount of computational resources that are available to run the problem.

!!! Parameters

All parameters are generally set to OFF as a first simulation. This creates a warm-start file ''mpu.t0'' and verifies that there are no spurious degrees of freedom.

Once the base-case is converged, a select number of parameters can be set free for the optimization problem. The parameter is set to be fixed or calculated based on the DBS parameter ''STATUS''. A value of ''1'' indicates that the parameter is to be calculated by the optimizer while a value of ''0'' indicates that the parameter is to remained fixed.

Changed line 13 from:

||border=1 width=~~80~~%

to:

||border=1 width=50%

Added lines 3-6:

The DBS file parameter ''imode'' is used to control the simulation mode. This option is set to ''2'' for model parameter update.

''NLC.imode = 2''

''NLC.imode = 2''

Changed lines 11-13 from:

''NLC.imode =

to:

Measurements are brought into the optimization problem via a Comma Separate Value (CSV) file. The CSV file consists of columns of data for a unique measurement. The rows of the CSV file are the measurement at various times that correspond to steady-state conditions. If the data is not at steady-state, dynamic parameter estimation should be used instead.

||border=1 width=80%

||! Measurement #1 ||! Measurement #2 ||! Measurement #3 ||

|| Time 1, Meas 1 || Time 1, Meas 2 || Time 1, Meas 3 ||

|| Time 2, Meas 1 || Time 2, Meas 2 || Time 2, Meas 3 ||

||border=1 width=80%

||! Measurement #1 ||! Measurement #2 ||! Measurement #3 ||

|| Time 1, Meas 1 || Time 1, Meas 2 || Time 1, Meas 3 ||

|| Time 2, Meas 1 || Time 2, Meas 2 || Time 2, Meas 3 ||

Changed lines 5-9 from:

''objective = minimize(meas-model)''

to:

''objective = minimize(meas-model)''

The DBS file parameter ''imode'' is used to control the simulation mode. This option is set to ''2'' for model parameter update.

''NLC.imode = 2''

The DBS file parameter ''imode'' is used to control the simulation mode. This option is set to ''2'' for model parameter update.

''NLC.imode = 2''

Added lines 1-5:

!! Parameter Estimation

Parameter estimation with model parameter update (MPU) is accomplished by solving a number of steady state conditions simultaneously. The MPU cases are selected from a number of steady state operating conditions. The objective is to minimize the difference between measured values and model states.

''objective = minimize(meas-model)''

Parameter estimation with model parameter update (MPU) is accomplished by solving a number of steady state conditions simultaneously. The MPU cases are selected from a number of steady state operating conditions. The objective is to minimize the difference between measured values and model states.

''objective = minimize(meas-model)''