二元二次 - 有待通用化为N元N次

function Formula(){
this.a = 0;this.b = 0;this.c = 0;
this.toString=function(){
return this.a +","+this.b+","+this.c;
};
this.getY=function(x){
return this.a*Math.pow(x,2)+this.b*x+this.c;
};
this.getX=function(y,xRange){//x=[-b±√(b^2+4a(y-c))]/(2a)
var sqrt_b2_4ac = Math.sqrt(Math.pow(this.b,2)+4*this.a*(y-this.c));
var ret = (sqrt_b2_4ac - this.b)/(2*this.a);
if (xRange[0] <= ret && ret <= xRange[1]){
return ret;
}else {
return (-sqrt_b2_4ac - this.b)/(2*this.a);
}
};
}
function elimateCoefficient(p0,p1,pos){
var s = p0[pos]/p1[pos];
for(var i=0;i<4;i++){
if (pos == i){
p1[i]= 0 ;
}else{
p1[i]*=s;
p1[i] -= p0[i];
}
}
return p1;
}
function clone(ar){
var ret = new Array(ar.length);
for(var i=0;i<ar.length;i++){
ret[i] = ar[i];
}
return ret;
}
function parseFormula(points){
var p0 = y_fx(points[0]);
var p1 = y_fx(points[1]);
var p2 = y_fx(points[2]);

var p01 = elimateCoefficient(clone(p0),clone(p1),0);

var p12 = elimateCoefficient(clone(p1),clone(p2),0);

var p012 = elimateCoefficient(clone(p01),clone(p12),1);

var formula = new Formula();
formula.c = p012[3]/p012[2];
formula.b = (p01[3] - p01[2]*formula.c)/p01[1];
formula.a = (p0[3] - p0[2]*formula.c - p0[1]*formula.b)/p0[0];
return formula;
}

 

function y_fx(p){
return [Math.pow(p[0],2),p[0],1,p[1]];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值