How can we solve an equation if it has infinite number of solutions?
5 views (last 30 days)
Show older comments
I want to solve x-exp(x) - (exp(1) - 1) * (pi/2) * cos(pi * x / 2) in MATLAB. It has unlimited solutions. I want to write algorithm so that I may find solutions in a particular interval like -10 to 10, or -100 to 10, or -1000 to 10 etc. How can I do that?
0 Comments
Answers (1)
John D'Errico
on 7 Nov 2023
Edited: John D'Errico
on 7 Nov 2023
Can you ALWAYS find all solutions to a problem in some interval? NO. Sometimes, the use of returnconditions will help, since it will give you the set of all solutions, parameterized in some way. Even in this case, there are issues.
syms x
prob = x-exp(x) - (exp(1) - 1) * (pi/2) * cos(pi * x / 2)
solve(prob == 0,x,'returnconditions',true)
As you can see here, it fails, since most equations with a variable inside and outside of exponential and trig functions make an analytical solution impossible. (Not always the case, since you can read about the Lambert W, and Wright omega functions, and think about why they exist.)
But can you use a solver to find all solutions in an interval? Um, not totally true, but you can do much. First, ALWAYS PLOT WHAT YOU HAVE. LOOK AT WHAT YOU SEE.
fplot(prob,[-10,10])
ylim([-5,5])
yline(0)
grid on
So it appears there are two solutions, and as x gets larger or smaller than those limits, some thought would suggest there can not be any more than those two. (As such, your claim about infinitely many solutions seems inaccurate.)
Now just use fzero on appropriate intervals. Just looking at the plot, I'd guess [-10,-2] and [-2,10] would suffice.
Can you do better, in a semi-automatic way? Well yes. You can sample the function at many points in the interval, and look for sign changes. That does not insure success, since it can fail in two subtle ways, but with some care you can make it work most of the time. The extent of the care you make in your code will make it better code.
f = matlabFunction(prob);
xsamp = linspace(-20,20,1000);
fsamp = f(xsamp);
loc = find(diff(fsamp >= 0));
xbrak = xsamp([loc;loc+1])
As you should see, we have now found brackets on two potential roots. I could now use fzero on those intervals. Be careful, as there are problems where that simple test will fail. But any such numerical heuristic is subject to failure by someone who understands the issues and failure modes, and knows how to create a problem where those modes will operate.
[xsol,fval,exitflag] = fzero(f,xbrak(:,1))
This would tell you a simple loop on the columns of xbrak might give you want you want.
1 Comment
Walter Roberson
on 7 Nov 2023
The equations can sometimes be (much) more understandable if you are careful with converting floating point to symbolic
syms x
prob = x-exp(x) - (exp(sym(1)) - 1) * (sym(pi)/2) * cos(sym(pi) * x / 2)
solve(prob == 0,x,'returnconditions',true)
In some cases, that can make a complete difference as to whether solve() can find the solution or not... but as you can see here, sometimes solve() just is not able to find a solution anyhow.
See Also
Categories
Find more on Mathematics and Optimization 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!

