Generate Equation from 3D surf Plot in Matlab - matlab

I am wondering if anyone knows (or is it possible?) how to generate a trend equation from a 3D surf plot from Matlab? I understand that we can create trendline for 2D plots (linear and nonlinear) and show its equation, but how about 3D plot? Can we create something like:
z = ax + by?
Regards
Kit

If you have the curve fitting toolbox you can fit 3D surfaces using cftool as described here.
Here's an example:
[X,Y] = meshgrid(1:100,1:100);
X = reshape(X,numel(X),1);
Y = reshape(Y,numel(Y),1);
Z = 3*X+4*Y;
plot3(X,Y,Z)
f = fit([X, Y], Z, 'poly11');
coeffvalues(f)

Related

Creating meshgrid of scattered Cartesian data for plotting on a sphere

I have a set of n=8000 cartesian coordinates X,Y and Z as vectors and also a vector V of same size which I want to use as values to create a heatmap on a sphere.
I saw this link (visualization of scattered data over a sphere surface MATLAB), but I don't understand how I convert this set of data into a meshgrid for plotting using surf.
Almost every example I saw uses meshgrids.
Right now, I am doing by plotting a sphere and then use scatter3 to plot my points as big balls and try to smooth them later. I looks like this:
I would like to get the figure as the plotting of the example in that link, where he uses:
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
surf(x,y,z,c);
colormap([1 1 0; 0 1 1])
axis equal
EDIT:
(Sorry for taking so long to reply, the corona crises kept away from work)
What I am actually doing is:
for i=1:numel(pop0n)
ori(i,:)=ori(i,:)/norm(ori(i,:));
end
x = ori(:,1);
y = ori(:,2);
z = ori(:,3);
%// plot
m=100;
[aa,bb,cc] = sphere(m);
surf(aa,bb,cc,ones(m+1,m+1)*min(pop0n))
hold on
colormap jet;
scatter3(x,y,z,400,pop0n/norm(pop0n),'filled');
colorbar
shading interp
The array 'ori' is 8000x3, and contains the x, y and z coordinates of the points I want to plot and pop0n is a 8000 sized vector with the intensities of each coordinate.
My main question is how do I transform my x, y, z and pop0n, that are vectors, into 2D arrays (meshgrid) to use surf?
Because I cannot simply do surf(x,y,z,pop0n) if they are vectors.
Thanks in advance
As David suggested, griddata does the job.
What I did was:
for i=1:numel(pop0n)
ori(i,:)=ori(i,:)/norm(ori(i,:));
end
x = ori(:,1);
y = ori(:,2);
z = ori(:,3);
%// plot
m=100;
[aa,bb,cc] = sphere(m);
v = griddata(x,y,z,pop0n,aa,bb,cc,'nearest');
surf(aa,bb,cc,v)
colormap jet;
colorbar
shading interp

Plot a surface in MATLAB

I want to plot a surface in MATLAB using surf. I have this equation: x = y^2 +4z^2.
What I am doing is the following:
[x,y] = meshgrid(-4:.1:4, -4:.1:4);
z = sqrt((x - y.^2)./4); % Basically I'm just clearing for z
surf(x,y,z)
But with this I am getting the error: Error using surf X,Y,Z and C cannot be complex. I know there is a complex number because of the values that x and y have, plus the square root. Is there another way to plot a surface in MATLAB? because I really don't know what to do, and my skills are very basics.
Why do you feel that you need to grid x and y, and not use the form of the original equation itself?
This seems to work perfectly fine
[y,z] = meshgrid(-4:.1:4, -4:.1:4);
x = y.^2 + 4*z.^2;
surf(x,y,z)
to produce

3D - spline for trajectory generation in Matlab/Octave

I am currently trying to create a 3D trajectory for a robot end-effector in Octave.
The end-effector is supposed to visit a set of 3D points X = [x_1 ,..., x_n], where x_i = [xcoord_i; ycoord_i; zcoord_i]. Simple linear interpolation would lead to non-smooth robot movements. Hence I want to generate a 3D-spline curve that generates N 3D points between my reference points. There exists an Matlab implementation for this kind of task (documentation). Can you give me a hint on how to approach this kind of problem in Octave?
You can just do three one-dimensional spline interpolation on X(1,:), X(2,:) and X(3,:) using interp1, see
https://www.gnu.org/software/octave/doc/interpreter/One_002ddimensional-Interpolation.html
This should work:
t = 1:n;
ti = 0:0.01:n;
xi = interp1(t, X(1,:), ti, "spline");
yi = interp1(t, X(2,:), ti, "spline");
zi = interp1(t, X(3,:), ti, "spline");
Xi = [xi; yi; zi];
Of course, you should adapt t and ti to your needs.

Interpolating surface normals from scattered data

I have scattered data [x(:),y(:),z(:)] and I want, given a new point (x1,y1), to interpolate the surface normal at this point.
Currently I'm using "scatteredInterpolant" in matlab, and then interpolate the derivatives of the surface at the wanted point.
I would like to find a more efficient option that I can convert to mex using matlab's coder.
my current code:
F = scatteredInterpolant(X(:), Y(:), Z(:));
[Xq,Yq] = meshgrid(X(1,:),linspace(Y(1,1),Y(end,1),size(Y,1)));
Zq = F(Xq,Yq);
[ZX,ZY] = gradient(Zq);
gradX=interp2(Xq, Yq, ZX,X1, Y1);
gradY=interp2(Xq, Yq, ZY,X1, Y1);
Thanks in advance

MatLab - Euclidean Distance Plot 3D

Im new in matlab programming and I have small issue.
I want to draw a plot 3d of Euclidean distance function for 2 coordinates, like in this picture below:
Could you help me with the source code? How I can draw this plot?
My first thoughts was wrong:
[A] = meshgrid(-100:.5:100, -100:.5:100);
D1 = bwdist(A);
figure
surf(double(A), double(D1))
It is done like this...
[x, y] = meshgrid(-100:.5:100, -100:.5:100);
The you have to calculated the euclidean distances. I assume you want them with the origin.
z = (x.^2 + y.^2).^0.5; % square root of sum of squares (euclidean distance with origin)
surf(x, y, z);
NOTE: meshgrid(-100:.5:100, -100:.5:100) might make the resolution of the plot too high. If you have trouble viewing the plot, reduce the resolution.
Use [x, y] = meshgrid(-100:5:100, -100:5:100);