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])
Related
I would like to make a scattered stars and plot multiple spheres on top of it? I tried doing this:
x = rand(100,1); y = rand(100,1); z = rand(100,1);
scatter(x,y,z,'c*')
After this I tried plotting sphere but the sphere pushes the stars to the side. How do I fix this?
vec = [1;1;1]; rads = 1;
[x y z] = sphere;
x = rads*x+vec(1); y = rads*y+vec(2);
z = rads*z+vec(3);
surf(x, y, z, 'Edgecolor', 'none')
colormap colorcube
As you can see it pushed the stars to the side
Thank you.
Its pushing the scatter plot to the side because the range of the 'stars' is [0 1] because the command rand(...) generates values between 0 and 1. On the other hand, the sphere goes from 0 to 2 in all 3 directions.
To fix the problem, you can simply multiply the data used to generate the scatter plot by 2, so they will be in the range [0 2].
Doing so results in the following:
And the code. Note that I used scatter3 instead of scatter.
clear
clc
close all
xs = 2*rand(100,1); ys = 2*rand(100,1); zs = 2*rand(100,1);
hScatter = scatter3(xs,ys,zs,'c*')
hold on
vec = [1;1;1]; rads = 1;
[x y z] = sphere;
x = rads*x+vec(1); y = rads*y+vec(2);
z = rads*z+vec(3);
surf(x, y, z, 'Edgecolor', 'none')
colormap colorcube
rotate3d on
I was following the following example for matlab: http://se.mathworks.com/help/matlab/creating_plots/contour-plot-in-polar-coordinates.html. This example works, but when I want to plot my own data, the figure does not change, and I get no output. Plotting my data as usual with contour() works. What am I doing wrong?
Edit: The code from the example is:
th = (0:5:360)*pi/180;
r = 0:.05:1;
[TH,R] = meshgrid(th,r);
[X,Y] = pol2cart(TH,R);
Z = X + 1i*Y;
f = (Z.^4-1).^(1/4);
figure
surf(X,Y,abs(f))
colormap summer;
hold on
surf(X,Y,zeros(size(X)))
hold off
xlabel('Real')
ylabel('Imaginary')
zlabel('abs(f)');
figure
contour(X,Y,abs(f),30)
axis equal
xlabel('Real')
ylabel('Imaginary')
h = polar([0 2*pi], [0 1]);
delete(h)
hold on
contour(X,Y,abs(f),30)
My values are a, b and c, representing a meshgrid (as TH and R do) and c which is a height matrix. I simply replaced the last command:
contour(a, b, c, 30)
a is a 361x361-sized matrix with range from 2 to 226, b the same. c goes from 0.5 to 50 and is 361x361-sized.
Ok, the solution of the problem was simply to rewrite this line
h = polar([0 2*pi], [0 1]);
to
h = polar([0 2*pi], [0 max(a)]);
because my data started at a x-value of 2, and was therefore not displayed in the range from 0 to 1.
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.
Ok, right now i'm trying to create a connect four game through Matlab coding; now the game is still infant but my problem is that I either can't get the figure to plot in each grid square or I can't get the 'circle' figure to plot at all. Please help in any way possible. Also if anyone knows about any connect four matlab tutorials, it would be greatly appreciated.
function [] = Kinect4(nrRows, nrCols)
board = zeros(nrRows, nrCols);
nrMoves = 0;
set(gca, 'xlim', [0 nrCols]);
set(gca, 'ylim', [0 nrRows]);
for r = 1 : 1 : nrRows - 1
line([0, nrCols], [r, r], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
for c = 1 : 1 : nrCols - 1
line([c, c], [0, nrRows], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
DrawBoard(nrRows, nrCols)
hold on;
while nrMoves < nrRows * nrCols %Computes ability to move polygon
[x, y] = ginput(1);
r = ceil(y); % convert to row index
c = ceil(x); % convert to column index
angles = 0 : 1 : 360;
x = cx + r .* cosd(angles);
y = cy + r .* sind(angles);
plot(x, y, 'Color', [1 1 1], 'LineWidth', 3);
axis square;
end
end
Here are some fixes to the code.
removed the line DrawBoard(nrRows, nrCols). Not sure if you put it there as a comment as you have already drawn the board or if DrawBoard is a separate function.
Changed the calculation for r and c to give the center of the cell you wan the put the peg in. This is done by subtracting 0.5 from each.
Changed the line x = cx + r .* cosd(angles); to x = c + 0.5*cosd(angles);. In the previous one, variable cx is undefined and instead of r being the radius of the peg, I used 0.5 you can replace it by appropriate variable. But the idea is to draw a circle of radius 0.5 (so that it fits in a cell) with the center offset by c along x-axis. Similar change for y to offset the peg along y-axis.
Changed the color in plot command to [0 0 0], which is black. [1 1 1] is white and is impossible to see on white background :). I would suggest using 'k' for black, 'b' for blue and so on. See matlab documentation for basic color specifications.
I am guessing you are yet to implement gravity so that the peg moves down. Also you need to check is a cell is already filled. All these and other improvements (like removing unnecessary for-loops, better way to draw pegs, etc.) are left once you get to a working code.
Here's a "working" code:
function [] = Kinect4(nrRows, nrCols)
board = zeros(nrRows, nrCols);
nrMoves = 0;
set(gca, 'xlim', [0 nrCols]);
set(gca, 'ylim', [0 nrRows]);
for r = 1 : 1 : nrRows - 1
line([0, nrCols], [r, r], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
for c = 1 : 1 : nrCols - 1
line([c, c], [0, nrRows], ...
'LineWidth', 4, 'Color', [0 0 1]);
end
axis square;
hold on;
while nrMoves < nrRows * nrCols %Computes ability to move polygon
[x, y] = ginput(1);
r = ceil(y) - 0.5;
c = ceil(x) - 0.5;
angles = 0 : 1 : 360;
x = c + 0.5*cosd(angles);
y = r + 0.5*sind(angles);
plot(x, y, 'Color', [0 0 0], 'LineWidth', 3);
end
end
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);