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?
Related
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.
>> 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
I am trying to create four 11 by 11 by 11(since we start from 0) uniform arrays called X (for X domain), Y, Z and W. The space domains (X,Y,Z) should be from 0 to 100 (assuming cm), so the uniform mesh is 10cm of length (X), depth (Y) and height (Z). I also want to generate a function like a Gaussian exp (-x^2-y^2-z^2)) distributed in X,Y,Z domains and stored in W. I am stuck on how do I generate the 4 arrays I need as well as how to generate the uniform mesh. I have tried to use meshgrid but I cant get the correct surface plots. Any help is appreciated
You can use meshgrid to generate your independent variables X, Y, and Z like so:
[X, Y, Z] = meshgrid(0:10:100);
Now, you can easily compute a dependent variable W using the above matrices and element-wise array operations. For your example formula:
W = exp(X.^2+Y.^2+Z.^2);
Note that the .^ operator is used to raise each element of the matrix to a power.
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 two splines that I've generated with (a bit of modification of) getcurve() which represent an XY and XZ of a trajectory. I would like to combine the two splines together to generate a 3d plot, via plot3(). I modified getcurve() to output the XData and YData of the line drawn on the plot.
Since the splines are graphically generated, the number of elements won't necessarily be the same between XY and XZ. Suppose length(XY)>length(XZ). I'm trying to create a new vector XZ_2 that has the same length as XY and has the same x-values as XY.
My first idea was to interpolate as follows:
XZ_2(:,2) = interp1(XZ(:,1), XZ(:,2), XY(:,1))
but I get an error:
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 191)
F = griddedInterpolant(X,V,method);
The spline XZ looks like this:
I don't understand why I can't interpolate given this spline. It doesn't look like anything special. You can rebuild this spline yourself by doing the following (you'll need the Curve Fitting Toolbox):
>> xz = [0.0288 0.0518 0.1071 0.1763 0.2707 0.3583 0.4988 0.5864 0.7339 0.8191 0.9182 0.9781
1.8070 1.3626 0.9766 0.4152 -0.0643 -0.3684 -0.9181 -1.1637 -1.4795 -1.6667 -1.8070 -1.9474];
>> fnplt(cscvn(xz));
Is there a way to "resize" XZ to use the same x-values as XY? I realize that some information about XZ will be lost when I do this, but that's okay.
it says that in interp1(x, y, xi);, the x and xi must be monotonic increasing, i.e. sorted.