Clear Filters
Clear Filters

lsqcurvefit with two functions and same vriables

2 views (last 30 days)
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 :-)

Accepted Answer

Matt J
Matt J on 22 Apr 2021
No, the problem is that YY is not 8x2.
  1 Comment
Yama Abawi
Yama Abawi on 22 Apr 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!