Matlab Plotting x=a? - matlab

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

Related

Vertical lines with text in Matlab plot

I have created a plot in Matlab. Let's assume for simplicity that I have the following plot:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
Now I would like to add vertical lines (going from the bottom of the figure to the top) at positions x = 1, x = 3 and x = 5. Additionally, the vertical lines should have text (next to the line or on top of the line). For example, for the line at x = 1 I would like to have the text "test 1".
How can this be done? This seems to be a pretty tricky thing in Matlab.
Here are some ways to draw lines:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y,[1 1],[-1 1],[3 3],[-1 1],[5 5],[-1 1]);
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y); hold on;
for ind1 = 1:2:5
line([ind1 ind1],[min(y) max(y)],'Color',[0 0 0]);
end
x = 0:pi/100:2*pi;
y = sin(x);
A = zeros(6); A(sub2ind(size(A),1:6,[2 1 4 3 6 5])) = 1;
plot(x,y); hold on; gplot(A, [repelem(1:2:5,1,2).', reshape(repelem([1 -1],3,1).',[],1)]);
Etc.
Either use hold on and plot in several commands, or provide all inputs to your plot function right aways to get the desired result. Consult the documentation of the above functions for more information.
For texts refer to text.
for i=1:2:5
hold on
plot([i i],[0 1])
s=sprintf('test%1.0f', i)
t=text(i,1,s)
set(t,'Rotation',90)
end

Efficiently Plot Markers of varying individual size

The plot function allows us to plot all markers with a constant size s.
figure;
x = -10 : 10;
y = x .^ 2;
s = 10;
plot(x, y, 'bo', 'MarkerSize', s);
Suppose instead we would like each marker to have some individual size sx. For example, sx = abs(x) + 1.
One way to achieve this would be to use a for loop.
figure;
x = -10 : 10;
y = x .^ 2;
sx = abs(x) + 1;
hold on;
for i = 1 : length(x)
plot(x(i), y(i), 'bo', 'MarkerSize', sx(i));
end
This works well enough for small number of x. However, what if x is larger? For example, x = -100 : 0.01 : 100.
This now takes significantly longer, while plot(x, y, 'bo', 'MarkerSize', 100) would still complete almost instantly. Ideally, we would be able to do something such as plot(x, y, 'bo', 'MarkerSize', sx) where sx is a vector of sizes with each entry in sx corresponding to an entry in x and y. Unfortunately, that would give an error of Value not a numeric scalar.
Is there an efficient way to plot markers where each marker have varying individual sizes?
What you're trying to do is possible using scatter instead of plot as follows:
scatter(x,y,abs(x)+1)

Example for plotting contour on polar grid is not working in matlab

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.

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

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