How to use fminsearch for a function converted from symbolic expression
Show older comments
Hello,
I am trying to fit a plane to a set of data. I'd like to write my own function with fminsearch function.
I created the objective function using symbolic, and then I convert the symbolic expression to a function handle using "matlabFunction". Finally, I pass the function handle to the fminsearch function. However, Matlab reports "Not enough input arguments" How do I fix this?
My code is as the following
% I have 36 3D points. I'd like to use a plane to fit them
% Pcr_th is a 36-3 matrix that contains the point coordinates, the columns represent X, Y and Z coordinates
syms A B C D
sum_sqd = 0;
for i = 1:36
d = ( A*Pcr_th(i,1) + B*Pcr_th(i,2) +C*Pcr_th(i,3) + D) / (sqrt(A*A+B*B+C*C));
sqd = d*d;
sum_sqd = sum_sqd + sqd;
end
% find the centroid
ctroid_x = sum(Pcr_th(i,1))/36;
ctroid_y = sum(Pcr_th(i,2))/36;
ctroid_z = sum(Pcr_th(i,3))/36;
% initial guess for D
Dint = -(ctroid_x+ ctroid_y + ctroid_z);
% Create the objective function
fun = matlabFunction(sum_sqd,'vars',[A,B,C,D]);
intg = [1, 1, 1, Dint]; % this is the initial guess.
a = fminsearch (fun, intg) % This reports Not enough input arguments. And error in fun=matlabFunction...
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!