roots of a complicated function
Show older comments
Hello everyone, I need some help here! I have a very complicated function of x. How can I find its roots? Thank you in advance!
Accepted Answer
More Answers (1)
John D'Errico
on 25 Jun 2016
1 vote
Use fzero. Or solve. Or roots. Or vpasolve. Or fsolve. ...
Much depends on the function. How many variables? Is it in symbolic form? Are you looking for an analytical solution? All roots? (a nasty problem.)
2 Comments
Goldy
on 25 Jun 2016
John D'Errico
on 25 Jun 2016
That it uses loops is not relevant. Put a function header in front of it, so that you can evaluate the thing as a function of a vector of length 3.
Why do I say 3? Because you have ONLY 3 variables (unknowns) NOT 4.
z may be a variable as you have written it, but it is NOT an independent unknown.
So you will have a function, perhaps called F, that takes a single vector. That vector must be of length 3. Inside the function, you will extract x,y,Z. Then create z from f(x), and evaluate your messy function in those loops.
The problem you will find is that NO rootfinder, including solve, vpasolve, fzero, roots, vpasolve, will work! In fact, this would be your next anguished question, "Why does my favorite root finder not work here?"
Ok, so why do I say that? Assuming that your messy relation here is a continuous function of the input unknowns (x,y,Z), then there will be generally infinitely many solutions if there are any solutions.
You can think of this as a contour plotting problem, with three dimensions. A contour plot (in two dimensions, can be viewed as finding the set of values for which some function w(u,v)==0, or any other fixed constant. In fact, contour plotters are usually used to solve that problem for various values of the fixed constants. This is called a level surface problem. Here, you have the same thing, but as a function of THREE input variables. So effectively, the general "solution" to your problem will look like a surface in three dimensions, what I might call a 2-manifold, embedded in the three dimensional space (x,y,Z). For example, the surface of a sphere would be one such case.
Ok, so how do you solve THAT problem? The simplest solution is to use a tool like isosurface.
You will need to compute the value of F at EVERY point in a 3-dimensional grid on the variables x,y,Z. So, use the function ndgrid to create all combinations of those variables. Then evaluate your function at EVERY combination of values, and finally call isosurface, like this:
isosurface(x,y,Z,values,0)
Categories
Find more on Quadratic Programming and Cone 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!