码迷,mamicode.com
首页 > 其他好文 > 详细

解方程 (允许误差)

时间:2014-07-01 08:14:48      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   2014   os   javascript   

解方程 (允许误差)


1. 确定范围
2. 确定误差
3. 确定增量
4. 在区间内执行方程,满足误差存入解集
5. 返回解集


var solveFun = function(
f /*function*/,
c /*closest to 0*/,
from /*from what number*/,
to /*to what number*/,
ic /*increasement*/){


var arr = new Array();
for(var i =from ; i <= to ; i+=ic){
var r = f(i);
if(Math.abs(r) <= c) {arr.push({x:i,y:r});}
}


if(arr.length == 0) {return undefined;}


return arr;
}


var ret = solveFun(function (n){return n*n*n -3*n + 1},0.001,-10,20,0.001);


if(ret == undefined){console.log("no answer.");}


else {
console.log(ret.length + "answer found ");
for(var i = 0 ;i < ret.length; i++)
console.log("x = " + ret[i].x + ", y= " + ret[i].y);


}


解方程 (允许误差),布布扣,bubuko.com

解方程 (允许误差)

标签:style   blog   java   2014   os   javascript   

原文地址:http://blog.csdn.net/lan_liang/article/details/36014263

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!