Solving a symbolic, nonlinear equation in Matlab/Octave

3.6k Views Asked by At

I'm trying to solve the following steady-state equation for x:

0 = -C + 2*C0*(1-exp(-k*A*x*phi))

I've defined all variables as syms, but can't figure out how to solve the equation for x. Since all other variables are known, I've tried substituting them in:

f = -C + 2*C0*(1-exp(-k*A*x*phi))
subs(f, [C 20], [C0 11], [k .015], [A .031], [phi .01])

But this also doesn't work.

1

There are 1 best solutions below

3
On

The correct way to replace symbolic variables with values using subs is to use the three input variant. The first is the symbolic expression, the second is an array of symbolic variables to replace, and the third is an array of things that you want to replace each variable in the second input with.

syms C C0 k A x phi

f = -C + 2*C0*(1-exp(-k*A*x*phi));

% Substitute in values that are known
newf = subs(f, [C, C0, k, A, phi], [20, 11, 0.015, 0.031, 0.01]);
%   2 - 22*exp(-(93*x)/20000000)

% Solve the resulting symbolic expression for x
result = solve(newf == 0, x)
%   (20000000*log(11))/93

% And if you need a numeric (rather than symbolic) result
double(result)
%   5.1568e+05