MATLAB Answers

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

조회 수: 1(최근 30일)
Thomas Ward
Thomas Ward 2020년 11월 18일
댓글: Jon 2020년 11월 18일
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) );
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.

채택된 답변

Star Strider
Star Strider 2020년 11월 18일
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.00084760100256718346991013215880125 + 2.629191905223913240754608540514e-34i
The imaginary component is infinitesimal, so either disregard it (use real) or calculate the magnitude (use abs).

추가 답변(1개)

Jon 2020년 11월 18일
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))
  댓글 수: 3
Jon 2020년 11월 18일
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.

댓글을 달려면 로그인하십시오.




Community Treasure Hunt

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

Start Hunting!

Translated by