lsqcurvefit requires all values returned by functions to be of data type double.

9 views (last 30 days)
Quentin Javey
Quentin Javey on 6 Jul 2022
Commented: Torsten on 8 Jul 2022
I am having this error :
"Error using lsqncommon
lsqcurvefit requires all values returned by functions to be of data type double.
Error in lsqcurvefit (line 295)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,optimgetFlag,caller,..."
Here's my code.
syms a b c d e f theta
ydata=[10 12 5 65 32 12 25 30 15 26];
theta_Val=0:5:45;
y = (a+445)*c*cos(theta)+e/f*sin(theta)+d;
%Variable equal to 0
Var_Zero=[a b c d];
%Variable to search
Var_Search=[e f];
%Initial value
Init=[0,0];
%I replace my variable equal to 0 by 0
y_reduce = eval(subs(y,Var_Zero,zeros(size(Var_Zero))));
%I declare my function
Fonction = @(Var_Cherche,theta) y_reduce;
%I do a test to see if the size are corresponding
size(ydata) %1x10
size(Fonction(Init,theta_Val)) %1x1
%They are not
%I replace my theta with the values that I want
y_reduce=(subs(y_reduce,theta,theta_Val));
%I recreate the function
Fonction = @(Var_Search,theta) y_reduce;
%Size Test
size(ydata) %1x10
size(Fonction(Init,theta_Val)) %1x10
%They are the same size
%Then I try to fit
x = lsqcurvefit(Fonction,Init,theta_Val,ydata)
The problem comes with how I handle theta in my function but I don't know what to change.

Accepted Answer

Torsten
Torsten on 6 Jul 2022
Edited: Torsten on 6 Jul 2022
Setting y = e/f*sind(theta) makes no sense because this setup will have infinitely many solutions.
Use a single parameter e instead.
syms a b c d e theta
ydata=[10 12 5 65 32 12 25 30 15 26];
theta_Val=0:5:45;
y = (a+445)*c*cosd(theta)+e*sind(theta)+d;
%Variable equal to 0
Var_Zero=[a b c d];
%Variable to search
Var_Search=[e];
%Initial value
Init=[1];
%I replace my variable equal to 0 by 0
y_reduce = eval(subs(y,Var_Zero,zeros(size(Var_Zero))));
%I declare my function
Fonction = @(Var_Cherche,theta) y_reduce;
%I do a test to see if the size are corresponding
%size(ydata) %1x10
%size(Fonction(Init,theta_Val)) %1x1
%They are not
%I replace my theta with the values that I want
y_reduce=(subs(y_reduce,theta,theta_Val));
%I recreate the function
%Fonction = @(Var_Search,theta) y_reduce
Fonction = matlabFunction(y_reduce)
Fonction = function_handle with value:
@(e)[0.0,e.*sin(pi./3.6e+1),e.*sin(pi./1.8e+1),-e.*(sqrt(2.0)./4.0-sqrt(6.0)./4.0),e.*sin(pi./9.0),e.*sin(pi.*(5.0./3.6e+1)),e./2.0,e.*sin(pi.*(7.0./3.6e+1)),e.*sin(pi.*(2.0./9.0)),(sqrt(2.0).*e)./2.0]
Fonction = @(x,theta_Val)Fonction(x);
%Size Test
%size(ydata) %1x10
%size(Fonction(Init,theta_Val)) %1x10
%They are the same size
%Then I try to fit
x = lsqcurvefit(Fonction,Init,theta_Val,ydata)
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 48.8708
Fonction(x,theta_Val)-ydata
ans = 1×10
-10.0000 -7.7406 3.4863 -52.3513 -15.2852 8.6537 -0.5646 -1.9689 16.4135 8.5569
  5 Comments
Torsten
Torsten on 8 Jul 2022
According to the error message, your function "Angle" does not depend on y_l_RX_P.
Thus you'll have to define
Fonction = @(x,theta_Val)Fonction(x(1),x(2));
instead of
Fonction = @(x,theta_Val)Fonction(x(1),x(2),x(3));
Since the source error is always the same in your requests:
After defining
Fonction = matlabFunction(something);
look at the list of input parameters to the function and count them. If the total number is N, redefine Fonction as
Fonction = @(x,theta_Val)Fonction(x(1),x(2),x(3),...,x(N))

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by