0

I am trying to fit 3D data points (>1000) with the implicit function of a cylinder (5 parameters : a,b,c,d,r) :

 -r + sqrt((x-(x+a*(-b+y)+c*(-d+z))/(1+a^2+c^2))^2+(-b+y-(a*(x+a*(-b+y)+c*(-d+z)))/(1+a^2+c^2))^2+(-d+z-(c*(x+a*(-b+y)+c*(-d+z)))/(1+a^2+c^2))^2) == 0

I can't find a good way to implement that with Matlab (my knowledge is till very shallow in Matlab syntax). It would be much easier with a explicit function for sure. I have looked extensively on the net and I haven't found any specific answer.

I have also the parametric function of the cylinder using the same parameters if you know a way to fit the parametric equation directly?

x = v-(c*r*cos(u))/(sqrt(1+c^2))-(a*r*sin(u))/((1+c^2)*sqrt(1+(a^2)/(1+c^2)));
y = b+a*v+(r*sin(u))/(sqrt(1+(a^2)/(1+c^2)));
z = d+c*v+(r*cos(u))/(sqrt(1+c^2))-(a*c*r*sin(u))/((1+c^2)*sqrt(1+(a^2)/(1+c^2)));

Thanks a lot in advance.

B.jour
  • 523
  • 1
  • 5
  • 10

1 Answers1

0

You can use cftool with a user define function of the cylinder form you specify above. From the docs,

cftool( x, y, z ) creates a surface fit to x and y inputs and z output. x, y, and z must be numeric, have two or more elements, and have compatible sizes. Sizes are compatible if x, y, and z all have the same number of elements or x and y are vectors, z is a 2D matrix, length(x ) = n, and length(y) = m where [m,n] = size(z). cftool opens Curve Fitting app if necessary.

If you just type cftool it will open an interactive session where you can try some fits (assuming you have the toolbox)...

EDIT: You can use fsolve in this case. Save the cylinder function to a separate file called cylinder, where the code is something like:

function F = cylinder(P, X)

    a = P(1);
    b = P(2);
    c = P(3);
    d = P(4);
    r = P(5);
    x = X(1,:);
    y = X(2,:);
    z = X(3,:);

    % f(x,y,z) - r = 0
    F = sqrt(  (   x-(  (x+a*(-b+y)+c*(-d+z)))/(1+a^2+c^2)).^2 ...
              +(-b+y-(a*(x+a*(-b+y)+c*(-d+z)))/(1+a^2+c^2)).^2 ...
              +(-d+z-(c*(x+a*(-b+y)+c*(-d+z)))/(1+a^2+c^2)).^2 ) -r;

end

I've assumed you have the correct form of cylinder here. You would call this with you N data points, X in form (3,N),

startparams = [0,0,0,0,1]
coeff=fsolve('cylinder',startparams,[],X)
Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • unfortunatly, I as far as I played with cftool, there is no way to defind an implicit function. you need to define a custom explicit function in the form : z = f(x,y) What I am trying to do is to fit with a function of the form : f(x,y,z) = 0 – B.jour Jul 03 '15 at 14:16
  • Hey Ed, thank you a lot for your help. unfortunatly the last solution is not working neither because the fsolve does not see X as the coordinate he should use in the function, if in understand well what this script does. I get the "Not enough input arguments" error. I have tried to go around it but its still not working... I can feel this is very close to the final solution though.. – B.jour Jul 22 '15 at 14:16