Effacer les filtres
Effacer les filtres

lsqcurvefit with two functions and same vriables

2 vues (au cours des 30 derniers jours)
Yama Abawi
Yama Abawi le 22 Avr 2021
Commenté : Hung Huu Vo le 22 Nov 2022
Hi, this is my problem:
I have two data sets X1,Y1 and X1,Y2. For exmaple
X1 =
0.0079
0.0157
0.0314
0.0629
Y1 =
2.5172
3.0338
2.5687
2.2744
Y2 =
0.3167
0.7461
0.3454
0.3072
Now the data set X1,Y1 has to be fitted with the function fun_1 and the data set X1,Y2 with the function fun_2 using lsqcurvefit.
This is my code, which is NOT working:
% Putting both data sets in one vector for X and one vector for Y:
XX = [X1;X1]
YY = [Y1;Y1]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Start and boundary data
%%%-----Parameter(1):
start_p1=10;
lb_p1=0;
ub_p1=100;
%%%-----Parameter(2):
start_p2=-0.5;
lb_p2=-1;
ub_p2=0;
parameterstart = [start_p1,start_p2];
lb =[lb_p1,lb_p2];
ub =[ub_p1,ub_p2];
% Declaration of the functions
fun_1 = @(parameter,x)parameter(1).*(x).^parameter(2);
fun_2 = @(parameter,x)(-parameter(1).*cot(((parameter(2)+1)*pi)/2)).*(x).^parameter(2);
% Putting both functions in one array
fun_12=@(parameter,x)[fun_1(parameter,x), fun_2(parameter,x)];
% Fitting with lsqcurvefit
parameter = lsqcurvefit(fun_12,parameterstart,XX,YY,lb,ub)
The error is
Error using lsqcurvefit (line 262)
Function value and YDATA sizes are not equal
The problem is that XX has the dimension 8x1, but since XX is addressed by fun_12 twice (since it contains two functions fun_1 and fun_2) , fun_12 becomes the dimension 8x2.
How can this problem be solved?
Thanks in Advance :-)
  1 commentaire
Hung Huu Vo
Hung Huu Vo le 22 Nov 2022
Thank you very much for your super helpful question

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 22 Avr 2021
No, the problem is that YY is not 8x2.
  1 commentaire
Yama Abawi
Yama Abawi le 22 Avr 2021
:-)
Thanka alot Matt J for the quick response. It helped. I adapted YY and now I dont get the error message, but I need to check if the results are reasonable.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by