c语言怎么都解不出来这个方程组,五个解四个未知数按道理应该是可行的,但是怎么都没思路
采用“牛顿迭代法”或者“列文伯格马夸尔特法”求解。代码太多不好贴,但是在github上有
你的方程根据t1,t2的值大概率无解。当 t1 =1 时,求解
t1 = b - a
a*a = 5*5
b*b = 4*4 - (x +2)*(x +2)
class MyCostFunction : public CostFunction{
public:
MyCostFunction (Real t1 ,Real t2):t1_(t1),t2_(t2){}
Real value(const Array& x) const override {
// QL_REQUIRE(x.size()==1,"independent variable must be 1 dimensional");
Real result = 0;
Array y(x.size());
y = values(x) ;
for (int i = 0; i < x.size(); ++i) {
result += y[i] *y[i];
}
return result/y.size();
}
Disposable<Array> values(const Array& array) const override {
Array y(array.size());
Real a = array[0];
Real b = array[1];
Real x = array[2];
y[0]=t1_ -b +a ;
y[1]=a*a - 5*5 ;
y[2]=b*b - 4*4 + (x +2)*(x +2);
return y;
}
private:
Real t1_;
Real t2_;
};
int main(){
Real esp = QL_EPSILON;
const Array initialValues = {0,0,0}; //初始迭代值
MyCostFunction costFct(1,0);
NoConstraint noConstraint; //约束条件
Problem prob(costFct, noConstraint, initialValues);
EndCriteria endCriteria(500, 100, esp, esp, esp);
LevenbergMarquardt().minimize(
prob, endCriteria);
Real a = prob.currentValue()[0];
Real b = prob.currentValue()[1];
Real x = prob.currentValue()[2];
std::cout<<"a:"<<a<<std::endl;
std::cout<<"b:"<<b<<std::endl;
std::cout<<"x:"<<x<<std::endl;
return 0 ;
}
a b c x t1 t2
哪些是已知的
你这个变量这么多,解不出来吧