Dissimilar results to the surface plot in MATLAB - matlab

>> x=0:0.001:720;
>> y=sind(x);
>> z=cosd(x);
>> surf(x,y,z);
I want to plot a surface using the above code i.e. x in X-axis, y is Y-axis, and z in Z-axis.
I also programmed a FORTRAN code for the following purpose, created a csv file, and plotted it in origin. I am getting a result as this:
However, in MATLAB, I am getting a similar idea when using:-
>> plot3(x,y,z)
as in this image:
but it's not a surface (for obvious reasons).
When using the surf command, I am also getting an error saying:
Z must be a matrix, not a scalar or vector.
What could be the possible problem with my code?

Using surf requires Z to be a matrix. This is fixed easily with functions like meshgrid (also useful is griddata).
Using meshgrid makes using surf very accessible.
But both Z and Y are just functions of X so I'm unable to explain why your plot Z-value would change with both X and Y. From the analytical (mathematical) equations you've listed, the Z-value should be constant in the Y-dimension.
stepsize = 1; % use 10 for cleaner look
x = 0:stepsize:720;
y = sind(x);
[X,Y] = meshgrid(x,y);
Z = cosd(X);
surf(X,Y,Z)
Notice that the contour lines are straight & parallel in the Y dimension (using surfc(X,Y,Z)).
Another method is to loop through elements of x (indexed by i) and y (indexed by j) where both x and y (vectors) to calculate Z(i,j) where Z is a matrix. Due to default assignment for rows & columns, this method requires transposing the Z matrix such as surf(X,Y,Z.').
Related Posts:
How can I plot a function with two variables in octave or matlab?
MATLAB plot part of surface

Related

putting function values in meshgrid format in matlab

I have coordinate vectors X and Y and corresponding vector of function values Z. I need to plot a surface Z(X,Y), and I need to put Z values on a regular mesh, right now X and Y define points distributed irregularly. What is the best way to do it in matlab?
Thanks!
If you have a function such as z=f(x,y) then you could use fsurf().
fsurf(#(x,y) x^2+y^2);
Or else, if you have data points then you could use surf()
[x, y]=meshgrid(-5:0.1:5);
z = x.^2 + y.^2;
mesh(x,y,z);
You could specify the axis as per your need.
The following worked for me:
Zinter = scatteredInterpolant(Xs,Ys,Z) %defines interpolant for scattered data and allows to calculate values of z for any point

3d plot with ksdensity in matlab

I have a problem in matlab.
I used a ksdensity function on a vector of deltaX, which was my computed X minus actual X.
And I did the same on deltaY.
Then I used plot on that data. This gave me two 2d plots.
As I have two plots showing how (in)accurate was my system in computing X and Y (something like gaussian bell it was). Now I would like to have one plot but in 3d.
The code was just like that:
[f,xi] = ksdensity(deltaX);
figure;
plot(xi,f)
Ok what I'm about to show is probably not the correct way to visualize your problem, mostly because I'm not quite sure I understand what you're up to. But this will show you an example of how to make the Z matrix as discussed in the comments to your question.
Here's the code:
x = wgn(1000,1,5);%create x and y variables, just noise
y = wgn(1000,1,10);
[f,xi] = ksdensity(x);%compute the ksdensity (no idea if this makes real-world sense)
[f2,xi2] = ksdensity(y);
%create the Z matrix by adding together the densities at each x,y pair
%I doubt this makes real-world sense
for z=1:length(xi)
for zz = 1:length(xi2)
Z(z,zz) = f(z)+f2(zz);
end
end
figure(1)
mesh(xi,xi2,Z)
Here's the result:
I leave it up to you to determine the correct way to visualize your density functions in 3D, this is just how you could make the Z matrix. In short, the Z matrix contains the plot elevation at each x,y coordinate. Hope this helps a little.

Three dimensional plot on matlab

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!

Plot x=y plane in MATLAB

I fail to think how should I proceed for plotting x=y plane withing a 2x2x2 space.
I create a 2x2 meshgrid
[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
then I come to define Z - I get stuck
My thoughts go like this
I need to create a 21x21 array for Z ( I use surf function.. and the dimensions of X, Y, Z must match. Right?).
I need to populate only those values of Z that follow x==y
Now for each such point i.e. x==y Z will vary -1:0.1:1 .. Does This require that I iterate again and again on the x==y and keep drawing Z with values from -1:0.1:1?
Am I going the right way about plotting this plane? Kindly help.
You simply need to define X and Z, Y is equal to X by definition:
[X Z] = meshgrid(-1:.1:1,-1:.1:1);
figure;
surf(X,X,Z);xlabel('x');ylabel('y');zlabel('z');
Results with
You are actually trying to do something two dimensional in a 3 dimensional setting.
A bit unintuitive, but that does not mean it can't be done, for example:
[X,Y]=meshgrid(-1:0.1:1,-1:0.1:1);
Z = zeros(size(X)); % Perhaps you want NaN rather than zeros
idx = X==Y;
Z(idx)=X(idx).^2+Y(idx) % Of course identical to X(idx).^2+X(idx)
surf(Z)

Generate Contour Given X, Y and Z vectors

Given 3 vector-pair, X, Y and Z, how to generate the contour? I understand that we need to make use of the contour plot. But the thing is that we need to pass in a 2x2 matrix for this argument, which presumably, is a matrix of Z corresponding to each X,Y pair. But this would mean that I have to go extra miles to create such a matrix by using griddata interpolation first before talking about contour generation.
Is there any other more succinct method?
Yes. Use the Tricontour tool. It is found on the file exchange (on Matlab Central.) This does the contouring as you desire directly, without forcing you to use meshgrid and griddata.
MATLAB addresses this need of yours fairly succinctly.
What you need to do is use meshgrid to two-dimensionalize your X and Y vectors. Here is a simple example to demonstrate how to generate a contour plot of z = sin (x^2 + x*y^2):
x = -10:0.1:10;
y = -10:0.1:10;
[x,y] = meshgrid(x,y);
z = sin(x.^2+x.*y.^2);
contour(x,y,z)
Note the use of the .^ and .* notations, which forces MATLAB to conduct an element-by-element evaluation of the z matrix, making it 2D in the process.