How to get all the solution of a function?

39 views (last 30 days)
syms x;
f =@(x) 14*x.*exp(x-2) -12*exp(x-2) - 7*x.^3+ 20*x.^2 - 26*x + 12;
sol = solve(f(x),x)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
sol = 
0.85714285714285714285714285714286
This function has two solutions, but solve() shows only one. I have also tried vpasolve.
sol = vpasolve(f(x),x)
sol = 
0.85714285714285714285714285714286
How to get both the solutions?
By the way, not in this way as follows:
sol = vpasolve(f(x),x,3)
sol = 
2.0000000000000073615018252131379

Accepted Answer

Walter Roberson
Walter Roberson on 23 Aug 2021
syms x;
f =@(x) 14*x.*exp(x-2) -12*exp(x-2) - 7*x.^3+ 20*x.^2 - 26*x + 12;
fx = f(x)
fx = 
factors = factor(fx)
factors = 
digits(50)
partial_sols = arrayfun(@(E) solve(E,x), factors, 'uniform', 0)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
partial_sols = 1×3 cell array
{0×1 sym} {1×1 sym} {1×1 sym}
sols = horzcat(partial_sols{:})
sols = 

More Answers (1)

KSSV
KSSV on 23 Aug 2021
Refer this, you need to provide close approximate solution value to get both the solutions.
  2 Comments
Apurbo Roy Chowdhury
Apurbo Roy Chowdhury on 23 Aug 2021
What if I don't know the solutions before solving and how many solutions are there?
Walter Roberson
Walter Roberson on 23 Aug 2021
Just as I suspected...
You are looking for options that you can pass to vpasolve() to get it to return all roots of general nonlinear equations (though perhaps you would be willing to exclude functions that have an infinite number of solutions, as long as it would return all 9 billion solutions if that was the number the function had.)
Unfortunately, it has been mathematically proven for that to be impossible for some functions. There are some functions in which knowing where many or even all of the other roots are, does not give you any information about the locations of the remaining roots.
The task might be possible for some classes of functions, but might be tricky or impossible for other functions.
For example, suppose you had
syms c d1 d2 real
syms x
solve((x - d1)*(x - d2)*exp((x - d1)*(x - d2)) == c, x)
How many real-valued solutions does it have? MATLAB says it cannot find any solutions. Maple says
d1/2 + d2/2 + sqrt(d1^2 - 2*d1*d2 + d2^2 + 4*LambertW(c))/2
d1/2 + d2/2 - sqrt(d1^2 - 2*d1*d2 + d2^2 + 4*LambertW(c))/2
That is the same expression inside the sqrt() for the two of them, so you can ask about whether d1^2 - 2*d1*d2 + d2^2 + 4*LambertW(c) is non-negative so that the sqrt() will be real-valued . So at first glance it looks like the number of real solutions should be 0 (of that term inside the sqrt() is negative), or 2 (if it is not). But! LambertW has two real solutions for c in the range -1/e to 0 (exclusive on both sides), so a more correct analysis would be 0, or 2, or 4 plus the additional possibility that the expression inside the sqrt() is not negative as such but rather is complex-valued because the LambertW is complex valued for c < -1/e
But... what if d1 == d2? Does that change the number of solutions? In that case, the solutions devolve to
d1 +/- sqrt(LambertW(c))
LambertW( c) is complex for c < -1/e and has two negative values for c in the range -1/e to 0, and one positive value for c > 0 . So sqrt(LambertW( c)) would be complex for c < 0 and the additional solutions case cannot apply. So now you also have to worry about whether LambertW(c ) is more negative than (x-d1)*(x-d2) is positive. However, to be fair, that can occur even with d1 and d2 not being equal, so this is not really a new problem.
Anyhow... you could imagine other situations like plain (x-d1)*(x-d2) in which case the number of solutions depends upon whether d1 == d2. And any practical algorithm that had to proceed numerically (because there might be a nonlinear component) would have to worry about whether d1 == d2 to within calculation precision even if they are different.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!