Plot / Solve system for nontrivial answer. dot operator issue?
Show older comments
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
More Answers (1)
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
Jon
on 18 Nov 2020
you could also take the abs rather than the real part if that is what you are looking for. I'm also wondering whether there is some issue with your problem formulation that you end up with an equation with complex roots.
Thomas Ward
on 18 Nov 2020
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.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!