Solving system of Non-linear Equations graphically

71 views (last 30 days)
Problem Statement: I want to solve a set of equations graphically by plotting their graphs and looking at the interesection. I know how to solve a linear system of equations in 2D, like this.
%Simulataneous Equations
%3x1+2x2 =10
% 2x1 - x2 =2
%Graphical Solution
x= 0:0.1:10;
x1 = (1/3).*(18-2.*x);
x2= -2 + 2.*x;
plot(x,x1,'r')
hold on
plot (x,x2,'b');
grid on
xlabel('x1')
ylabel('x2')
Now I want to do a similar thing but for a set of equations with two variables instead of one. The equations are
I want them to plot like I could for above example and see their intersection. I know second equation is just a constant and hence i expected a plane passing through the value 5.
My attempt:
% Making an vector for input variable
x1_1= linspace(0,20);
y1_1=ones(1,100)*5;
%making a grid
[x1_1,y1_1] = meshgrid(x1_1,y1_1);
%The third equation of the set
z1_1= (1 + x1_1.*y1_1)./(x1_1+x1_1^2);
%making a vector for other plot
y1_2=ones(1,100)*5;
z1_2 = linspace(0,20);
%making a grid
[y1_2,z1_2] = meshgrid(y1_2,z1_2);
x1_2= 1+y1_2-z1_2;
% Plotting for 3D graph.
surf(x1_1,y1_1,z1_1);
hold on
surf(x1_2,y1_2,z1_2);
I did not do for the third equation but all I get are two lines instead of two planes intersecting at some point. If any help or a small hint with the code can be provided, i would be thankful to you.
Thank you.

Accepted Answer

Ameer Hamza
Ameer Hamza on 23 Apr 2020
try this
figure;
ax = axes();
view(3);
hold(ax);
% First equation
x1 = linspace(-20,20);
y1 = linspace(-20,20);
%making a grid
[x1,y1] = meshgrid(x1,y1);
%The third equation of the set
z1= 1 + y1 - x1;
surf(ax, x1, y1, z1, 20*ones(size(x1)));
% Second equation
x1 = linspace(-20,20);
z1 = linspace(-20,20);
[x1,z1] = meshgrid(x1,z1);
y1 = 5*ones(size(x1));
surf(ax, x1, y1, z1, 10*ones(size(x1)));
% Third equation
x1 = linspace(-20,20);
y1 = linspace(-20,20);
%making a grid
[x1,y1] = meshgrid(x1,y1);
%The third equation of the set
z1= (1 + x1.*y1)./(x1+x1.^2);
surf(ax, x1, y1, z1, 1*ones(size(x1)));
zlim([-20 20])
legend({'Eq1', 'Eq2', 'Eq3'})
  6 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance 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!