Full plot of an implicit equation

I have the equation;
x^2+y^2=1+4.5*sin^2(xy)
I want to plot this equation, and was explicitly told to use the plot function, instead of ezplot or contour. Since the equation is implicit, I tried using fzero to solve for a set of x.
f=@(x,y) x.^2+y.^2-1-4.5.*sin(x.*y).^2;
xv=linspace(-3,3);
yv=zeros(size(xv));
for i=1:length(xv);
yv(i)=fzero(@(y) f(xv(i),y), 0);
end
plot(xv, yv)
However, this obviously only gives one y for each x, while the equation can be satisfied by several y values for some x.
How do I add these additional solutions, and add them to my plot?

 Accepted Answer

Star Strider
Star Strider on 8 Oct 2015
Edited: Star Strider on 8 Oct 2015
One option is to do a second fzero call in each iteration, with a different initial parameter estimate:
f=@(x,y) x.^2+y.^2-1-4.5.*sin(x.*y).^2;
xv=linspace(-3,3);
yv=zeros(size(xv));
for i=1:length(xv);
yv(i,1)=fzero(@(y) f(xv(i),y), 1);
yv(i,2)=fzero(@(y) f(xv(i),y), -1);
end
plot(xv, yv)
This produces an additional negative mirror image of the single-call loop. You could expand on this with other initial parameter estimates (perhaps ±5, ±10, &c.) to see if you got other results. Experiment with it!

5 Comments

It's a step. But it's a guessing-game I can't win. I've managed to find about 2/3 of the plot, but I can't find the rest. Is there no simpler way?
The only simpler way is the one forbidden to you: the contour function. It’s the one I always go to when I want to find the roots of a bivariate function.
The option I would try would be to give a range of (x,y) values, plot it, and see what the results are. That’s how I initially analyse all functions.
I also would not say you could not win it. Looking at your equation, it should be ‘symmetrical’ (for lack of a more appropriate term) in both variables, so solutions with fixed ‘x’ should be the same as those with fixed ‘y’. (I’m not sure exactly how best to express that, but I’m certain you understand my intent.) Also, it could have complex roots. I believe fzero limits itself to real roots only. Some Optimization Toolbox functions might help you find complex roots, if you have access to it and are allowed to use them.
I fully understand what you mean. Most importantly, if you were to "swap places" of all the X and Y values, the result would be identical. While I could use this to fill in the gap, by rotating by 90 degrees, it's not very satisfying. I mean, sure, it works in this specific case, but if it wasn't completely symmetrical that would be near impossible.
I solved it by plotting both xv,yv and yv,xv. Not the best way, but it works. Thanks!
My pleasure!
I agree it may not be the best way in a more inclusive perspective, but you were given what I consider to be a somewhat difficult problem. I imagine the problem was presented to you specifically so that you would see the symmetry properties and use them to your advantage.
In a non-symmetrical sort of problem, you would either have to use contour or do a rather exhaustive search for the zeros of the function. That is definitely not something I would want to do, especially if contour had already solved the programming problem for me.

Sign in to comment.

More Answers (0)

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!