用 math.js 求解 Lotka–Volterra 方程
Solving Lotka–Volterra equations with math.js
math.js主页上有一个解微分方程的例子math.js,但是比较复杂,没有提供足够的信息让我个人能够应用math.js对于其他类似问题。所以,我要做的是解决 Lotka–Volterra equations for predator-prey simulation。系统中有两个方程:
dx/dt = ax - bxy
dy/dt = cxy - y
在 math.js 中完成此操作,我得到了
math.import({ndsolve:ndsolve});
const sim2 = math.parser();
sim2.eval("dxdt(x, y) = x - x * y");
sim2.eval("dydt(x, y) = x * y - y");
sim2.eval("dt = 1.0 s"); // Simulation timestep
sim2.eval("x0 = 0");
sim2.eval("y0 = 0");
sim2.eval("tfinal = 100 s"); // Simulation duration
sim2.eval("result_stage1 = ndsolve([dxdt, dydt], [x0, y0], dt, tfinal)");
其中 ndsolve
来自火箭轨迹示例:http://mathjs.org/examples/browser/rocket_trajectory_optimization.html.html
function ndsolve(f, x0, dt, tmax) {
var n = f.size()[0]; // Number of variables
var x = x0.clone(); // Current values of variables
var dxdt = []; // Temporary variable to hold time-derivatives
var result = []; // Contains entire solution
var nsteps = math.divide(tmax, dt); // Number of time steps
for(var i=0; i<nsteps; i++) {
// Compute derivatives
for(var j=0; j<n; j++) {
dxdt[j] = f.get([j]).apply(null, x.toArray());
}
// Euler method to compute next time step
for(var j=0; j<n; j++) {
console.log(x.get([j]));
console.log(dt);
x.set([j], math.add(x.get([j]), math.multiply(dxdt[j], dt)));
}
result.push(x.clone());
}
return math.matrix(result);
}
但是,运行这段代码我得到了错误
Unexpected type of argument in function add (expected: number or Complex or BigNumber or Fraction, actual: Unit, index: 1)
此错误的来源是什么?我错过了正确的单位吗?
感谢任何帮助。
解决方案:删除所有单位,如 's'、'm/s' 等。或者,所有单位都必须匹配。
math.js主页上有一个解微分方程的例子math.js,但是比较复杂,没有提供足够的信息让我个人能够应用math.js对于其他类似问题。所以,我要做的是解决 Lotka–Volterra equations for predator-prey simulation。系统中有两个方程:
dx/dt = ax - bxy
dy/dt = cxy - y
在 math.js 中完成此操作,我得到了
math.import({ndsolve:ndsolve});
const sim2 = math.parser();
sim2.eval("dxdt(x, y) = x - x * y");
sim2.eval("dydt(x, y) = x * y - y");
sim2.eval("dt = 1.0 s"); // Simulation timestep
sim2.eval("x0 = 0");
sim2.eval("y0 = 0");
sim2.eval("tfinal = 100 s"); // Simulation duration
sim2.eval("result_stage1 = ndsolve([dxdt, dydt], [x0, y0], dt, tfinal)");
其中 ndsolve
来自火箭轨迹示例:http://mathjs.org/examples/browser/rocket_trajectory_optimization.html.html
function ndsolve(f, x0, dt, tmax) {
var n = f.size()[0]; // Number of variables
var x = x0.clone(); // Current values of variables
var dxdt = []; // Temporary variable to hold time-derivatives
var result = []; // Contains entire solution
var nsteps = math.divide(tmax, dt); // Number of time steps
for(var i=0; i<nsteps; i++) {
// Compute derivatives
for(var j=0; j<n; j++) {
dxdt[j] = f.get([j]).apply(null, x.toArray());
}
// Euler method to compute next time step
for(var j=0; j<n; j++) {
console.log(x.get([j]));
console.log(dt);
x.set([j], math.add(x.get([j]), math.multiply(dxdt[j], dt)));
}
result.push(x.clone());
}
return math.matrix(result);
}
但是,运行这段代码我得到了错误
Unexpected type of argument in function add (expected: number or Complex or BigNumber or Fraction, actual: Unit, index: 1)
此错误的来源是什么?我错过了正确的单位吗?
感谢任何帮助。
解决方案:删除所有单位,如 's'、'm/s' 等。或者,所有单位都必须匹配。