lsqcurvefit question (Function value and YDATA sizes are not equal.)

I am new to MATLAB. I want to do a curve fitting to get coefficent. But there is some problem. It said Function value and YDATA sizes are not equal. Where did i do mistake...? Thanks for help.
xdata=[1,5,10,20,50];
ydata=[0.3, 1.6, 2.8, 4.6, 7.6];
fun=@(x,xdata)x(1)*1-x(1)*(1-exp(((-1)*x(2))*xdata))/(x(2)*xdata);
x0=[10,5];
x=lsqcurvefit(fun,x0,xdata,ydata);
Function value and YDATA sizes are not equal.

 채택된 답변

Stephan
Stephan 2018년 7월 25일
Hi,
Use elementwise operations to declare your function:
xdata=[1,5,10,20,50];
ydata=[0.3, 1.6, 2.8, 4.6, 7.6];
fun=@(x,xdata)x(1).*1-x(1).*(1-exp(((-1).*x(2)).*xdata))./(x(2).*xdata);
x0=[10,5];
x=lsqcurvefit(fun,x0,xdata,ydata);
This version works.
Best regards
Stephan

추가 답변 (1개)

카테고리

도움말 센터File Exchange에서 Nonlinear Least Squares (Curve Fitting)에 대해 자세히 알아보기

태그

질문:

2018년 7월 25일

답변:

2018년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by