Plot / Solve system for nontrivial answer. dot operator issue?

3 views (last 30 days)
Hello,
I don't use matlab much and my coding skills aren't too good. Any help would be appreciated. I am trying to solve the equation:
0.001 * 18 * ( -x/2 + x^(1/3) ) = -log(1-x) + x + 0.4*x^2
I tried:
syms x
eqn = 0.001 * 18 * ( -x/2 + x^(1/3) ) == -log(1-x) + x + 0.4*x^2 ;
S = solve(eqn,x)
But it gave me the trivial solution of x=0, which isn't what I am looking for, so I tried a plotting based code:
x= linspace(0,100);
y = -log(1-x) + x + 0.4*x^2;
q = 0.001 * 18 * ( -x/2 + x^(1/3) );
plot(y,x,q,x)
But that is also giving me issues. I think it may have something to do with using the dot operator (.) to define y and q as an array of values corresponing to the array of values of x, rather than a single value, because I remember that giving me trouble in the past, but I tried a few versions and I am still using it incorrectly. Please help.

Accepted Answer

Star Strider
Star Strider on 18 Nov 2020
Since you wnat a numeric result, use vpasolve instead, wither with an initial value (if you have an idea of what that is) or a random value (that I use here):
syms x
eqn = 0.001 * 18 * ( -x/2 + x^(1/3) ) == -log(1-x) + x + 0.4*x^2 ;
S = vpasolve(eqn,x, 'Random',1)
In different runs it returned:
0.00084760100256718346991013215880274 - 5.4840170403140472394650157772261e-34i
0.00084760100256718346991013215880166
0.00084760100256718346991013215880125 + 2.629191905223913240754608540514e-34i
The imaginary component is infinitesimal, so either disregard it (use real) or calculate the magnitude (use abs).

More Answers (1)

Jon
Jon on 18 Nov 2020
I don't have the symbolic toolbox, but you can also solve this type of problem using fzero
You need to write a little function that will equal zero at a solution to your equation, so put all of the terms on one side so that you have f(x) = 0, so make a script which includes a function definition such as shown below. You could also do this a little more compactly using anonymous functions.
Note that you are going to have problems due to the fact that log(1-x) is complex for x>1 and x^(1/3) is complex for x<0. I got around this in the example below by just taking the real part, but I don't know if this is what you are looking for
%
x0 = 3; % I put this arbitrarily you may have a better guess
x = fzero(@myfunc,x0)
function fval = myfunc(x)
% x - possible solution
%
fval = real(.001 * 18 * ( -x/2 + x^(1/3) ) -(-log(1-x) + x + 0.4*x^2))
end
  3 Comments
Thomas Ward
Thomas Ward on 18 Nov 2020
It is the Flory-Rehner equation for polymer volume saturation in a solvent. It is interesting it is returning complex roots, obviously it should be a real number, but this is the only time I have had to solve it for polymer volume saturation, if you are solving for any other variable it is much easier, but the volume saturation term is multiplied by every term in the equation, including once inside a log which was a curve ball for me.
Jon
Jon on 18 Nov 2020
By the way, I think you will get the same solution using fzero as listed above as Star Strider found using the symbolic toolbox, so that is an alternative if you don't have the symbolic solver toolbox.

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!