1

我开始使用mathjs并尝试重写一些sympy用于代数计算的 Python 代码。我在代码中实现了从研究文章中获得的拟合函数(如果感兴趣,请参阅代码片段中的链接),并且需要重新排列/求解摩尔分数(变量x_1)或密度(变量rho)的方程。

文档中给出的示例所示,我将拟合方程定义为我感兴趣的两个变量的函数,如下所示:

const eqn = math.compile('f(x_1, rho) = param_1 + param_2 * temp + param_3 * rho + param_4 * rho^2 + param_5 * rho^(-1) + param_6 * temp * rho + param_7 * temp^(-1) - x_1')

为了解决我感兴趣的变量的拟合方程,我定义了两个不同的范围。它们都包含相同的常数参数param_1toparam_7和温度值temp。由于我想使用solve for,x_1我需要提供rho我添加到第一个范围(scope1)中的缺失变量。为了求解方程,rho我将缺失的变量添加x_1到第二个作用域 ( scope2)。

有了这些解释,我的代码如下所示:

const math = require('mathjs');

// physical properties and fit parameter
// fit from: 'doi:10.3390/fermentation4030072'
let scope1 = {
    // this scope is for solving the equation for `x_1`        
    param_1: -96.32780,     // -
    param_2: -0.02856512,   // 1/K
    param_3: 98.96611,      // cm3/g
    param_4: -37.81838,     // cm6/g2
    param_5: 35.07342,      // g/cm3
    param_6: 0.02844898,    // cm3/gK
    param_7: 36.74344,      // K
    temp: 293.15,
    rho: 0.93167,
}

let scope2 = {
    // this scope is for solving the equation for `rho`
    param_1: -96.32780,     // -
    param_2: -0.02856512,   // 1/K
    param_3: 98.96611,      // cm3/g
    param_4: -37.81838,     // cm6/g2
    param_5: 35.07342,      // g/cm3
    param_6: 0.02844898,    // cm3/gK
    param_7: 36.74344,      // K
    temp: 293.15,
    x_1: 0.224269,
}

const eqn = math.compile('f(x_1, rho) = param_1 + param_2 * temp + param_3 * rho + param_4 * rho^2 + param_5 * rho^(-1) + param_6 * temp * rho + param_7 * temp^(-1) - x_1')

// compiled expression/equation needs to be solved for `x_1`
const res1 = eqn.eval(scope1)
console.log(res1)

// compiled expression/equation needs to be solved for `rho`
const res2 = eqn.eval(scope2)
console.log(res2)

这给出了以下输出:

{ [Function: f] signatures: { 'any,any': [Function] }, syntax: 'f(x_1, rho)' }
{ [Function: f] signatures: { 'any,any': [Function] }, syntax: 'f(x_1, rho)' }

如何分别从 resultsres1或中得到结果res2

4

0 回答 0