Rud Banisterloid
Rud Banisterloid

Reputation: 315

how to plot x,y,z in matlab?

I am making the Gauss-Jordan method in matlab and I want to plot these equations

x + y + 4*z = -1 
-2*x – y + z= -5 
3*x-2*y+3*z=-4

To see in what point of the graph they intersect, but I do not know how to plot in matlab

Upvotes: 0

Views: 901

Answers (3)

Benoit_11
Benoit_11

Reputation: 13945

Is this what you are looking for?

clc
clear
close all

%// Generate x and y values to plot from.
[x,y] = meshgrid(linspace(0,10,100),linspace(0,10,100));

%// Get equation for plane; i.e. z position
z1 = 0.25.*(-1-x-y);
z2 = -5+y+2*x;
z3 = (-4-3.*x+2.*y)./3;

%// Use surf to generate surface plots
figure;

surf(x,y,z1,'linestyle','none','facealpha',0.4)
hold on
surf(x,y,z2,'linestyle','none','facealpha',0.4)
surf(x,y,z3,'linestyle','none','facealpha',0.4)
hold off

%// Use to manually rotate the plot
rotate3d on

Which gives this:

enter image description here

You can play around with the 'FaceAlpha' property of course to make things clearer. Have a look at the surf function for more options.

EDIT: Alternatively to @rayryeng solution to solve for x,y and z you can use mldivide:

A = [1 1 4;-2 -1 1;3 -2 3];
B = [-1;-5;-4];

X = mldivide(A,B)

X =

    1.0000
    2.0000
   -1.0000

Upvotes: 3

rayryeng
rayryeng

Reputation: 104504

Though this is not plotting, perhaps this is also something you can use. If you want to determine the simultaneous solution to those equations, consider using solve

syms x y z
A = solve('x + y + 4*z == -1', '-2*x - y + z == -5', '3*x - 2*y + 3*z == -4')
disp([A.x A.y A.z]);

[ 1, 2, -1]

This will check to see whether your Gauss-Jordan elimination is correct. If you don't like using solve, you can use linear algebra to help you solve this for you. Simply place your coefficients for your system in a matrix and vector, then find the inverse of the matrix and multiply by the vector.

A = [1 1 4; -2 -1 1; 3 -2 3];
b = [-1;-5;-4];
x = A \ b

x =

1.0000
2.0000
-1.0000

... and even another method is to use rref to reduce your system into row-reduced echelon form. This would be the result after you successfully apply Gauss-Jordan elimination to your linear system. As such:

A = [1 1 4; -2 -1 1; 3 -2 3];
b = [-1;-5;-4];
rref([A b])


ans =

 1     0     0     1
 0     1     0     2
 0     0     1    -1

Reading the above answer, x = 1, y = 2, z = -1. This represents the augmented system where the first 3 columns denote the coefficients of your system and the fourth column denotes the right hand side of your linear system of equations.

Upvotes: 2

Nras
Nras

Reputation: 4311

Here is how i would plot those planes. The 4-th argument of surf lets you specify the color.

% // create function handles of form z = f(x,y)
f1 = @(X,Y) 1/4*(-1 - X -Y);
f2 = @(X,Y) -5 + 2*X + Y;
f3 = @(X,Y) 1/3*(-4 -3*X + 2*Y);

% // create a 2d-grid to plot the functions over
x = linspace(-5, 5, 10);
y = linspace(-10, 10, 20);
[X,Y] = meshgrid(x,y);

% // plot the planes in different colors (4th argument) and without edges
figure
surf(X, Y, f1(X,Y), ones(size(f1(X,Y))), 'EdgeColor', 'None');
hold on
surf(X, Y, f2(X,Y), ones(size(f2(X,Y)))*2, 'EdgeColor', 'None');
surf(X, Y, f3(X,Y), ones(size(f3(X,Y)))*3, 'EdgeColor', 'None');
legend('plane1', 'plane2', 'plane3')
xlabel('x'), ylabel('y'), zlabel('z')

enter image description here

Upvotes: 2

Related Questions