Cubic Spline (cspline) Object
Main.ObjectCspline History
Show minor edits - Show changes to output
Changed lines 31-37 from:
!!!! [[https://gekko.readthedocs.io/en/latest/|GEKKO Python]] Example
Sample the function {`3\sin(x)-(x-3)`} at 50 random points between 5 and 15. Use the randomly sampled points to construct a cubic spline and find the minimum of that function.
(:toggle hide gekko button show="Example GEKKO (Python) Code":)
(:div id=gekko:)
(:toggle hide gekko button show="Example GEKKO (Python) Code":)
(:div id=gekko
to:
!!!! [[https://apmonitor.com/wiki/index.php/Main/PythonApp|APM Python]] Example
Use the following x and y data to construct a cubic spline.
x_data , y_data
-1.0000000e+00 , 3.8461538e-02
-8.0000000e-01 , 5.8823529e-02
-5.0000000e-01 , 1.3793103e-01
-2.5000000e-01 , 3.9024390e-01
0.0000000e+00 , 1.0000000e+00
1.0000000e-01 , 8.0000000e-01
2.0000000e-01 , 5.0000000e-01
5.0000000e-01 , 1.3793103e-01
Find the maximum of the interpolated function.
(:toggle hide apm button show="Example APMonitor (Python) Source":)
(:div id=apm:)
Use the following x and y data to construct a cubic spline.
x_data , y_data
-1.0000000e+00 , 3.8461538e-02
-8.0000000e-01 , 5.8823529e-02
-5.0000000e-01 , 1.3793103e-01
-2.5000000e-01 , 3.9024390e-01
0.0000000e+00 , 1.0000000e+00
1.0000000e-01 , 8.0000000e-01
2.0000000e-01 , 5.0000000e-01
5.0000000e-01 , 1.3793103e-01
Find the maximum of the interpolated function.
(:toggle hide apm button show="Example APMonitor (Python) Source":)
(:div id=apm:)
Deleted line 50:
Changed lines 53-107 from:
"""
minimize y
s.t. y = f(x)
using cubic spline with random sampling of data
"""
# Function to generate data for cspline
def f(x):
return 3*np.sin(x) - (x-3)
# Create model
c = gekko()
# Cubic spline
x = c.Var(value=15)
y = c.Var()
x_data = np.random.rand(50)*10+10
y_data = f(x_data)
c.cspline(x,y,x_data,y_data,True)
c.Obj(y)
# Options
c.options.IMODE = 3
c.options.CSV_READ = 0
c.options.SOLVER = 3
c.solve()
# Generate continuous trend for plot
z = np.linspace(10,20,100)
# Check if solved successfully
if c.options.SOLVESTATUS == 1:
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b',label='data')
plt.scatter(x.value,y.value,200,'k','x',label='minimum')
plt.legend(loc='best')
else:
print ('Failed to converge!')
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b')
plt.legend(loc='best')
plt.show()
(:sourceend:)
(:divend:)
%width=550px%Attach:cspline_gekko.png
!!!! [[https://apmonitor.com/wiki/index.php/Main/PythonApp|APM Python]] Example
Use the following x and y data to construct a cubic spline.
to:
from APMonitor.apm import *
s = 'https://byu.apmonitor.com'
a = 'cspline'
model = '''
Objects
c = cspline
End Objects
File c.csv
s = 'https://byu.apmonitor.com'
a = 'cspline'
model = '''
Objects
c = cspline
End Objects
File c.csv
Deleted lines 72-100:
Find the maximum of the interpolated function.
(:toggle hide apm button show="Example APMonitor (Python) Source":)
(:div id=apm:)
(:source lang=python:)
import numpy as np
import matplotlib.pyplot as plt
from APMonitor.apm import *
s = 'https://byu.apmonitor.com'
a = 'cspline'
model = '''
Objects
c = cspline
End Objects
File c.csv
x_data , y_data
-1.0000000e+00 , 3.8461538e-02
-8.0000000e-01 , 5.8823529e-02
-5.0000000e-01 , 1.3793103e-01
-2.5000000e-01 , 3.9024390e-01
0.0000000e+00 , 1.0000000e+00
1.0000000e-01 , 8.0000000e-01
2.0000000e-01 , 5.0000000e-01
5.0000000e-01 , 1.3793103e-01
Added lines 115-176:
!!!! [[https://gekko.readthedocs.io/en/latest/|GEKKO Python]] Example
Sample the function {3sin(x)-(x-3)} at 50 random points between 5 and 15. Use the randomly sampled points to construct a cubic spline and find the minimum of that function.
(:toggle hide gekko button show="Example GEKKO (Python) Code":)
(:div id=gekko:)
(:source lang=python:)
from gekko import gekko
import numpy as np
import matplotlib.pyplot as plt
"""
minimize y
s.t. y = f(x)
using cubic spline with random sampling of data
"""
# Function to generate data for cspline
def f(x):
return 3*np.sin(x) - (x-3)
# Create model
c = gekko()
# Cubic spline
x = c.Var(value=15)
y = c.Var()
x_data = np.random.rand(50)*10+10
y_data = f(x_data)
c.cspline(x,y,x_data,y_data,True)
c.Obj(y)
# Options
c.options.IMODE = 3
c.options.CSV_READ = 0
c.options.SOLVER = 3
c.solve()
# Generate continuous trend for plot
z = np.linspace(10,20,100)
# Check if solved successfully
if c.options.SOLVESTATUS == 1:
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b',label='data')
plt.scatter(x.value,y.value,200,'k','x',label='minimum')
plt.legend(loc='best')
else:
print ('Failed to converge!')
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b')
plt.legend(loc='best')
plt.show()
(:sourceend:)
(:divend:)
%width=550px%Attach:cspline_gekko.png
Added line 178:
See also [[Main/ObjectBspline|B-Spline Object]] for 2D surface function approximations from data
Changed line 109 from:
(:toggle hide apm button show="Example APMonitor Model":)
to:
(:toggle hide apm button show="Example APMonitor (Python) Source":)
Changed line 33 from:
Sample the function {3sin(x)-(x-3)} at 50 random points between 5 and 15. Use the randomly sampled points to construct a cubic spline and find the maximum of that function.
to:
Sample the function {3sin(x)-(x-3)} at 50 random points between 5 and 15. Use the randomly sampled points to construct a cubic spline and find the minimum of that function.
Changed lines 31-32 from:
!!!! GEKKO Example
to:
!!!! [[https://gekko.readthedocs.io/en/latest/|GEKKO Python]] Example
Sample the function {3sin(x)-(x-3)} at 50 random points between 5 and 15. Use the randomly sampled points to construct a cubic spline and find the maximum of that function.
Sample the function {3sin(x)-(x-3)} at 50 random points between 5 and 15. Use the randomly sampled points to construct a cubic spline and find the maximum of that function.
Deleted lines 37-38:
Changed lines 93-107 from:
!!!! APM Python Example
to:
!!!! [[https://apmonitor.com/wiki/index.php/Main/PythonApp|APM Python]] Example
Use the following x and y data to construct a cubic spline.
x_data , y_data
-1.0000000e+00 , 3.8461538e-02
-8.0000000e-01 , 5.8823529e-02
-5.0000000e-01 , 1.3793103e-01
-2.5000000e-01 , 3.9024390e-01
0.0000000e+00 , 1.0000000e+00
1.0000000e-01 , 8.0000000e-01
2.0000000e-01 , 5.0000000e-01
5.0000000e-01 , 1.3793103e-01
Find the maximum of the interpolated function.
Use the following x and y data to construct a cubic spline.
x_data , y_data
-1.0000000e+00 , 3.8461538e-02
-8.0000000e-01 , 5.8823529e-02
-5.0000000e-01 , 1.3793103e-01
-2.5000000e-01 , 3.9024390e-01
0.0000000e+00 , 1.0000000e+00
1.0000000e-01 , 8.0000000e-01
2.0000000e-01 , 5.0000000e-01
5.0000000e-01 , 1.3793103e-01
Find the maximum of the interpolated function.
Deleted lines 32-33:
Added lines 36-37:
Sample the function {3sin(x)-(x-3)} at 50 random points between 5 and 15. Use the randomly sampled points to construct a cubic spline and find the maximum of that function.
Changed lines 91-93 from:
!!!! APMonitor Example
to:
%width=550px%Attach:cspline_gekko.png
!!!! APM Python Example
!!!! APM Python Example
Added lines 31-32:
!!!! GEKKO Example
Added line 91:
!!!! APMonitor Example
Added lines 31-35:
%width=550px%Attach:cspline_gekko.png
(:toggle hide gekko button show="Example GEKKO (Python) Code":)
(:div id=gekko:)
(:toggle hide gekko button show="Example GEKKO (Python) Code":)
(:div id=gekko:)
Added line 37:
from gekko import gekko
Added lines 40-95:
"""
minimize y
s.t. y = f(x)
using cubic spline with random sampling of data
"""
# Function to generate data for cspline
def f(x):
return 3*np.sin(x) - (x-3)
# Create model
c = gekko()
# Cubic spline
x = c.Var(value=15)
y = c.Var()
x_data = np.random.rand(50)*10+10
y_data = f(x_data)
c.cspline(x,y,x_data,y_data,True)
c.Obj(y)
# Options
c.options.IMODE = 3
c.options.CSV_READ = 0
c.options.SOLVER = 3
c.solve()
# Generate continuous trend for plot
z = np.linspace(10,20,100)
# Check if solved successfully
if c.options.SOLVESTATUS == 1:
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b',label='data')
plt.scatter(x.value,y.value,200,'k','x',label='minimum')
plt.legend(loc='best')
else:
print ('Failed to converge!')
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b')
plt.legend(loc='best')
plt.show()
(:sourceend:)
(:divend:)
(:toggle hide apm button show="Example APMonitor Model":)
(:div id=apm:)
(:source lang=python:)
import numpy as np
import matplotlib.pyplot as plt
Deleted lines 155-211:
(:toggle hide gekko button show="Example GEKKO (Python) Code":)
(:div id=gekko:)
%width=550px%Attach:cspline_gekko.png
(:source lang=python:)
from gekko import gekko
import numpy as np
import matplotlib.pyplot as plt
"""
minimize y
s.t. y = f(x)
using cubic spline with random sampling of data
"""
# Function to generate data for cspline
def f(x):
return 3*np.sin(x) - (x-3)
# Create model
c = gekko()
# Cubic spline
x = c.Var(value=15)
y = c.Var()
x_data = np.random.rand(50)*10+10
y_data = f(x_data)
c.cspline(x,y,x_data,y_data,True)
c.Obj(y)
# Options
c.options.IMODE = 3
c.options.CSV_READ = 0
c.options.SOLVER = 3
c.solve()
# Generate continuous trend for plot
z = np.linspace(10,20,100)
# Check if solved successfully
if c.options.SOLVESTATUS == 1:
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b',label='data')
plt.scatter(x.value,y.value,200,'k','x',label='minimum')
plt.legend(loc='best')
else:
print ('Failed to converge!')
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b')
plt.legend(loc='best')
plt.show()
Added lines 95-150:
(:toggle hide gekko button show="Example GEKKO (Python) Code":)
(:div id=gekko:)
(:source lang=python:)
from gekko import gekko
import numpy as np
import matplotlib.pyplot as plt
"""
minimize y
s.t. y = f(x)
using cubic spline with random sampling of data
"""
# Function to generate data for cspline
def f(x):
return 3*np.sin(x) - (x-3)
# Create model
c = gekko()
# Cubic spline
x = c.Var(value=15)
y = c.Var()
x_data = np.random.rand(50)*10+10
y_data = f(x_data)
c.cspline(x,y,x_data,y_data,True)
c.Obj(y)
# Options
c.options.IMODE = 3
c.options.CSV_READ = 0
c.options.SOLVER = 3
c.solve()
# Generate continuous trend for plot
z = np.linspace(10,20,100)
# Check if solved successfully
if c.options.SOLVESTATUS == 1:
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b',label='data')
plt.scatter(x.value,y.value,200,'k','x',label='minimum')
plt.legend(loc='best')
else:
print ('Failed to converge!')
plt.figure()
plt.plot(z,f(z),'r-',label='original')
plt.scatter(x_data,y_data,5,'b')
plt.legend(loc='best')
plt.show()
(:sourceend:)
(:divend:)
Added lines 14-17:
(:html:)
<iframe width="560" height="315" src="https://www.youtube.com/embed/s1jSLpDXvzs" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
(:htmlend:)
Changed line 23 from:
%width=400px%Attach:cspline.png
to:
%width=500px%Attach:cspline.png
Changed lines 21-22 from:
The function is evaluated at the points x_data = [-1.0 -0.8 -0.5 -0.25 0.0 0.1 0.2 0.5].
to:
The function is evaluated at the points x_data = [-1.0 -0.8 -0.5 -0.25 0.0 0.1 0.2 0.5]. Evaluating at additional points [[Attach:cspline_plot.zip|shows the cubic spline interpolation function]]. The maximum of the original function is at ''x''=0 with a result ''y''=1. Because the cubic spline has only 8 points, there is some approximation error and the optimal solution of the cubic spline is slightly to the left of the true solution.
Changed line 25 from:
to:
The cubic spline intersects the points to create the function approximations in the range of x between -1.0 and 0.5. There is extrapolation error outside of this range, as expected. Bounds on ''x'' should be added or additional cubic spline sample points should be added to avoid problems with optimizer performance in the extrapolation region.
Added lines 17-22:
Find the maximum of a function defined by 8 points that approximate the true function.
{$y(x) = \frac{1}{1+25 x^2}$}
The function is evaluated at the points x_data = [-1.0 -0.8 -0.5 -0.25 0.0 0.1 0.2 0.5].
{$y(x) = \frac{1}{1+25 x^2}$}
The function is evaluated at the points x_data = [-1.0 -0.8 -0.5 -0.25 0.0 0.1 0.2 0.5].
Added lines 24-25:
A cubic spline intersects the points to create the function approximations in the range of x between -1.0 and 0.5. There is extrapolation error outside of this range, as expected. Bounds on ''x'' should be added or additional cubic spline sample points should be added to avoid problems with optimizer performance in the extrapolation region.
Changed lines 17-26 from:
Objects
to:
(:source lang=python:)
import numpy as np
import matplotlib.pyplot as plt
from APMonitor.apm import *
s = 'https://byu.apmonitor.com'
a = 'cspline'
model = '''
Objects
import numpy as np
import matplotlib.pyplot as plt
from APMonitor.apm import *
s = 'https://byu.apmonitor.com'
a = 'cspline'
model = '''
Objects
Changed lines 28-30 from:
to:
End Objects
File c.csv
File c.csv
Changed lines 40-42 from:
to:
End File
Connections
Connections
Changed lines 45-47 from:
to:
End Connections
Parameters
End Parameters
Variables
Parameters
End Parameters
Variables
Changed lines 53-55 from:
to:
End Variables
Equations
Equations
Changed lines 57-80 from:
to:
End Equations
'''
# write file
fid = open('model.apm','w')
fid.write(model)
fid.close()
# clear prior, load new model
apm(s,a,'clear all')
apm_load(s,a,'model.apm')
# set steady state optimiation and solve
apm_option(s,a,'apm.imode',3)
output = apm(s,a,'solve')
print(output)
# retrieve solution
z = apm_sol(s,a)
# print solution
print('x: ' + str(z['x']))
print('y: ' + str(z['y']))
(:sourceend:)
'''
# write file
fid = open('model.apm','w')
fid.write(model)
fid.close()
# clear prior, load new model
apm(s,a,'clear all')
apm_load(s,a,'model.apm')
# set steady state optimiation and solve
apm_option(s,a,'apm.imode',3)
output = apm(s,a,'solve')
print(output)
# retrieve solution
z = apm_sol(s,a)
# print solution
print('x: ' + str(z['x']))
print('y: ' + str(z['y']))
(:sourceend:)
Added lines 1-45:
(:title Cubic Spline (cspline) Object:)
(:keywords Cubic spline, Object, APMonitor, Option, Configure, Default, Description:)
(:description One dimensional cubic spline for nonlinear function approximation with multiple interpolating functions that have continuous first and second derivatives:)
%width=50px%Attach:apm.png [[Main/Objects|APMonitor Objects]]
Type: Object
Data: Two data vectors that define 1D function points
Inputs: Name of first data column (e.g. x)
Outputs: Name of second data column (e.g. y)
Description: Cubic spline for nonlinear function approximation
A cubic spline is a nonlinear function constructed of multiple third-order polynomials. These polynomials pass through a set of control points and have continuous first and second derivatives everywhere. The second derivative is set to zero at the left and right endpoints, to provide a boundary condition to complete the system of equations. There is poor extrapolation when function retrievals are requested outside of the data points. The input should be constrained or else additional data points added to avoid extrapolation.
'''Example Usage'''
Objects
c = cspline
End Objects
File c.csv
x_data , y_data
-1.0000000e+00 , 3.8461538e-02
-8.0000000e-01 , 5.8823529e-02
-5.0000000e-01 , 1.3793103e-01
-2.5000000e-01 , 3.9024390e-01
0.0000000e+00 , 1.0000000e+00
1.0000000e-01 , 8.0000000e-01
2.0000000e-01 , 5.0000000e-01
5.0000000e-01 , 1.3793103e-01
End File
Connections
x = c.x_data
y = c.y_data
End Connections
Variables
x = -0.5 >= -1 <= 0.5
y
End Variables
Equations
maximize y
End Equations
(:keywords Cubic spline, Object, APMonitor, Option, Configure, Default, Description:)
(:description One dimensional cubic spline for nonlinear function approximation with multiple interpolating functions that have continuous first and second derivatives:)
%width=50px%Attach:apm.png [[Main/Objects|APMonitor Objects]]
Type: Object
Data: Two data vectors that define 1D function points
Inputs: Name of first data column (e.g. x)
Outputs: Name of second data column (e.g. y)
Description: Cubic spline for nonlinear function approximation
A cubic spline is a nonlinear function constructed of multiple third-order polynomials. These polynomials pass through a set of control points and have continuous first and second derivatives everywhere. The second derivative is set to zero at the left and right endpoints, to provide a boundary condition to complete the system of equations. There is poor extrapolation when function retrievals are requested outside of the data points. The input should be constrained or else additional data points added to avoid extrapolation.
'''Example Usage'''
Objects
c = cspline
End Objects
File c.csv
x_data , y_data
-1.0000000e+00 , 3.8461538e-02
-8.0000000e-01 , 5.8823529e-02
-5.0000000e-01 , 1.3793103e-01
-2.5000000e-01 , 3.9024390e-01
0.0000000e+00 , 1.0000000e+00
1.0000000e-01 , 8.0000000e-01
2.0000000e-01 , 5.0000000e-01
5.0000000e-01 , 1.3793103e-01
End File
Connections
x = c.x_data
y = c.y_data
End Connections
Variables
x = -0.5 >= -1 <= 0.5
y
End Variables
Equations
maximize y
End Equations