how to plot x,y,z in matlab? - 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

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:
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

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')

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.

Related

Applying 3D rotation matrix to x, y, z values obtained from surface function

Applying 3D rotation matrix to the x,y,z values obtained from surface function object. The error I get is due to the matrix not being nonconforment but how can I adjust the matrix correctly?
I know hgtransform / makehgtform can do rotations but I need to use rotation matrices since I plan on testing it using matrices created from quaternions.
I've created a little plane out of cylinders and the surface functions.
See code below:
clear all,clf
ax=axes('XLim',[-2 2],'YLim', [-2 10],'ZLim',[-1.5 1.5]);
grid on;
%axis equal;
xlabel('x');
ylabel('y');
zlabel('z');
ax
% rotate around
rot_mat = [.707 -.707 0;.707 .707 0; 0 0 1] %rotation matrix
[xc yc zc] = cylinder([0.1 0.0]); %cone
[x y z]= cylinder([0.2 0.2]);
h(1) = surface(xc,zc,-yc,'FaceColor', 'red'); %noise cone
h(2) = surface(z,y,0.5*x,'FaceColor', 'blue'); %right wing
h(3) = surface(-z,y,0.5*x,'FaceColor', 'yellow');%left wing
h(4) = surface(x,-1.5*z,0.5*y,'FaceColor', 'green'); %main body
h(5) = surface(xc,(1.5*yc)-1.3,z*.5,'FaceColor', 'red'); %tail
view(3);
x_temp = get(h(1),'xdata'); % get x values
y_temp = get(h(1),'ydata');
z_temp =get(h(1),'zdata');
xc_new=x_temp.*rot_mat;
%zc_new=
%yc_new=
I can get the x,y, and z value by using the commands
x_temp = get(h(1),'xdata');
y_temp = get(h(1),'ydata');
z_temp = get(h(1),'zdata');
The error I get is due to the matrix being nonconforment but how can I adjust the matrix correctly?
error: test_object_matrix_rot: product: nonconformant arguments (op1 is 2x21, op2 is 3x3).
The error is with the line xc_new=x_temp.*rot_mat;
PS: I'm using Octave 5.0.91 which is like Matlab
YOu are messing up a lot of things......in fact I would say, you have made your work complex. YOu should straight away work on matrices to rotate to new positons instead of arrays and picking them from the figure.
This line:
x_temp = get(h(1),'xdata'); % get x values
giving you a 2*21 array and your rot_mat is 3X3.....you cannot multiply them. YOu need to pick (x,y,z) and multiply this point with rotation matrix to get the point shifted. Check the below pseudo code.....yo can develop your logic with the below example code.
t = 0:0.1:1;
[X,Y,Z] = cylinder((t));
%% Rotation
th = pi/2 ;
Rx = [1 0 0 ; 0 cos(th) -sin(th) ; 0 sin(th) cos(th)] ;
P0 = [X(:) Y(:) Z(:)] ;
P1 = P0*Rx ;
X1 = reshape(P1(:,1),size(X)) ;
Y1 = reshape(P1(:,2),size(X)) ;
Z1 = reshape(P1(:,3),size(X)) ;
figure
hold on
surf(X,Y,Z)
surf(X1,Y1,Z1)
view(3)

Mesh Plot Problems in Matlab

I want to draw a meshc contour plot, but the contour is not being drawn as I want.
x = linspace(P(1),P(2)); %// x axis
y = linspace(P(3),P(4)); %// y axis
[X1 Y1] = meshgrid(x,y); %// all combinations of x, y
%[X1,Y1] = meshgrid(1:.125:3);
Z1 = mvnpdf([X1(:) Y1(:)],mu,sigma); %// compute Gaussian pdf
Z2 = reshape(Z1,size(X1)); %// put into same size as X, Y
meshc(X1,Y1,Z2);
%axis([1 3 1 3 -5 10]);
axis([P(1) P(2) P(3) P(4) -5 10])
The code above draws it this way:
But I want it to be this way:
How could I do this?
To get desired distance between the plots you can just subtract some number from the Z2 matrix. I cannot figure out why Matlab divides the plots in this way, but it works:
P = [1 3 1 3];
mu = [2 1.1];
sigma = [.09 .003; ...
.003 .002];
x = linspace(P(1),P(2)); %// x axis
y = linspace(P(3),P(4)); %// y axis
[X1 Y1] = meshgrid(x,y); %// all combinations of x, y
Z1 = mvnpdf([X1(:) Y1(:)],mu,sigma); %// compute Gaussian pdf
Z2 = reshape(Z1,size(X1)); %// put into same size as X1, Y1
Z2 = Z2 - 0.01;
meshc(X1,Y1,Z2);
axis([P(1) P(2) P(3) P(4) -5 10])
Maybe you need to play with other numbers to get a desired result. In my example I subtracted a small number, in order to avoid changing of the pdf-plot range.

Trying to understand the Matlab's patch command

I don't understand why the patch command fill with red the area above my function instead of the area below it; I mean the area between the graph of my function and the axis of abscissas.
x = linspace(0, 4);
f = x.^2;
plot(x, f)
patch(x, f, [1 0 0])
Because patch draws a closed polygon. And after your last point it goes back to the first on the shortest way. You need to add an additional points at the end and beginning with the last/first argument, but f = 0.
Like that:
x = linspace(0, 4);
f = x.^2;
plot(x, f)
%adjust data
x = [x(1) x x(end)];
f = [0 f 0];
patch(x, f, [1 0 0])
will give:
Or just use area as suggested by Luis Mendo:
area(x, f,'FaceColor',[1 0 0])

Matlab Plotting x=a?

How can I draw i.e. x = 5 line in plot at Matlab?
I plot like that:
x = (-10:.1:10);
f= 10;
plot(x, f, 'r');
of course it doesn't work. For every variable of x, y is equal to 0 except for x=10. When x = 10 y equals to everything. How to plot this?
In MATLAB, plot(X, Y) simply draws points on the graph (and connects them with lines). Note that in this form of syntax, X and Y must have the same dimensions. Therefore, to plot the line x = 5 create a vector of your desired y-coordinates, and then create matching x-coordinates, which are all equal to 5:
Y = -10:0.1:10;
X = 5 * ones(size(Y));
plot(X, Y);
A useful function from the FileExchange in hline and vline.
You could also achieve this by plotting only 2 points:
f = 5;
plot([-10 10] , [1 1]*f);
I think using line is more straightforward here than plot.
x = [-10, 10];
f = ones(size(x));
f = 5 .* f;
line(x, f);

Gurobi solver in matlab

I want to use Gurobi solver in Matlab, but I don't know how to calculate the required matrices (qrow and qcol).
For your reference I am copying the example provided in documentation.
0.5 x^2 - xy + y^2 - 2x - 6y
subject to
x + y <= 2
-x + 2y <= 2, 2x + y <= 3, x >= 0, y >= 0
c = [-2 -6]; % objective linear term
objtype = 1; % minimization
A = sparse([1 1; -1 2; 2 1]); % constraint coefficients
b = [2; 2; 3]; % constraint right-hand side
lb = []; % [ ] means 0 lower bound
ub = []; % [ ] means inf upper bound
contypes = '$<<
vtypes = [ ]; % [ ] means all variables are continuous
QP.qrow = int32([0 0 1]); % indices of x, x, y as in (0.5 x^2 - xy + y^2); use int64 if sizeof(int) is 8 for you system
QP.qcol = int32([0 1 1]); % indices of x, y, y as in (0.5 x^2 - xy + y^2); use int64 if sizeof(int) is 8 for you system
QP.qval = [0.5 -1 1]; % coefficients of (0.5 x^2 - xy + y^2)
Does it mean that if I have 4 decision variables than i should use 0,1,2,3 as indices for my decision variables x_1, x_2, x_3, x_4.?
Thanks
Note: I tried to use mathurl.com but I don't get how to write in proper format show that it will appear as latex text. Sorry for the notation.
This seems to be your reference. However your question seems to relate different example. You may need to show that one.
Anyway according Gurobi documentation:
The quadratic terms in the objective function should be specified by opts.QP.qrow, opts.QP.qcol, and opts.QP.qval, which correspond to the input arguments qrow, qcol, and qval of function GRBaddqpterms. They are all 1D arrays. The first two arguments, qrow and qcol, specify the row and column indices (starting from 0) of 2nd-order terms such as and . The third argument, qval, gives their coefficients.
So the answer is yes use indicies [0 1 2 3] for your decision variables x0, x1, x2, x3.