Why do I receive "Error using lsqcurvefit (line 251) Function value and YDATA sizes are not equal."

2 views (last 30 days)
f = ...
[15 25 35 50 65 80];
s = ...
[1070 1602 1931 2100 2041 1876];
plot(f,s,'ro')
title('Data points')
F1 = @(x,xdata)x(1)*sin(xdata*pi/180)*(1-exp(-50*x(2)));
x0 = [1000 0.01];
x1 = lsqcurvefit(F1,x0,f,s)
plot(f,F1(x1,f))
F2 = @(x,xdata)(x(1)*sin(xdata*pi/180)*(1-exp(-50*x(2)))/(1-cos(xdata*pi/180)*exp(-50*x(2))));
x0 = [1000 0.01];
x2 = lsqcurvefit(F2,x0,f,s)
plot(f,F2(x2,f))
%The program works with function F1 but not function F2

Accepted Answer

Star Strider
Star Strider on 3 Aug 2021
Do element-wise division
F2 = @(x,xdata)(x(1)*sin(xdata*pi/180)*(1-exp(-50*x(2)))./(1-cos(xdata*pi/180)*exp(-50*x(2))));
↑ ← HERE
and it works!
f = ...
[15 25 35 50 65 80];
s = ...
[1070 1602 1931 2100 2041 1876];
figure
plot(f,s,'ro')
hold on
title('Data points')
F1 = @(x,xdata)x(1)*sin(xdata*pi/180)*(1-exp(-50*x(2)));
x0 = [1000 0.01];
x1 = lsqcurvefit(F1,x0,f,s)
Local minimum possible. lsqcurvefit stopped because the size of the current step is less than the value of the step size tolerance.
x1 = 1×2
1.0e+03 * 2.4952 0.0016
plot(f,F1(x1,f))
F2 = @(x,xdata)(x(1)*sin(xdata*pi/180)*(1-exp(-50*x(2)))./(1-cos(xdata*pi/180)*exp(-50*x(2))));
x0 = [1000 0.01];
x2 = lsqcurvefit(F2,x0,f,s)
Local minimum possible. lsqcurvefit stopped because the size of the current step is less than the value of the step size tolerance.
x2 = 1×2
1.0e+03 * 2.4952 0.0014
plot(f,F2(x2,f),'--')
hold off
legend('Data','F_1','F_2', 'Location','best')
.
.
  1 Comment
Alex Sha
Alex Sha on 5 Aug 2021
F1 = @(x,xdata)x(1)*sin(xdata*pi/180)*(1-exp(-50*x(2)));
The above F1 function can be simplified to "F1 = @(x,xdata)x(1)*sin(xdata*pi/180);", with the exactly same result, but avoiding multiple solutions.
For F2 function, the best result should be:
Root of Mean Square Error (RMSE): 14.9218445307446
Sum of Squared Residual: 1335.96866519828
Correlation Coef. (R): 0.999332922291571
R-Square: 0.998666289575811
Parameter Best Estimate
---------- -------------
x1 4323.03442875393
x2 0.00984518829719931

Sign in to comment.

More Answers (0)

Categories

Find more on Descriptive Statistics in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!