I started to work with mathjs
and tried to rewrite some Python code using sympy
for algebraic calculations. I implemented a fit function I got from a research article (see link in code snippet if interested) in code and need to rearrange/solve that equation for either the mole fraction (variable x_1
) or the density (variable rho
).
As shown in the examples given in the docs I defined the fit equation as a function of the two variables I am interested in like this:
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')
In order to solve the fit equation for the variables I am interested in I defined two different scopes. Both of them contain the same constant parameters param_1
to param_7
and the temperature value temp
. Since I want to use solve for x_1
I need to provide the missing variable rho
which I added to the first scope (scope1
). To solve the equation for rho
I added the missing variable x_1
to the second scope (scope2
).
With those explanations, my code looks like that:
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)
Which gives the following output:
{ [Function: f] signatures: { 'any,any': [Function] }, syntax: 'f(x_1, rho)' }
{ [Function: f] signatures: { 'any,any': [Function] }, syntax: 'f(x_1, rho)' }
How do I get the results out of the results res1
or res2
, respectively?