资料库: ALGLIB - 数值分析库 - 页 5

 

您好!


我是 ALGLIB 的新手。因此,我试图执行以下任务,即使用脚本计算 k²。我可以在 Excel 中轻松完成这项工作,但不知道如何使用带有一个盒式约束的非线性方程模型。


void OnStart()
  {
   const double x[22] = {28.98, 30.48, 30.98, 31.48, 31.98, 32.48, 32.98, 33.48, 33.98, 34.48, 34.98, 35.48, 35.98, 36.48, 36.98, 37.48, 37.98, 38.48, 38.98, 39.98, 40.98, 41.98};
   const double y[22] = {8.26, 7.26, 5.71, 6.15, 5.90, 5.15, 5.03, 4.30, 4.15, 3.30, 2.90, 2.71, 1.96, 1.57, 1.27, 0.90, 0.70, 0.47, 0.33, 0.20, 0.09, 0.06};
   const double w[22] = {2, 8, 2, 2, 2, 1, 11, 12, 24, 12, 6, 12, 21, 7, 74, 51, 159, 385, 171, 109, 18, 18}; //重量
   const double x0 = 37.74; //中心价值定理

//模型方程
//y = (1/2)*((x^2-2x0x+x0^2+4k^2)^(1/2) + x+x0) - x
//k^2 的计算结果为 0.582018800686398,在 Excel 中使用求解器后,总误差为 w(y-y0)^2 的最小化和
//方框约束 k^2>0;
  };




感谢您的阅读!

 
Arthur Albano #:

你好!


我是 ALGLIB 的新手。因此,我试图执行以下任务,即使用脚本计算 k²。我可以在 Excel 中轻松完成这项工作,但不知道如何使用带有一个盒式约束条件的非线性方程模型。





感谢您的阅读!


到目前为止,看起来很难看...


//+------------------------------------------------------------------+
//|AlgLibExample.mq5
//|阿瑟-阿尔巴诺
//|https://www.mql5.com/zh/users/arthuralbano|
//+------------------------------------------------------------------+
#property copyright "Arthur Albano"
#property link      "https://www.mql5.com/zh/users/arthuralbano"
#property version   "1.00"
//+------------------------------------------------------------------+
//| 脚本程序启动功能|
//+------------------------------------------------------------------+

#include <Math\Alglib\alglib.mqh>
#include <Math\Alglib\interpolation.mqh>
#include <Arrays\ArrayDouble.mqh>

//+------------------------------------------------------------------+
//||
//+------------------------------------------------------------------+
void OnStart()
  {
   const double x0 = 37.74; //中心价值定理

//模型方程
//f(x,k²,x0) = (1/2)*((x^2-2x0x+x0^2+4k^2)^(1/2) + x+x0) - x
//k^2 在 Excel 中使用求解器计算后为 0.582018800686398
//box contraint k^2>0;

// https://www.alglib.net/translator/man/manual.cpp.html#example_lsfit_d_nlfb

   const double x[22] = {28.98, 30.48, 30.98, 31.48, 31.98, 32.48, 32.98, 33.48, 33.98, 34.48, 34.98, 35.48, 35.98, 36.48, 36.98, 37.48, 37.98, 38.48, 38.98, 39.98, 40.98, 41.98};
   double w[22] = {2, 8, 2, 2, 2, 1, 11, 12, 24, 12, 6, 12, 21, 7, 74, 51, 159, 385, 171, 109, 18, 18}; //重量
   double y[22] = {8.26, 7.26, 5.71, 6.15, 5.90, 5.15, 5.03, 4.30, 4.15, 3.30, 2.90, 2.71, 1.96, 1.57, 1.27, 0.90, 0.70, 0.47, 0.33, 0.20, 0.09, 0.06};
   
   const int n = ArraySize(x);

   double c[1] = {x0 / 100.};
   CLSFitState state;

   CMatrixDouble xx(n, 1);
   for(int i = 0; i < n; i++)
     {
      xx[i].Set(0,x[i]);
     };

   CLSFit::LSFitCreateWFGH(xx, y, w, c, n,1,1,state);

   const double epsf = 0.000001;
   const double epsx = 0.000001;
   const int maxits = 0;
   CLSFit::LSFitSetCond(state,epsf,epsx,maxits);
   
   double bndl[1] = {0.};
   double bndu[1] = {x0 / 100.};
   CLSFit::LSFitSetBC(state,bndl,bndu);
   
   CNDimensional_PFunc *Pfunc = new CNDimensional_PFunc;

   //CAlglib::LSFitFit(state,Pfunc,,,rep,state);
   
   int info;
   CLSFitReport rep;
   CLSFit::LSFitResults(state,info,c,rep);
  };
//+------------------------------------------------------------------+
//||
//+------------------------------------------------------------------+
double func(const double _x, const double _k, const double _x0)
  {
   return((1. / 2.) * (pow(pow(_x, 2.) - 2 * _x0 * _x + pow(_x0, 2.) + 4 * pow(_k, 2.), 1. / 2.) + _x + _x0) - _x);
  };
//+------------------------------------------------------------------+
 

完成。

基本上是从 AlgLib TestInterface.mqh 复制粘贴过来的。

//+------------------------------------------------------------------+
//|AlgLibTest.mq5
//|版权所有 2022 年,MetaQuotes 有限公司。|
//|https://www.mql5.com ||
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| 脚本程序启动功能|
//+------------------------------------------------------------------+

#include <Math\Alglib\alglib.mqh>

//+------------------------------------------------------------------+
//||
//+------------------------------------------------------------------+
void OnStart()
  {
   Print(TEST_LSFit_D_NLF());
  };

//+------------------------------------------------------------------+
//| 从 CNDimensional_PFunc 派生的类|
//+------------------------------------------------------------------+
class CNDimensional_CX_1_Func : public CNDimensional_PFunc
  {
public:
   //--- 构造函数、析构函数
                     CNDimensional_CX_1_Func(void);
                    ~CNDimensional_CX_1_Func(void);
   //--- 方法
   virtual void      PFunc(double &c[], double &x[], double &func, CObject &obj);
  };
//+------------------------------------------------------------------+
//| 不带参数的构造函数|
//+------------------------------------------------------------------+
CNDimensional_CX_1_Func::CNDimensional_CX_1_Func(void)
  {

  }
//+------------------------------------------------------------------+
//| 销毁器|
//+------------------------------------------------------------------+
CNDimensional_CX_1_Func::~CNDimensional_CX_1_Func(void)
  {

  }
//+------------------------------------------------------------------+
//| 该回调函数计算 f(c,x),其中 x 是一个 |
//| X 轴上的位置,c 为可调参数。
//+------------------------------------------------------------------+
void CNDimensional_CX_1_Func::PFunc(double &c[], double &x[], double &func, CObject &obj)
  {
   double x0 = 37.74;
   func = (1.0/2.0)*(sqrt(x[0]*x[0] - 2.0*x[0]*x0 + x0*x0 + 4*c[0]) + x[0] + x0) - x[0];
  }

//+------------------------------------------------------------------+
// 只使用函数值进行非线性拟合
//+------------------------------------------------------------------+
double TEST_LSFit_D_NLF()
  {
//--- 创建变量
   CMatrixDouble           x;
   double                  y[];
   double                  c[];
   double                  w[];
   CObject                 obj;
   CNDimensional_CX_1_Func fcx1func;
   CNDimensional_Rep       frep;
   x.Resize(22,1);
//--- 初始化
   x[0].Set(0,28.98);
   x[1].Set(0,30.48);
   x[2].Set(0,30.98);
   x[3].Set(0,31.48);
   x[4].Set(0,31.98);
   x[5].Set(0,32.48);
   x[6].Set(0,32.98);
   x[7].Set(0,33.48);
   x[8].Set(0,33.98);
   x[9].Set(0,34.48);
   x[10].Set(0,34.98);
   x[11].Set(0,35.48);
   x[12].Set(0,35.98);
   x[13].Set(0,36.48);
   x[14].Set(0,36.98);
   x[15].Set(0,37.48);
   x[16].Set(0,37.98);
   x[17].Set(0,38.48);
   x[18].Set(0,38.98);
   x[19].Set(0,39.98);
   x[20].Set(0,40.98);
   x[21].Set(0,41.98);
//--- 分配
   ArrayResize(y,22);
//--- 初始化
   y[0]=8.26;
   y[1]=7.26;
   y[2]=5.71;
   y[3]=6.15;
   y[4]=5.90;
   y[5]=5.15;
   y[6]=5.03;
   y[7]=4.30;
   y[8]=4.15;
   y[9]=3.30;
   y[10]=2.90;
   y[11]=2.71;
   y[12]=1.96;
   y[13]=1.57;
   y[14]=1.27;
   y[15]=0.90;
   y[16]=0.70;
   y[17]=0.47;
   y[18]=0.33;
   y[19]=0.20;
   y[20]=0.09;
   y[21]=0.06;
//--- 分配
   ArrayResize(c,1);
//--- 初始化
   c[0] =  0.5;
//--- 创建变量
   int maxits=0;
   int info;
   CLSFitStateShell state;
   double diffstep=0.0001;
//--- 配重
   CAlglib::LSFitCreateF(x,y,c,diffstep,state);
//--- 函数调用
   double epsf = 0.0000001;
   double epsx = 0.0000001;
   CAlglib::LSFitSetCond(state,epsf,epsx,maxits);
//--- 函数调用
   CAlglib::LSFitFit(state,fcx1func,frep,0,obj);
//--- 函数调用
   CLSFitReportShell rep;
   CAlglib::LSFitResults(state,info,c,rep);
//--- 利用权重拟合
//--- (you can change weights and see how it changes result)
   ArrayResize(w,22);
//--- 初始化
   w[0]=2;
   w[1]=8;
   w[2]=2;
   w[3]=2;
   w[4]=2;
   w[5]=1;
   w[6]=11;
   w[7]=12;
   w[8]=24;
   w[9]=12;
   w[10]=6;
   w[11]=12;
   w[12]=21;
   w[13]=7;
   w[14]=74;
   w[15]=51;
   w[16]=159;
   w[17]=385;
   w[18]=171;
   w[19]=109;
   w[20]=18;
   w[21]=18;
//--- 清除权重
// ArrayFill(w,0,ArraySize(w),1);
//--- 函数调用
   CAlglib::LSFitCreateWF(x,y,w,c,diffstep,state);
//--- 函数调用
   CAlglib::LSFitSetCond(state,epsf,epsx,maxits);
//--- 函数调用
   CAlglib::LSFitFit(state,fcx1func,frep,0,obj);
//--- 函数调用
   CAlglib::LSFitResults(state,info,c,rep);
   return(c[0]);
  }
//+------------------------------------------------------------------+
 
请重新设置一个新版本,它无法编译!(
 
Koshtenko #:
请重置最新版本,它无法编译!(

请使用 Alglib 的标准版本,它早已随终端一起提供。复数类在其中进行了调整:


 
qingyouwei #:
How can i multiply two matrix  use the lib alglib ? 

Hello!

Can you compile ALGLIB and pass it?

My compiled the following error:

'complex' - unexpected token complex.mqh 37 8

'{' - name expected complex.mqh 38 3

'complex' - unexpected token, probably type is missing? complex.mqh 79 1

'im' - unexpected token, probably type is missing? complex.mqh 79 31

'im' - class type expected complex.mqh 79 31

'}' - semicolon expected complex.mqh 82 3

'}' - expressions are not allowed on a global scope complex.mqh 82 3

'complex' - unexpected token, probably type is missing? complex.mqh 86 1

'im' - unexpected token, probably type is missing? complex.mqh 86 41

'im' - variable already defined complex.mqh 86 41

see previous declaration of variable 'im' complex.mqh 79 31

'im' - class type expected complex.mqh 86      

 

我们何时添加光谱分析功能
SSA ?
ALGLIB 有这些功能,但 MT 仍然没有。
https://www.alglib.net/translator/man/manual.cpp.html#example_ssa_d_basic

 
Ivan Shishikin #:

我们何时添加光谱分析功能
SSA ?
ALGLIB 有这些功能,但 MT 仍然没有。
https://www.alglib.net/translator/man/manual.cpp.html#example_ssa_d_basic

也许在标准库没有添加之前(也许不会很快),现有的源代码(12)就可以了?

 
只有模型本身。但这还不够。
 
Ivan Shishikin #:

我们何时添加光谱分析功能
SSA ?
ALGLIB 有这些功能,但 MT 仍然没有。
https://www.alglib.net/translator/man/manual.cpp.html#example_ssa_d_basic

我认为,根本无法保证会出现这些功能......也许与其等待,不如直接在 C 代码中使用它们?