p = [3,3]
plot(p, 'x')
This weirdly generates this:
I'd like it to be a point at x=3/y=3 on the plot. How?
#mathematician1975 is right, but I feel like this requires a bit more explanation:
Like the official documentation states:
plot(Y) plots the columns of Y versus the index of each value when Y is a real number.
so in fact this is not weird at all that plot(p, 'x') plots each value in p against its index, i.e. the points (1, 3) and (2, 3).
This is actually handy in some cases (when you want the x-coordiantes to be a running index), but not in yours. To plot point p correctly, use the syntax plot(X, Y), that is:
plot(p(2), p(1), 'x')
(here I assumed that the y-coordinate is the first in p, but if it's the x-coordinates you can just swap the places of the input arguments).
In the general case, if p is a matrix with two columns (say, the first contains all y-coordinates and the second all x-coordinates), you can plot all points like so:
plot(p(:, 2), p(:, 1), 'x')
You need vectors of each coordinate. For example:
x = [3,4]
y = [5,6]
plot(x,y,'x')
will plot the points (3,5) and (4,6)
Related
I am having some trouble understanding contours.
What I have understood so far is that contours are a way to represent a 3d figure in a 2d plane. It does so by plotting a function of 2 variables as curves along which the function has same value.
Now if I do:
z=[1 4; 10 7];
contour(z);
I get this:
I read the documentation and it says:
contour(Z) draws a contour plot of matrix Z, where Z is interpreted as
heights with respect to the x-y plane. Z must be at least a 2-by-2
matrix that contains at least two different values. The x values
correspond to the column indices of Z and the y values correspond to
the row indices of Z. The contour levels are chosen automatically.
Thus for x=1,y=1: z=1, x=2,y=1: z=4 and so on. However I can't understand how to interpret this as the contour plot shown above.
And if I write:
contour(X1, X2, vals, [0.5 0.5], 'b'); where X1, X2 and vals are equal sized matrices and vals is a matrix of only 0s and 1s. I can't understand what does the argument [0.5 0.5] do. I read the documentation which states:
contour(Z,v) draws a contour plot of matrix Z with contour lines at
the data values specified in the monotonically increasing vector v. To
display a single contour line at a particular value, define v as a
two-element vector with both elements equal to the desired contour
level.
and I am unable to understand this statement.
The problem of the first contour is that there are just 4 values. Try something like
x = 0:0.1:10;
y = 0:0.1:10;
z = sin(x') * cos(y);
contour(z)
For the second thing, this means that if you want to see just particular contours, input them as vector v. In the example above:
contour(z, [0.1, 0.2, 0.3])
will show contour lines of 0.1, 0.2 and 0.3.
To have a single contour line, you can't have just (z, 0) but require (z, [0,0])
I have two equations:
ellipseOne = '((x-1)^2)/6^2 + y^2/3^2 = 1';
and
ellipseTwo = '((x+2)^2)/2^2 + ((y-5)^2)/4^2 = 1';
and I plotted them:
ezplot(ellipseOne, [-10, 10, -10, 10])
hold on
ezplot(ellipseTwo, [-10, 10, -10, 10])
title('Ellipses')
hold off
Now I'm trying to find the intersection of the two ellipses. I tried:
intersection = solve(ellipseOne, ellipseTwo)
intersection.x
intersection.y
to find the points where they intersect, but MATLAB is giving me a matrix and an equation as an answer which I don't understand. Could anyone point me in the right direction to get the coordinates of intersection?
The solution is in symbolic form. The last step you need to take is to transform it into numeric. Simply convert the result using double. However, because this is a pair of quadratic equations, there are 4 possible solutions due to the sign ambiguity (i.e. +/-) and can possibly give imaginary roots. Therefore, isolate out the real solutions and what is left should be your answer.
Therefore:
% Your code
ellipseOne = '((x-1)^2)/6^2 + y^2/3^2 = 1';
ellipseTwo = '((x+2)^2)/2^2 + ((y-5)^2)/4^2 = 1';
intersection = solve(ellipseOne, ellipseTwo);
% Find the points of intersection
X = double(intersection.x);
Y = double(intersection.y);
mask = ~any(imag(X), 2) | ~any(imag(Y), 2);
X = X(mask); Y = Y(mask);
The first three lines of code are what you did. The next four lines extract out the roots in numeric form, then I create a logical mask that isolates out only the points that are real. This looks at the imaginary portion of both the X and Y coordinate of all of the roots and if there is such a component for any of the roots, we want to eliminate those. We finally remove the roots that are imaginary and we should be left with two real roots.
We thus get for our intersection points:
>> disp([X Y])
-3.3574 2.0623
-0.2886 2.9300
The first column is the X coordinate and the second column is the Y coordinate. This also seems to agree with the plot. I'll take your ellipse plotting code and also insert the intersection points as blue dots using the above solution:
ezplot(ellipseOne, [-10, 10, -10, 10])
hold on
ezplot(ellipseTwo, [-10, 10, -10, 10])
% New - place intersection points
plot(X, Y, 'b.');
title('Ellipses');
hold off;
I have an issue where I have my data formatted such that I have n data points in the Z axis, and every Z data point has m corresponding x & y points. I essentially want to have n 2D m-point plots, but merged into one surface.
My data takes the following form:
z = [0 1 2 3]
data(:, :, 1) = [0 1 2; 2 2 2] (the first row corresponds to the x points, second to the y; the page (the 1) corresponds to the respective z point at which I am plotting said x and y points)
So essentially, at z(i), I would like to have data(:, :, i) plotted. Then finally all merged together into one surface. How would I accomplish this?
An example that might help imagine the situation is if data(:, :, i) for all i was uniform, like the above sample, then a cube surface (of volume 12) would be drawn.
So essentially, at z(i), I would like to have data(:, :, i) plotted. Then finally all merged together into one surface. How would I accomplish this?
Use scatter3 to plot 3-D scatter plot, for every points with a set of x,y,z value.
Alternatively, use plot3 to make 3-D line plot. Be careful with the argument formats.
You can use code like
for ii = 1:length(z)
x = data(1, :, ii);
y = data(2, :, ii);
scatter3(x, y, z*ones(1, length(x));
end
Finally, rotate your angle of view using view([0,0,-1]) to the direction facing the z axis. By this, all layers of different z values are squeezed into one layer.
If you really mean you want to ignore the z-values, but only want to see an overlayed plot of all the points,
for ii = 1:length(z)
x = data(1, :, ii);
y = data(2, :, ii);
scatter(x, y);
end
An example that might help imagine the situation is if data(:, :, i) for all i was uniform, like the above sample, then a cube surface (of volume 12) would be drawn.
As #rayryeng commented, I found this difficult to imagine. What do you mean by "a cube surface of volume 12"? This part looks different than descriptions above it.
I am charting the following data:
a=[...
0.1, 0.7, 0.00284643369242828;...
0.1, 0.71, 0.00284643369242828;...]
such that column 1 never surpasses approximately 10
also such that column 2 goes from .7 to 1.
Column 3 seems ok
When i chart my surface using surf(a) it looks like this:
it appears not to be properly considering what should be x and y.
anything seem weird there?
I think you need to try one of two things: either break out your height column into its own rectangular matrix Z and use surf(Z) to plot each point relative to its location in the matrix (so your x- and y-axes will not be scaled the way you want), or you can put your desired x- and y-coordinates in their own vectors, and plot the matrix Z (defined at every point (xi, yj) for all i in N and j in M where x is N elements long and y is M elements long) with surf(x,y,Z).
x = 0.1:0.1:10; % or whatever increment you need
y = 0.7:0.01:1; % or whatever increment you need
Z = zeros(length(x),length(y); % initialized to the correct size, fill with data
I think you are going to have to regenerate your Z-data so that it is in a rectangular matrix that is (elements in x) by (elements in y) in dimension.
EDIT: You do not need to recreate your data. If you know that you have n unique elements in x and m unique elements in y, then you can use:
X = reshape(data(:,1),m,n);
Y = reshape(data(:,2),m,n);
Z = reshape(data(:,3),m,n);
surf(X,Y,Z);
And that should give you what you are looking for.
I am trying to fit a B-spline to a set of ordered discrete data points which represent pixels of a contour extracted from an image.
While the below code works fine for some simple shapes, but not for others (please see attached image for examples). Why does this happen, and what would be a better way to approach this problem?
I am quite new to differential geometry, appreciate any insights or inputs. Thanks.
% data contains two columns representing x,y coordinates of pixels
x = data(:, 1);
y = data(:, 2);
plot(x, y, 'bo');
fittedmodel = fit(x, y, 'cubicinterp');
plot(fittedmodel, 'r-');
What went wrong?
You have two sets of numbers x and y with the same number of elements in both sets.
You assume:
a. That there is a function f such that f( x_i ) = y_i for all pairs x_i,y_i in your sets.
b. That the points in the set are ordered: that is, if you follow the curve of f(x) then x_i "comes before" x_{i+1}.
While these assumptions hold for the "correct fit" example you have. They are no longer valid for the "incorrect fit" example.
As you can see for yourself, the input contour on the top cannot be expressed as y = f(x) since there are values of x for which there are two possible corresponding values of y (see the definition of mathematical function). The fit you got is the closest thing to a mathematical function y = f(x) that can be given the pairs x,y you gave (the red curve has the property of each x having only one y value).
What can you do?
In most cases when you try and fit a 2D curve you search for a parametric curve: that is, you introduce an auxilary parameter t such that each point along the curve can be represented as [x(t), y(t)] for some 0<=t<=1.
Now, if assumption b holds (and by looking at your examples, I'm not certain it is), what you can do is
t = linspace( 0, 1, numel(x) ); % define the parameter t
fitX = fit( t, x, 'cubicinterp'); % fit x as a function of parameter t
fitY = fit( t, y, 'cubicinterp'); % fit y as a function of parameter t
plot( fitX, fitY, 'r-' ); % plot the parametric curve