Plotting Symbolic 3x3 System of Equations

I have solved the following 3x3 system of equation
%Define symbolic variables and equations
syms x y w
eq1 = 2*x -y + w == -5;
eq2 = y + 3*w == 7;
eq3 = w==2;
% 2. Solve the system
[xsol, ysol, wsol] = solve([eq1, eq2, eq3], [x, y,w])
xsol = 
ysol = 
1
wsol = 
2
I would like to produce the 3D solution for a Linear Algebra textbook. Is there any more effective way than the following? This textbook is for high school students
% Numeric intersection point
pt = double([S.x, S.y, S.w]);
Unable to resolve the name 'S.x'.
% Define plotting grid
[xg, yg] = meshgrid(linspace(pt(1)-5, pt(1)+5, 30), linspace(pt(2)-5, pt(2)+5, 30));
% Plane 1: 2*x - y + z = -5 -> z = -5 - 2*x + y
z1 = -5 - 2*xg + yg;
% Plane 2: y + 3*z = 7 -> z = (7 - y)/3
z2 = (7 - yg) / 3;
% Plane 3: z = 2
z3 = 2*ones(size(xg));
% Plot
figure
hold on
surf(xg, yg, z1, 'FaceAlpha',0.5, 'EdgeColor','none', 'FaceColor',[0.8 0.4 0.4])
surf(xg, yg, z2, 'FaceAlpha',0.5, 'EdgeColor','none', 'FaceColor',[0.4 0.8 0.4])
surf(xg, yg, z3, 'FaceAlpha',0.5, 'EdgeColor','none', 'FaceColor',[0.4 0.4 0.8])
xlabel('x'), ylabel('y'), zlabel('w')
legend('Plane 1','Plane 2','Plane 3','Location','best')
axis equal
grid on
view(3)
hold off

1 Comment

%Define symbolic variables and equations
syms x y w
eq1 = 2*x -y + w == -5;
eq2 = y + 3*w == 7;
eq3 = w==2;
% 2. Solve the system
S = solve([eq1, eq2, eq3], [x, y,w])
S = struct with fields:
x: -3 y: 1 w: 2
% Numeric intersection point
pt = double([S.x, S.y, S.w]);
% Define plotting grid
[xg, yg] = meshgrid(linspace(pt(1)-5, pt(1)+5, 30), linspace(pt(2)-5, pt(2)+5, 30));
% Plane 1: 2*x - y + z = -5 -> z = -5 - 2*x + y
z1 = -5 - 2*xg + yg;
% Plane 2: y + 3*z = 7 -> z = (7 - y)/3
z2 = (7 - yg) / 3;
% Plane 3: z = 2
z3 = 2*ones(size(xg));
% Plot
figure
hold on
surf(xg, yg, z1, 'FaceAlpha',0.5, 'EdgeColor','none', 'FaceColor',[0.8 0.4 0.4])
surf(xg, yg, z2, 'FaceAlpha',0.5, 'EdgeColor','none', 'FaceColor',[0.4 0.8 0.4])
surf(xg, yg, z3, 'FaceAlpha',0.5, 'EdgeColor','none', 'FaceColor',[0.4 0.4 0.8])
xlabel('x'), ylabel('y'), zlabel('w')
legend('Plane 1','Plane 2','Plane 3','Location','best')
axis equal
grid on
view(3)
hold off

Sign in to comment.

Answers (2)

%Define symbolic variables and equations
syms x y w
eq1 = 2*x -y + w == -5;
eq2 = y + 3*w == 7;
eq3 = w==2;
equations = [eq1, eq2, eq3];
fimplicit3(equations)
Now this doesn't look like the plot you showed. There's a reason for that; the ordering of the variables is different than you expect.
s = symvar(equations);
xlabel(string(s(1)))
ylabel(string(s(2)))
zlabel(string(s(3)))
If you need w to be along the "z" axis you could change the view on the axes.
This is a meaningful contribution toward writing a linear algebra textbook for high school students. When three non-parallel planes intersect, there are three possible scenarios. You have demonstrated the case of one unique solution (a single intersection point). Here are the other two scenarios: infinite solutions and no solution.
Note that I can't use fsurf in these two scenarios because MATLAB assumes that any expression corresponds to z. Therefore, I use fimplicit3 instead.
syms x y z
% ===============================
%% Scenario 2: Infinite Solutions
% ===============================
% 3D plotting limits [xmin, xmax, ymin, ymax, zmin, zmax] for Scenario 2
limit2 = [0, 5.01, -4.01, 4.01, -5, 5];
figure
hold on
fimplicit3(x - y == 0, limit2, 'FaceAlpha', 0.25, 'EdgeColor', 'none', 'FaceColor', [0.8 0.4 0.4]);
fimplicit3(x + y == 0, limit2, 'FaceAlpha', 0.25, 'EdgeColor', 'none', 'FaceColor', [0.4 0.8 0.4]);
fimplicit3(x - 10*y == 0, limit2, 'FaceAlpha', 0.25, 'EdgeColor', 'none', 'FaceColor', [0.4 0.4 0.8]);
hold off
grid on
view(3)
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Z Axis')
title('The Binding of a Book (Infinite Solutions)')
% ===============================
%% Scenario 3: No Solution
% ===============================
% 3D plotting limits for Scenario 3
limit3 = [1, 5.01, -4.01, 4.01, -5, 5];
figure
hold on
fimplicit3(x + y == 1, limit3, 'FaceAlpha', 0.25, 'EdgeColor', 'none', 'FaceColor', [0.8 0.4 0.4]);
fimplicit3(x - y == 1, limit3, 'FaceAlpha', 0.25, 'EdgeColor', 'none', 'FaceColor', [0.4 0.8 0.4]);
fimplicit3(x == 5, limit3, 'FaceAlpha', 0.25, 'EdgeColor', 'none', 'FaceColor', [0.4 0.4 0.8]);
hold off
grid on
view(3)
xlabel('X Axis')
ylabel('Y Axis')
zlabel('Z Axis')
title('A Hollow Triangular Prism (No Solution)')

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2026a

Answered:

on 14 Aug 2026

Community Treasure Hunt

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

Start Hunting!