Plot / Solve system for nontrivial answer. dot operator issue?
3 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
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.00084760100256718346991013215880166
0.00084760100256718346991013215880125 + 2.629191905223913240754608540514e-34i
2 件のコメント
その他の回答 (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))
end
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!