# lsqcurvefit with two functions and same vriables

조회 수: 16(최근 30일)
Yama Abawi 2021년 4월 22일
댓글: Hung Huu Vo 2022년 11월 22일 17:22
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?
##### 댓글 수: 1표시숨기기 없음
Hung Huu Vo 2022년 11월 22일 17:22

댓글을 달려면 로그인하십시오.

### 채택된 답변

Matt J 2021년 4월 22일
No, the problem is that YY is not 8x2.
##### 댓글 수: 1표시숨기기 없음
Yama Abawi 2021년 4월 22일
:-)
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.

댓글을 달려면 로그인하십시오.

### 범주

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

Translated by