Modeling and Optimization of Dynamical Systems by Unconventional Spreadsheet Functions
Modeling and Optimization of Dynamical Systems by Unconventional Spreadsheet Functions
1, 1-12
Available online at http://pubs.sciepub.com/ajmo/4/1/1
© Science and Education Publishing
DOI:10.12691/ajmo-4-1-1
Abstract The spreadsheet computational engine is exploited via a nonstandard mechanism to support a functional
formulation for constrained optimization of parameterized differential systems by unconventional spreadsheet
functions. The nonstandard mechanism enables encapsulation of numerical algorithms into functions which take
variable formulas as a new type of input argument while retaining purity and recursion properties. This is in contrast
to conventional spreadsheet functions which are restricted to static input types. Several solvers for differential
equations and nonlinear minimization are developed which serve as building blocks for the functional formulation.
The latter makes it possible to express a program for a constrained dynamical minimization problem in as few as
three formula evaluations in Excel as demonstrated by several examples. The solver functions integrate seamlessly
with MS Excel, and propel the spreadsheet beyond traditional applications as a powerful tool for exploring
dynamical optimization problems.
Keywords: dynamical optimization, optimal control, differential equations, spreadsheet, functional paradigm
Cite This Article: Chahid Kamel Ghaddar, “Modeling and Optimization of Dynamical Systems by
Unconventional Spreadsheet Functions.” American Journal of Modeling and Optimization, vol.4, no. 1 (2016): 1-
12.doi:10.12691/ajmo-4-1-1.
Certainly, the inherent design limitations of the Table 1 illustrates utilizing (1) to compute the integral
spreadsheet unduly limit its full potential. Given the 1 ln x
spreadsheet intuitive interface for defining formulas, it of ∫ dx = −4 in Excel. The reference to the integrand
0 x
presents a practical platform for supporting native calculus
formula A1 1, the variable of integration X1, and limits are
solvers that could support a functional paradigm for
passed to (1) as regular parameters. Evaluating QUADF
dynamical optimization problems. Accordingly, the author
formula in A2 computes the results without any side
has developed a method which overcomes the spreadsheet
effects. Such a practical integration function has never
limitation and enables the creation of a first class function
existed in a spreadsheet application before.
– function that can take other functions (i.e., formulas and
variables) as arguments while preserving its mathematical Table 1. Computing a formula integral in Excel by the worksheet
properties including purity and recursion. Details of the function (1)
method are provided in [17] and are rather technical in A A
nature. However, the main idea is to capture the definition
1 =LN(X1)/SQRT(X1) 1 #NUM!
of an input formula via the spreadsheet Advanced
Programming Interface (API) and construct a relational 2 =QUADF(A1,X1,0,1) 2 -4
graph representing the formula inter-dependence on nested
More importantly, by preserving function properties
formulas, variable cells, and recursive calls. A graph
including purity and recursion, the first class solvers can
evaluator which exploits the spreadsheet API evaluates the
be utilized as building blocks of a functional paradigm for
value of a formula without modifying any data in the
solving dynamical optimization problems. A simple
spreadsheet.
example of a functional program is illustrated in Table 2
The flowchart logic for developing a first class solver is
in which (1) is employed to compute the volume integral
shown in Figure 1. The benefits gained by enabling a first
3
class solver in the spreadsheet are noteworthy. The 2 3− x 6 − 3 x − 2 y
spreadsheet’s computational engine could be exploited to ∫ ∫ 0 0
2
0 ∫ 1 − xdzdydx =
3. Here each inner
support intrinsic solvers for virtually any system that can QUADF formula serves as the integrand for the next outer
be modelled by formulas and variables [18]. For example, QUADF formula. Evaluating the outer integral in A4
the flow chart of Figure 1 makes possible, for the first computes the triple integral value.
time, the existence of the following intrinsic worksheet
integration function: Table 2. Example of using recursion to compute triple integral in
Excel
= QUADF ( f , x, a, b, [Options ]) (1) A A
b 1 =1-X1 1 1
for computing a formula integral ∫a f ( x )dx using
2 =QUADF(A1,Z1,0,6-3*X1-2*Y1) 2 6
appropriate algorithms [19]. 3 =QUADF(A2,Y1,0,3-3*X1/2) 3 9
4 =QUADF(A3,X1,0,2) 4 3
1
The #NUM! error reported by Excel is due to division by X1 which is
Figure 1. Flow chart for unconventional first class spreadsheet function undefined. X1 is chosen as a dummy variable for the formula. Its value,
design assumed zero by Excel, is irrelevant, and the error can be ignored.
American Journal of Modeling and Optimization 3
Figure 2. The result table serves as a discrete proxy for the system by a suitable solver IVSOLVE(),
function 𝒖(𝑥, 𝒑𝒅 ) in (2)-(6) providing a map between the BVSOLVE(), or PDSOLVE().
independent variable 𝑥 and the state variables 𝒖 for II. Define constraints on the initial system response
specified values of the design parameters 𝒑𝒅 . using the criterion functions ARRAYVAL() and
ODEVAL().
III. Solve for the set of constraints for the optimal
design parameters using NLSOLVE().
These steps are demonstrated in the following section
with three examples in Excel.
2.3. Functional Minimizer Figure 3. Schematic for the forces acting on the travelling train of
problem 3.1
A functional minimizer NLSOLVE(), which
corresponds to (6), is described in Appendix C. It receives Using 𝑑/𝑅 ≪ 1, the angle of rotation, 𝜃, (see Figure 3)
the set of formula constraints, and design parameters can be approximated by the following formula:
variables, and computes optimal values for the latter that −1 R
minimize the implicit cost functional (5). tan d / 2 − x , x<d /2
The aforementioned spreadsheet functions enable the
π
θ ( x ) =
three-step optimization formulation (2)-(6) comprising the
= , x d /2 (9)
following practical steps.
2
I. Using initial values of the design parameters, obtain −1 R
an initial response to the parameterized differential tan +π, x > d / 2
d / 2− x
American Journal of Modeling and Optimization 5
On arrival at City B, the train comes to a halt, so the the displacement, 𝑥, at the final but unknown time, 𝑡𝑓 , and
final conditions can be stated as: the travelled distance 𝑑 , while constraint C15 demands
that the interpolated value for the velocity, 𝑣, at the final
( )
x tf = d
(10) time, 𝑡𝑓 , vanishes. As described in Appendix B,
x (t f ) = 0 ODEVAL() uses internally a spline curve fit to perform
the interpolation operation. The first argument for
The final conditions (10) can be viewed as constraints ODEVAL(), J2:J38, (selected from Table 5) defines the
on the differential equation (8) for the train’s motion. The desired range for the interpolation operation, and second
problem is thus reduced to finding optimal values for the argument, x, identifies the variable to interpolate 3.
unknown propulsion force 𝐹𝑝 and the travel time 𝑡𝑓 such
that the two constraints (10) are satisfied. We compute the Table 5. Solution computed by (11) for the system definition given in
answer by a simple functional program in Excel Table 4
spreadsheet corresponding to the three-step optimization J K L J K L
process as shown below. 1 t x V 20 2250 535135 146.4526
2 0 0 0 21 2375 552249.8 127.3546
Step 1 3 125 6115.463 96.66716 22 2500 566971.2 108.1826
We model the differential equation (8) in Excel as a 4 250 23618.42 180.3757 23 2625 579292.2 88.95507
parameterized two first order equations in B2:B3 as shown 5 375 50345.74 243.5815 24 2750 589207.6 69.68509
in Table 4. For convenience, we assign names to raw cells
6 500 83614.12 285.3167 25 2875 596712.8 50.38592
addresses, e.g., ‘x’ for B6, ‘v’ for B7, ‘Fd’ for D2, ‘Th’
for D4 and so on. 7 625 120912.9 308.8178 26 3000 601804.6 31.06775
8 750 160244.8 318.6075 27 3125 604479.3 11.73845
Table 4. Parametrized differential system definition in Excel for 9 875 200165.6 318.7776 28 3250 604737.5 -7.59494
problem 3.1
A B C D 10 1000 239672.8 312.4788 29 3375 602579.9 -26.9254
1 Differential system Forces formulas 11 1125 278105.9 301.8894 30 3500 598007 -46.2459
12 1250 315027.7 288.4672 31 3625 591022 -65.5454
2 dx/dt =v Fd =0.5*v^2
=(Fp- 13 1375 350145.5 273.1784 32 3750 581623.1 -84.8236
3 dv/dt Fg =m*g*COS(Th)
Fd+Fg)/m 14 1500 383269.8 256.6382 33 3875 569814.6 -104.068
=IF(x<d/2,ATAN(Re/(d/2-
System variables with 15 1625 414271.2 239.2515 34 4000 555605.2 -123.257
4 Th x)),IF(x>d/2,ATAN(Re/(d/2-
initial conditions
x))+PI(),PI()/2)) 16 1750 443058.6 221.296 35 4125 539003.6 -142.369
5 t 0
17 1875 469577.2 202.9315 36 4250 520018.9 -161.382
6 x 0 Constant parameters 18 2000 493781.3 184.2855 37 4375 498664.1 -180.264
7 v 0 g 10 19 2125 515639.8 165.4439 38 4500 474960.7 -198.957
Design parameters
8 Re 6371000
with initial values
Table 6. Constraints formulas on the initial solution of Table 5. The
9 Fp 1000 d 1000000
constraints correspond to the conditions given in (10)
10 Tf 4500 m 100000 C
14 =ODEVAL(J2:J38, x, "INTERP", Tf) - d
The system is simulated with a guess value 𝐹𝑝 = 1000
for a sufficient time of 4500 seconds by evaluating the 15 =ODEVAL(J2:J38, v, "INTERP", Tf)
IVSOLVE() array formula:
Step 3
= IVSOLVE ( B 2 : B3, ( t , x, v ) , {0, 4500}) (11)
Using the functional minimizer NLSOLVE() (C.1), the
in the range J1:L38 2 . The first argument to (11) is a system of constraints C14:C15 is solved with 𝐹𝑝 and 𝑡𝑓 as
reference to the system formulas (see Table 4), the second variables by evaluating the NLSOLVE() formula:
argument is the system variables, and the third argument
specifies the time interval. IVSOLVE() computes and = NLSOLVE ( C14 : C15, ( Fp, Tf ) ) (12)
displays the solution in the range J1:L38 as shown in
Table 5. in the allocated range A16:B18. NLSOLVE() computes
and displays the values for 𝐹𝑝 and 𝑡𝑓 as shown Table 7.
Step 2
Table 7. Optimal parameters computed by (12) satisfying the
Using the criterion function ODEVAL() (B.2), we constraints of Table 6
define in Table 6 two constraint formulas, which A B
correspond to the final conditions (10). Constraint C14 16 Fp 62648.94
penalizes the difference between the interpolated value for
17 Tf 3863.575
2 18 SSERROR 1E-16
The choice of the range location and its allocated number of rows are
arbitrary. By default, the number of rows determines the uniform interval
for the output solution. This behavior can be changed via the options to
3
the solver [24], such as to use a custom step or non-uniform output In general, the second argument may be an arbitrary formula of system
points. variables and parameters [24].
6 American Journal of Modeling and Optimization
Figure 4 plots a simulation of equation (8) using the Table 8. Parametrized differential systeqm definitions in Excel for
problem 3.2
computed values for 𝐹𝑝 and 𝑡𝑓 in Table 7. The plot shows
A B C D
that the final arrival conditions (10) are satisfied.
1 Differential system Design parameters
2 dx/dt =v zeta 0.25
3 dv/dt =-2*zeta*wn*v-wn^2*x wn 1
System variables with initial
4 x0 1
conditions
5 t 0 x_peak -0.2
6 x =x0 t_peak 2.0
7 v 0 e_min 0.1
4
Figure 5. Plot of system response computed in Table 9 using the C2 makes use of the readily available velocity in Table 9, however, we
could impose an equivalent constraint on the displacement derivative
parameters values given in Table 8
directly using ODEVAL() (B.2) as follows
‘=ODEVAL(I1:I41,x,”DERIV”,2.0)’.
American Journal of Modeling and Optimization 7
1 zeta
D E
0.451081913
( )
x t p − x _ peak =
0
2 wn 1.659599536 ( )
v tp = 0 (18)
3 SSERROR 2.20497E-26
( )
e t p − e _ min ≥ 0
consider a slab that is initially at zero temperature with an PDSOLVE() populates the range with the requested
insulated right side. At time = 0, the left side is brought to transient format solution shown in Table 16 and plotted in
100 degrees. We are interested in controlling the right side Figure 9A. Alternatively, in the default snapshot format
temperature reached after one second. The problem is for the output array, the order of time and space are
described by the parabolic heat equation: exchanged in the table. The latter format simplifies
plotting the temperature spatial profile at different times as
∂u ∂ 2u
=k (20) shown in Figure 9B.
∂t ∂x 2
with initial condition 𝑢(𝑥, 0) = 0 , left boundary
condition 𝑢(0, 𝑡) = 100, and right boundary condition
𝜕𝑢(𝑥, 𝑡)/𝜕 𝑥 = 0 at 𝑥 = 1. The problem’s spatial domain
is 𝑥 ∈ [0 1] and the time interval is 𝑡 ∈ [0 1]. Table 15
shows the model definition in Excel for the solver
PDSOLVE() (A.7). Here we use the cells T1, X1, U1, U2,
and U3 to represent system variables 𝑡, 𝑥, 𝑢, 𝑢𝑥 , 𝑢𝑥𝑥
respectively. We also assign the names u, ux, and uxx for
U1, U2 and U3. The system RHS formula is defined in A1,
the left boundary condition in B1, and the right boundary
condition in C1, the conductivity 𝑘 in K1 and the initial
condition for 𝑢 in U1.
Table 15. PDE System definition in Excel for problem 3.3 Figure 9A.Transient plot of the system response computed in Table 16 at
A B C K U different spatial points
1 =k*uxx =u-100 =ux 1 =IF(X1=0,100,0)
= NLSOLVE ( C 2, P1) (22) Excel supports two types of formulas: simple formulas
and array formulas. A simple formula is assigned to one
with P1 as the variable. NLSOLVE() computes an optimal cell and evaluates to a single value, e.g., ’=SUM(A1:B4)’.
value of 84.0786 for P1, as shown in Table 18. Although Alternatively, an array formula is assigned to a range of
not shown, re-simulating the system (20) with the cells and evaluates to an array of values, e.g.,
computed value for P1 shows that the constraint C2 is ‘=MINVERSE(A1:C3)’ which computes the inverse of
satisfied. the 3 by 3 matrix A1:C3.
Table 18. Constraint and solver formulas for problem 3.3. The A.2 Differential Systems Solvers
optimal value computed by the solver is shown to the right
C C A.2.1 Initial Value Spreadsheet Solver: IVSOLVE()
2 =ARRAYVAL(H23) - 75
= IVSOLVE ( rhs, vars , interval , m, [ options ] ) (A.1)
3 =NLSOLVE(C2,P1) 84.07859699
The spreadsheet solver IVSOLVE (A.1) computes the
solution to an initial value ordinary differential-algebraic
4. Conclusion system represented in the ordered form:
dui
i ( t , u, y ) , i 1,..., n
The spreadsheet computing engine is exploited via a = f=
nonconventional approach to develop a novel set of dt (A.2)
=0 g=j ( t , u, y ) , j 1,..., m
solvers which support a functional paradigm for
dynamical optimization. Design of the solvers was made
possible by overcoming inherent limitations that restricted with initial conditions 𝑢𝑖 (0) = 𝑎𝑖 , 𝑦𝑗 (0) = 𝑏𝑗 , and over
spreadsheet functions to operating on static inputs only, the Interval 𝑡 ∈ [0 𝑇].
while retaining essential properties of purity and recursion. References to the system RHS formulas
The solvers are assembled in an add-in software library (𝑓1 , 𝑓2 , . . , 𝑓𝑛 , 𝑔1 , 𝑔2 , . . 𝑔𝑚 ) are supplied in the first
[24], which integrates seamlessly with MS Excel. Three parameter rhs and the system variables
examples were presented to demonstrate the merits of the (𝑡, 𝑢1 , 𝑢2 , . . , 𝑢𝑛 , 𝑦1 , 𝑦2 , . . 𝑦𝑚 ) are seeded with initial values
solvers, which expand the spreadsheet utility into a new and supplied via the 2nd parameter vars. The integration
direction. Future effort will consider extending the interval is defined in the third parameter, interval, and the
capability of this framework to solve general optimal number of algebraic constraints, if nonzero, is supplied in
control problems. 4th parameter m. IVSOLVE() implements several
Although we do not provide benchmark performance integration schemes [25,26], suitable for stiff and smooth
data in this paper, we comment that all the preceding problems. Algorithm control parameters and an optional
examples compute on the order of a second or less, on a system analytic Jacobian can be supplied via the
typical computer with an Intel core i5 processor. The [options][24].
performance is directly linked to the true performance of IVSOLVE() is executed as an array formula in a pre-
the spreadsheet engine due to direct interface to the allocated range of cells. It evaluates to an ordered tabular
spreadsheet engine API [17]. array of values for the system variables with the layout
shown in Figure 2. By default IVSOLVE() reports the
output at uniform intervals according to the allocated
Appendix A number of rows for the output array. Custom output
formats can be achieved via the optional parameters [24].
A.1 Basic Spreadsheet Concepts
A.2.2 Boundary Value Spreadsheet Solver:
A spreadsheet is composed of a large structured grid.
BVSOLVE()
Each cell in the grid is referenced by its column label and
row number (e.g., A1), and represents a global memory rhs, vars, bpts, bcs,
= BVSOLVE (A.3)
interval , m, [ options ]
placeholder. A range of cells can be referenced as a
rectangular array, e.g., A1:B3 or a union of disjoint arrays
and cells, e.g., (X1, A1:A3). A cell may store a constant The spreadsheet function BVSOLVE() (A.3) computes
value or a formula defined using basic spreadsheet syntax, the solution to a multi-point boundary-value first order
e.g., ‘= SQRT(X1^2 + Y1*Y1)’. The spreadsheet engine differential-algebraic systems represented in the order
insures orderly evaluation of all dependent formulas upon defined in (A.2), with boundary conditions expressed in
a change in the value of any cell. A general function can the form 0 = 𝑏𝑐𝑖 (𝒖, 𝑥) at 𝑥 = 𝑝𝑖 for 𝑖 = 1, … , 𝑛 in the
thus be identified by a root formula and a list of variable domain 𝑥 ∈ [0 𝐿].
cells. Nested dependency allows arbitrarily complex BVSOLVE() shares similar parameters with
functions to be constructed. To motivate the possibilities, IVSOLVE(), except for the boundary points and
consider the formula ‘=SUM(X1:Z1)’ assigned to A1, the associated conditions formulas, which are supplied via
pair (A1, Y1) identifies the function 𝑓(𝑦) = 𝑋1 + 𝑦 + 𝑍, bpts and bcs, respectively. BVSOLVE() implements the
where X1 and Z1 are treated as constant values. In another COLDAE collocation algorithm [27,28], and evaluates to
example, consider the formula ‘=1+COS(B1)’ assigned to the same results layout shown in Figure 2. Optional
A1, and the formula ‘=SQRT(ABS(X1))’ assigned to B1, analytical system and boundary conditions Jacobians as
the pair (A1,X1) identifies the function 𝑓(𝑥) = 1 + well as algorithm control parameters can be supplied via
cos(�|𝑥|). [𝑜𝑝𝑡𝑖𝑜𝑛𝑠] [24].
10 American Journal of Modeling and Optimization
To demonstrate BVSOLVE(), we solve the 4th order Table 20. partial listing of results computed by (A.6) for the system
equation: definition given in Table 19
H I J K L
1 − 6 x 2 z ''' − 6 xz ''
=z '''' , 1≤ x ≤ 2 4 X1 W1 V1 U1 Z1
x3 5 1 -0.5 1.552E-30 0.017132 0
z (1) = 0, z '' (1) = 0, (A.4) 6 1.05 -0.3301 -0.020516 0.016584 0.0008472
z ( 2 ) = 0, z ( 2 ) = 0
'' -- -- -- -- -- --
24 1.95 0.065617 -0.003203 -0.01121 0.0005634
which models a uniformly loaded beam of variable
stiffness simply supported at both ends [29]: 25 2 0.0625 1.735E-18 -0.01129 -1.084E-19
Using standard substitution, we convert (A.4) into a
system of 1st order equations. Letting 𝑢 = 𝑧′, 𝑣 = 𝑢′ = A.2.3 Initial Boundary Value Problem Spreadsheet
𝑧 ′′ , 𝑤 = 𝑣 ′ = 𝑧′′′, we have: Solver: PDSOLVE()
[27] U. M. Ascher, R. M. Mattheij and R. D. Russell, Numerical [30] Schiesser W.E (1991).The Numerical Method of Lines, San Diego,
Solution of Boundary Value Problems for Ordinary Differential CA: Academic Press, 1991.
Equations, SIAM, 1995. [31] Gao, Zhang and Cao in the article: “Differentiation and numerical
[28] U. Ascher and R. Spiteri, Collocation software for boundary value Integral of the Cubic Spline Interpolation”, in the Journal of
differential-algebraic equations, SIAM Journal on Scientific Computers, Vol. 6, No 10, 2011.
Computing. 1994, 15,938-952.
[29] GAWAIN, T.H., AND BALL, R.E. Improved finite difference
formulas for boundary value problems. Int. J. Numer. Meth. Eng.
12 (1978), 1151-1160.