lsqcurvefit requires all values returned by functions to be of data type double.
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Torsten
il 6 Lug 2022
Modificato: Torsten
il 6 Lug 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 = @(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)
Fonction(x,theta_Val)-ydata
5 Commenti
Torsten
il 8 Lug 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))
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!