Add(Id:String): INonLDVariable;
Id - identifier of added variable.
The Add method adds a variable to the collection.
The system of non-linear equations is created in this example:
Output variables: x1, x2.
Controlling variables: u, v.
Equations that form the system:
0.3 * x1[t-1] + 0.1 * x2[t+1] + u[t-1] * x1[t+1] *x2[t-1].
-0.2 * x1[t-1] + 0.4 *x2[t+1] + (x1[t+1] * x2[t-1])/(v[t-1]+1).
Sub Main;
Var
Optima: NonLinearDecomposition;
PeriodL, j, i: Integer;
Vars: INonLDVariables;
Vrbl: INonLDVariable;
RetroX1, ForestX1: Array Of Double;
RetroX2, ForestX2: Array Of Double;
RetroU, ForestU: Array Of Double;
RetroV, ForestV, InitApproximation: Array Of Double;
Funstions: Array Of String;
Res: INonLoResults;
val: Double;
Begin
Optima := New NonLinearDecomposition.Create;
// Set calculation period value
PeriodL := 4;
RetroX1 := New Double[PeriodL]; ForestX1 := New Double[PeriodL];
RetroX2 := New Double[PeriodL]; ForestX2 := New Double[PeriodL];
RetroU := New Double[PeriodL]; ForestU := New Double[PeriodL];
RetroV := New Double[PeriodL]; ForestV := New Double[PeriodL];
InitApproximation := New Double[PeriodL];
For i := 0 To PeriodL - 1 Do
RetroX1[i] := -2.1 - i; ForestX1[i] := 1.9 + i;
RetroX2[i] := -2.2 - i; ForestX2[i] := 1.8 + i;
RetroU[i] := -2.3 - i; ForestU[i] := 1.7 + i;
RetroV[i] := -2.4 - i; ForestV[i] := 1.6 + i;
InitApproximation[i] := 0.1;
End For;
// Add the first variable
Vars := Optima.Variables;
Vrbl := Vars.Add("x1");
Vrbl.Retrospective := RetroX1;
Vrbl.CoefficientsOrderRetrospective := "x1[t];x1[t-1]";
Vrbl.Forestall := ForestX1;
Vrbl.CoefficientsOrderForestall := "x1[t];x1[t+1]";
Vrbl.InitApproximation := InitApproximation;
// Add the second variable
Vars := Optima.Variables;
Vrbl := Vars.Add("x2");
Vrbl.Retrospective := RetroX2;
Vrbl.CoefficientsOrderRetrospective := "x2[t];x2[t-1]";
Vrbl.Forestall := ForestX2;
Vrbl.CoefficientsOrderForestall := "x2[t];x2[t+1]";
Vrbl.InitApproximation := InitApproximation;
// Set initial approximation to the first controlling variable
For i := 0 To PeriodL - 1 Do
InitApproximation[i] := 1.5 * (i + 1);
End For;
// Add 1-th controlling variable
Vars := Optima.Variables;
Vrbl := Vars.Add("u");
Vrbl.Retrospective := RetroU;
Vrbl.CoefficientsOrderRetrospective := "u[t];u[t-1]";
Vrbl.Forestall := ForestU;
Vrbl.CoefficientsOrderForestall := "u[t];u[t+1]";
Vrbl.ControlVariable := True;
Vrbl.InitApproximation := InitApproximation;
// Set initial approximation to the second controlling variable
For i := 0 To PeriodL - 1 Do
InitApproximation[i] := 2 * (i + 1);
End For;
// Add the second controlling variable
Vars := Optima.Variables;
Vrbl := Vars.Add("v");
Vrbl.Retrospective := RetroV;
Vrbl.CoefficientsOrderRetrospective := "v[t];v[t-1]";
Vrbl.Forestall := ForestV;
Vrbl.CoefficientsOrderForestall := "v[t];v[t+1]";
Vrbl.ControlVariable := True;
Vrbl.InitApproximation := InitApproximation;
// form system of non-linear equations
Funstions := New String[2];
Funstions[0] := "0.3 * x1[t-1] + 0.1 * x2[t+1] + u[t-1] * x1[t+1] *x2[t-1]";
Funstions[1] := "-0.2 * x1[t-1] + 0.4 *x2[t+1] + (x1[t+1] * x2[t-1])/(v[t-1]+1)";
Optima.Equations := Funstions;
Optima.NodesCount := 2;
Optima.Extremum := ExtremumType.Minimum;
Optima.MaxIteration := 250;
Optima.Tolerance := 0.000001;
Optima.MethodType := NonLinearEquationsType.HMethod;
// Calculate system
Res := Optima.Evaluate(PeriodL) As INonLoResults;
Debug.WriteLine(Res.ErrorMsg);
// Display results
If (Res.Status = 0) Then
Vars := Optima.Variables;
For j := 0 To Vars.Count - 1 Do
Vrbl := Vars.Item(j);
Debug.WriteLine("Variable: " + Vrbl.Id);
For i := 0 To PeriodL - 1 Do
Val := Res.VarValues(Vrbl.Id)[i];
Debug.WriteLine(Val);
End For;
Debug.WriteLine("-----");
End For;
End If;
End Sub Main;
After executing the example a system of non-linear equations is created and calculated. Calculation results are displayed in the console window.
See also: