Example of Working with the System of Non-Linear Equations

Example

Sub Main;

Var

MB: IMetabase;

CrInf: IMetabaseObjectCreateInfo;

MObj: IMetabaseObject;

Model: IMsModel;

Trans: IMsFormulaTransform;

VarTrans: IMsFormulaTransformVariable;

Tree: IMsFormulaTransformSlicesTree;

Slice: IMsFormulaTransformSlice;

Selector: IMsFormulaTransformSelector;

Formula: IMsFormula;

ComposForm: IMsCompositeFormulaTermList;

ComposTerm: IMsCompositeFormulaTerm;

LinReg: IMsLinearRegressionTransform;

Determ: IMsDeterministicTransform;

Varr: IMsVariable;

TransVar: IMsFormulaTransformVariable;

TermX1: IMsFormulaTerm;

Begin

MB := MetabaseClass.Active;

//create a model

CrInf := Mb.CreateCreateInfo;

CrInf.ClassId := MetabaseObjectClass.KE_CLASS_MSMODEL;

CrInf.Id := "New_NonLinEquation";

CrInf.Name := "New_NonLinEquation";

CrInf.Parent := Mb.ItemById("KONT_MODEL");

CrInf.Permanent := False;

MObj := Mb.CreateObject(CrInf).Edit;

Model := MObj As IMsModel;

Trans := Model.Transform;

//The first output variable

Varr := MB.ItemByIdNamespace("Y1", MB.ItemById("KONT_MODEL").Key).Bind As IMsVariable;

Model.Output.Add(Varr);

Varr := MB.ItemByIdNamespace("Y2", MB.ItemById("KONT_MODEL").Key).Bind As IMsVariable;

Model.Output.Add(Varr);

//Create a system of non-linear equations

Model.Kind := MsModelKind.NonLinearEquations;

//Add to the list of variable input

Varr := MB.ItemByIdNamespace("X1", MB.ItemById("KONT_MODEL").Key).Bind As IMsVariable;

Model.Input.Add(Varr);

//Setting the first equation

VarTrans := Trans.Outputs.Item(0);

Tree := VarTrans.SlicesTree(VarTrans);

Slice := Tree.CreateSlice(1);

Selector := Trans.CreateSelector;

Selector.Slice := Slice;

Formula := Trans.Transform(Selector);

Formula.Kind := MsFormulaKind.LinearRegression;

LinReg := Formula.Method As IMsLinearRegressionTransform;

TransVar := Model.Transform.Inputs.Item(0);

ComposForm := LinReg.Explanatories;

ComposTerm := ComposForm.Add;

Slice := TransVar.SlicesTree(Null).CreateSlice(1);

TermX1 := ComposTerm.Operands.Add(Slice);

ComposTerm.Expression.AsString := TermX1.TermToInnerText;

//Setting the second equation

VarTrans := Trans.Outputs.Item(1);

Tree := VarTrans.SlicesTree(VarTrans);

Slice := Tree.CreateSlice(1);

Selector := Trans.CreateSelector;

Selector.Slice := Slice;

Formula := Trans.Transform(Selector);

Formula.Kind := MsFormulaKind.Deterministic;

Determ := Formula.Method As IMsDeterministicTransform;

TransVar := Model.Transform.Inputs.Item(0);

Slice := TransVar.SlicesTree(Null).CreateSlice(1);

TermX1 := Determ.Operands.Add(Slice);

Determ.Expression.AsString := "(" + TermX1.TermToInnerText + "+ 5 ) / 3.14";

MObj.Save;

End Sub Main;

After executing the example a system of non-linear equations is created in the modeling container. The system consists of two equations, linear regression is used to calculate the first method, the method of the determinate equation is used for the second one.

See also:

IMsNonLinearEquationsTransform