How can I find the roots of implicit function using fzero function?

19 visualizzazioni (ultimi 30 giorni)
I have written a function on MATLAB, say z=fun(x,y), here fun is an implicit function. But my question is how do I find x given y and z using "fzero" function on MATLAB? Can I write another function calling both fun and fzero?
I attempted to do this with the following code, which is another .m function file I wrote, but I got an error when I used fun2 in editor, why?
function y=fun2(y,z)
temp=fun(x,y)-z
y=fzero(@temp,0)
end

Risposta accettata

Star Strider
Star Strider il 4 Ott 2015
Modificato: Star Strider il 4 Ott 2015
I’m not certain what you’re doing. The fzero function will only find the zero of a function of a single parameter. Since ‘y’ and ‘z’ are in your function’s workspace, you would have to define your fzero function call as:
temp = @(x) fun(x,y)-z;
x = fzero(temp,0);
NOTE — This is untested code.
  4 Commenti

Accedi per commentare.

Più risposte (1)

John D'Errico
John D'Errico il 4 Ott 2015
Modificato: John D'Errico il 4 Ott 2015
You have a TWO variable problem. It is not possible to solve this using fzero. In fact, there will typically be infinitely many solutions. I'll give an example of why this is so. A trivially simple example will suffice.
f(x,y) = x^2 + y^2
Now, lets choose some value for z to which we will set f(x,y) equal to. I'll pick z=1.
f(x,y) = x^2 + y^2 = 1
Now, can we use fzero to find the solution? Of course not. The solution locus of this problem is the perimeter of a circle. Your general problem is no different. The general solution locus to such a two variable problem will be a 1-manifold of solutions, an infinite set of points in the (x,y) plane, if any solution exists at all. (All such generalizations come with counter-examples of course.)
If I had to guess, I have an idea where your confusion arises. Consider the (non-implicit) function
y = x^2
Any general function will work here here though. Now, the classical root finding problem is of the form, where we try to solve for a value of x, such that y takes on some given value, say y==5. So now we might wish to find the roots of the equation
y = x^2 = 5
Of course, this is now a ONE variable problem, not a two variable problem.
You should see the fundamental difference.
In the former case, we have ONE nonlinear equation in two unknowns. There, y is allowed to vary, along with x.
In the latter case, we have ONE nonlinear equation in ONE unknown. We have essentially set y to some given value, so then we can solve for the unknown, x.
Now, if you go back to the implicit function example, that of the circle equation, and ALSO set y to some value, then again, we can talk about roots. Fzero will find only ONE of the roots of course. So, given
x^2 + y^2 = 1
If we choose a value for y, perhaps 1 / 2, then we can solve in theory for the two solutions for x.
x^2 + (1 / 2)^2 = 1
so we have
x = +/- sqrt(3)/2
And fzero will find one of the roots, depending on you choice of starting value. For example...
f = @(x,y,z) x^2 + y^2 - z;
% Solve for x such that:
% y = 0.5
% z = 1
x = fzero(@(x) f(x,0.5,1),[0 2])
x =
0.86603
Of course this is what we expected, i.e., sqrt(3)/2.
Remember that an implicit function is not truly a function. And when you set such an expression equal to some given value, you are doing no more than creating an often infinite locus of solutions. Fzero is not equipped to handle such a problem.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by