MATLAB Surface Plot with Vectors - matlab

How to make the surface plot of the three vectors with following dimentions in MATLAB using Surf function.
Dimension of vector x= 43x1
Dimension of vector y= 10x1
Dimension of vector z= 43x1
I have three vectors x, y, z with above dimensions. When I tried, it says dimension does not match. Please help.

Related

Dissimilar results to the surface plot in 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

Plot of numerical Integral of product of matrices in matlab

I would like to compute numerically an integral of a function that is a product of matrices like this ([1 1 1] is a row matrix with 3 entries, A(t) is a 3x3 matrix with each entry a function of t and P(t),S(t) is a column matrix with each entry a function of t)
I then tried to plot the integral of this function with
g=#(upper)integral(#(t)f(t),0,upper)
and
t=0:0.1:10;
plot(g(t),t)
but this doesnt seem to work. For example to let matlab accept t in A(t) and P(t) I had to have
syms t
but then it complains about this symbol in the integration.

Fittype with 2D data

I have a surface defined by a matrix nxn. The x axis of the surface is a 1xn vector, and the y axis is another 1xn vector. My goal is to make a function to gives me the bests x_approx vector and y_approx vector that, interpolating the matrix, gives the minimum error.
If n is a big number (like 1000, for a surface with a big resolution) and I want to find a 10x10 matrix that represent this surface as best as posible, I need to do something like that. At this moment (for a 10x10 fit), I have this code:
ft = fittype(#(x,y)customizedFitFunction([x,y],k0,k1,k2,k3,k4,k5,k6,k7,k8,k9,q0,q1,q2,q3,q4,q5,q6,q7,q8,q9),'numindep',2);
where x and y are the axis of the matrix, and k_i q_i are the best values for x and y axis, respectively. The data of the surface (z) is loaded into customizedFitFunction.
But when I execute this code, I have an error:
Error using fittype>iDeduceCoefficients (line 640)
This expression has no coefficients or non-scalar coefficients.
Any help?

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!

covariance of two vectors in matlab

I am trying to compute the covariance between two vectors in matlab:
x = [1:10]
y = [21:30]
cov(x,y)
This returns the matrix of covariance and variance. I just want 1 number: the covariance between the two vectors. How does one get this in matlab?
If you only have two one-dimensional vectors, the number you're looking for is the (1,2) element of the output of cov. By definition, the covariance matrix contains variances on its diagonal and covariance values on off-diagonal values.
I guess x in one realization of a random variable, so is y. Then cov(x',y') will give you the covariance matrix, where the diagonal entries are the variance of x and y, and the off diagonal element is the their covariance. Note cov(x',y') is symmetric matrix.