% Program to illustrate nonlinear equation solving involving
% 2 equations in 2 unknowns.
% The solution points are (1,2) and (-1,2).
% DPO 11/02

% Set up some x and y values for function evaluation.

x = -6:.1:6;
[X,Y] = meshgrid(x,x);

% Evaluate the two functions.

f1 = X.^2 + Y.^2 - 5;
f2 = (Y - X.^2 - 1);

% Plot the 1st function.

figure(1)
mesh(X,Y,f1)
title('f1 = x^2 + y^2 - 5')

% Plot the 2nd function.

figure(2)
mesh(X,Y,f2)
title('f2 = y - x^2 - 1')

% Plot the set of points (x,y) where each function is zero.

figure(3)
contour(X,Y,f1,[0 0],'g')
hold on
contour(X,Y,f2,[0 0],'r')
axis([-3 3 -3 3])

% Choose two points.  At the first, the functions are both negative.

disp('First point:')
x1 = -.5; y1 = 1;
disp([x1 y1])
f1x1 = x1.^2 + y1.^2 - 5;
f2x1 = (y1 - x1.^2 - 1);
disp(sprintf('f1 = %f, f2 = %f',f1x1,f2x1))

% At the second, the functions are both positive.

disp('Second point:')
x2 = 1; y2 = 2.5;
disp([x2 y2])
f1x2 = x2.^2 + y2.^2 - 5;
f2x2 = (y2 - x2.^2 - 1);
disp(sprintf('f1 = %f, f2 = %f',f1x2,f2x2))

% Connect the points by a blue line.
plot([x1 x2],[y1 y2],'b')
legend('f1','f2','line from neg values to pos')