繁体   English   中英

JS:解决 x 的公式

[英]JS: Resolve a Formula for x

我有一个包含多个组件的公式,假设w = x * y / z^2 + c 现在我为每个变量都有一个输入字段。 我的目标是,尽快计算丢失的一个,因为所有其他人都输入了。 困难在于,您可以选择要填写哪些字段以及要保留哪些字段。

(naive) way would of course be to resolve it for each variable by hand, detect the missing var, and have seperate js functions for each case.(天真)的方法当然是手动为每个变量解析它,检测丢失的变量,并为每种情况使用单独的 js 函数。 但我什至还链接了公式(如上面公式中的x = a + b也是x = a + b ),并且选项几乎是不定式的。 a formula by a specified variable? JS 中是否有任何选项可以通过指定变量公式? 然后我可以用分配的值替换每个变量字符串,然后eval字符串。

一开始我以为是Nerdamer ,但结果证明它只能计算表达式而不能处理方程。

这可能吗? 有什么更好的主意吗?

提前致谢!

PS:我的实际公式是:

dR = c * I^2 / A
R = L * dR
P = I * U
DV = R * I
DW = DV * I

它用于计算由于欧姆电阻引起的电缆损耗。 每个变量都有一个对应的输入字段。

可以构建以下解决方案以使用nerdamer查找“R”。 该逻辑可以扩展到求解剩余的变量。 请记住,当前的限制是 nerdamer 目前只能以代数方式求解三次函数。 高阶函数将通过数值求解。

 //You can then take care of the non linear containing I. I is quadratic var dR = nerdamer('R=L*dR').solveFor('dR'); var I = nerdamer('dR=c*I^2/A').sub('dR', dR).solveFor('I'); //You can first start by reducing the first few equations since they are linear and you can solve them as a linear system var solutions = nerdamer.solveEquations(['P = I * U', 'DV = R * I', 'DW = DV * I'], ['I', 'DW', 'P']); //the solutions come back as an array arrays in the form of [variable, value] //you can see what they look like. In your case all your solutions will be in terns of DV & U since these are the only actual knowns //You can see what the solutions look like solutions.map(function(x) { console.log('-'+x[0]+' = '+x[1]); }); console.log('------------------ R ----------------'); var R = nerdamer.setEquation(I[0], solutions[0][1]).solveFor('R'); //I will have 3 solutions since it's cubic. You can console.log them below R.map(function(x) { console.log('R = '+x.toString()); });
 <script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/nerdamer.core.js"></script> <script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Algebra.js"></script> <script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Calculus.js"></script> <script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Extra.js"></script> <script src="https://cdn.jsdelivr.net/npm/nerdamer@latest/Solve.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM