I've plotted cos(x), cos(2x) and cos(3x) on a graph using following:
x=linspace(0,4*pi,50); y=cos(x)
plot(x,y)
y2 = cos(2*x)
hold on, plot(x,y2)
y3 = cos(3*x)
hold on, plot(x,y3)
grid on
xlabel (‘x’), ylabel(‘y’)
legend (‘y=cos(x)’, ‘y=cos(2x)’,’y=cos(3x)’)
How can I do it a different way using a matrices? If I create a 3 columned matrix representing cos(x), cos(2x) and cos(3x) by using: Y=[sin(x) sin(2*x) sin(3*x)]. What would I do after this? I typed in plot(x,Y) but says Error using plot. Vectors must be the same length. Perhaps an obvious thing but only recently started using MATLAB. Thanks in advance.
You're concatenating horizontally using the spaces (i.e., creating one long vector that is three times the length of x). If you concatenate vertically using ; to create a 3x50 matrix, everything will be fine:
x = linspace(0,4*pi,50);
Y = [cos(x);cos(2*x);cos(3*x)];
plot(x,Y);
Related
I am trying to do a figure similar to the one attached.
I have exactly a (224x1) vector with dates (x-axis), a (10x1) vector with maturities (y-axis) and a (224x10) matrix with the values (z-axis).
I tried surf(X, Y, Z) but I got an error ("data dimensions must agree").
How can I combine this to make a plot like the one attached?
Thanks, V!
Edit: The second plot is the one I am getting using Luis Mendo's suggestion:
Use
surf(Y,X,Z)
From the documentation (emphasis added):
surf(x,y,Z) and surf(x,y,Z,C), with two vector arguments replacing
the first two matrix arguments, must have length(x) = nand
length(y) = m where [m,n] = size(Z). In this case, the vertices
of the surface patches are the triples (x(j), y(i), Z(i,j)).
Note that x corresponds to the columns of Z and y corresponds to
the rows.
Do
[X,Y]=meshgrid(x,y);
surf(X,Y,Z);
You need to create a meshgrid to be able to plot a surf. X ,Y and Z need to be the same size!
I have a 25000x3-matrix, with each row containing a x-, a y- and a z-value. Now I wanted to do a graphical plot out of these. But for using for example surf(Z) I have to use a mxn-matrix as Z with m equal the size of x and n equal the size of y. How can I reshape the matrix I have to the needed mxn-matrix? The problem is that my x- and y-values are no ints, but floats, so I assume that I have to do a interpolation first. Is that true? My data plotted with plot3 looks like:
The fact that your x- and y- values are not integers is not a problem at all. The real question is: are your (x,y) points forming a grid, or not ?
If your points are forming a grid, then you have to reshape your columns to form m-by-n arrays. You may need to sort your data according to the first, then second column and then use the reshape function.
If your points are not forming a grid, then you will have to make an interpolation. By chance the scatterinterpolant class can nicely help you in doing so.
As you can see, the data you are providing is neither given in a gridded way, nor is the point cloud clean. You could however try to do the following:
Project the point cloud onto the x-y plane
Triangulate those points
Give the points their original z-coordinate back.
Plot the surface using trisurf
Here is a MATLAB code that does this:
%// Generate some points P
[X,Y] = ndgrid(0:30);
P = [X(:), Y(:), X(:).^2+Y(:)];
%%// Here the actual computation starts
[~,I] = unique(P(:,1:2),'rows'); %// Remove points with duplicate (x,y)-coords
P = P(I,:);
T = delaunay(P(:,1),P(:,2)); %// Triangulate the 2D-projection
surface = triangulation(T, P(:,1), P(:,2), P(:,3)); %// Project back to 3D
trisurf(surface); %// Plot
You may want to remove stray points first, though.
I have a vector with 1000 random numbers called v. I also have a vector, called x that represents the domain of which the numbers in v are generated, and another vector y that has the numbers of the cdf of the values in v. I know that I can do plot(x,y); and get a smooth function of the (non-empirical) cdf, and I also know that I can do cdfplot(v) to get a function of the empirical cdf.
My question is: How can I get these plots on the same set of axis?
Thank you for your help.
You could either generate data for an empirical cdf plot using ecdf or plot it directly with cdfplot like you mentioned. I would recommend using cdfplot since it sets up a few more things, such as a grid:
hFig = figure;
cdfplot(v);
hold all;
plot(x, y);
And as a bonus! Consider showing the X axis in logarithmic units, whichever reveals the data the best for you:
hAxes = get(hFig, 'CurrentAxes');
set(hAxes, 'XScale', 'log')
I need to be able to do a surface plot using data from 3 vectors. I found similar information, but no method seems to work with my data. My X and Y columns are evenly spaced, but not in increasing order. I tried different methods, but none of them seem to give me what I want, which is a simple surface linking close points together. I tried the following:
[X Y]=meshgrid(x,y);
Z=griddata(x,y,z, X,Y);
surf(X,Y,Z);
This is not exactly what I want, because it creates a surface at z=0 and makes it look more like a volume plot than just a surface. It also runs very slowly on my computer (probably from creating all the gridpoints). If I could get something that doesn't require as much memory it would be ideal (my vectors have about 20k values each), but this is not a necessity.
***Edit: I also tried using the scatteredInterpolant method found here,but the function doesn't seem to be recognized by MATLAB and I get this error:
Undefined function 'scatteredInterpolant' for input arguments of type 'double'.
Also here is an image of my problem:
You can see that we can't see under the surface, there is some z=0 plane blocking it.
If you have anything for me, any help is appreciated.
Thanks in advance.
**Edit 2: I added sample vectors, they're my x,y and z values from left to right.
***Edit 3: Here's an image of the triangulation I get. As you can see some points are being ignored for some reason, which gives those long and weird looking blue triangles.
Mike
As conventional methods seem to fail, I would suggest you to do it manually.
Create a Z matrix full of NaN values. The size of the matrix should be dependant on your x and y values.
Loop over all occuring x,y, pairs and put their (average?) z value in the right position of your Z matrix.
Loop over all NaN values and interpolate their value. Perhaps using filter2.
Use surf to plot the resulting surface
If you have points which are described by vectors, and you want to plot them you could always use a Delauny triangulation. The function in matlab is called Tri=delauny(X,Y,Z). The data generated by this function can be shown with either trimesh(Tri,X,Y,Z) or trisurf(Tri,X,Y,Z). Keep in mind trisurf is only for 3D data. If you want to adjust the transparancy of plots in your graph use the alpha setting.
I hope this helps
To me it looks like you just need to sort your data before plotting.
Here is an example which I believe is similar to your case (since I could not download your data).
x = [2 1 4 3 -1 -3 -4 -2];
y = [1 2 3 4 -1 -2 -3 -4];
z = 32 - x.*x - y.*y;
[X1 Y1] = meshgrid(x,y);
Z1 = 32 - X1.*X1 -Y1.*Y1;
surf(X1,Y1,Z1)
aux = sort([x;y],2);
x = aux(1,:);
y = aux(2,:);
[X2 Y2] = meshgrid(x,y);
Z2 = 32 - X.*X - Y.*Y;
figure()
surf(X2,Y2,Z2)
The first figure results in a very problematic surface:
The second figure contains the desired surface:
I have the following matlab code for approximating a differential equation via the Euler-method:
% Eulermethod
a=0;
b=0.6;
Steps=6;
dt=(b-a)/Steps;
x=zeros(Steps+1,1);
x(1,1)=1;
y=zeros(Steps+1,1);
for i=1:Steps
x(i+1,1)=x(i,1)+dt*(x(i,1)*x(i,1)+1);
end
plot(x)
I want to be able to plot the solution plot for several different values of Steps in one plot and have the x-axis go from 0 to 0.6 instead of from for example 1 to 100 000 etc. Can this be done?
If you use the hold on command this will allow you achieve multiple plots on the same figure. Similarly, if you separate your data into x and y vectors, you can plot them against eachother by passing 2 vectors to plot instead of just one. For example
figure
hold on
for i=1:m
x = [];
y = [];
%% code to populate your vectors
plot(x,y)
end
You should now see all your plots simultanesously on the same figure. If you want x to be composed of n equally spaced elements between 0 and 0.6, you could use the linspace command:
x = linspace(0.0,0.6,n);
In order to distinguish your plots, you can pass an extra paramter to the function .For example
plot(x,y,'r+')
will plot the data as a series of red + symbols.
Plot can take more arguments: plot(x_axis,values, modifiers);
If x-axis is a vector of M elements, values can be a matrix of MxN elements, each of which are drawn with a separate color.