Fitting 4 parameters with constraints

2 views (last 30 days)
Alfredo Scigliani
Alfredo Scigliani on 30 Apr 2022
Commented: Alex Sha on 30 Apr 2022
I am trying to fit a function to a data set in order to fit 4 parameters. I have been using lsqcurvefit because it is the only way that I know how to do a good easy fitting. But the problem is that I need to add constraints to my parameters. This is not possible using lsqcurvefit. I wonder if there is a function or a manual way of doing this.
where A1 D1 A2 D2 are unknown cosntants.
The constraints I need to have is that D1 and D2 are less than 0.001 (D1 and D2 < 0.0008). Any solution that includes either D1 or D2 greater than that is not of interest.
A solution that lsqcurvefit is finding is
A1 = 0.364
D1 = 0.001338
A2 = 0.610
D2 = 0.000110
*****Plot as a semilogY ****
D1 in this case is greater than my desired constraint. I would like for it to skip it and keep iterating.
I will include my data below if anybody could help me I appreciate it!
clear; clc; clf; close all;
D_0 = [0.5 0.00001 0.5 0.00001]; %initial guess if necessary for A1 D1 A2 D2 respectively
xdata = [9.85 32.05 66.83 114.44 174.55 247.57 333.00 431.44 542.19 666.04 802.12 951.39 1113.38 1287.47 1474.87 1674.29 1887.11 2600.14 2863.78 3139.17 3428.23 4043.41 4369.45 4709.33 5425.99 5802.67 6193.38 6595.39];
ydata = [1 0.9569 0.9528 0.8894 0.8387 0.8995 0.7911 0.773 0.7523 0.7155 0.7086 0.6478 0.6269 0.6175 0.574 0.551 0.4991 0.4559 0.4449 0.4314 0.4212 0.407 0.3856 0.3511 0.3526 0.303 0.3148 0.2912];

Answers (1)

Star Strider
Star Strider on 30 Apr 2022
But the problem is that I need to add constraints to my parameters. This is not possible using lsqcurvefit.
Yes, it is, however only constraint bounds.
objfcn = @(b,x) b(1).*exp(-b(2).*x) + b(3).*exp(-b(4).*x);
D_0 = [0.5 0.00001 0.5 0.00001];
xdata = [9.85 32.05 66.83 114.44 174.55 247.57 333.00 431.44 542.19 666.04 802.12 951.39 1113.38 1287.47 1474.87 1674.29 1887.11 2600.14 2863.78 3139.17 3428.23 4043.41 4369.45 4709.33 5425.99 5802.67 6193.38 6595.39];
ydata = [1 0.9569 0.9528 0.8894 0.8387 0.8995 0.7911 0.773 0.7523 0.7155 0.7086 0.6478 0.6269 0.6175 0.574 0.551 0.4991 0.4559 0.4449 0.4314 0.4212 0.407 0.3856 0.3511 0.3526 0.303 0.3148 0.2912];
[B,rsdnrm] = lsqcurvefit(objfcn, D_0, xdata, ydata, -Inf(1,4), [Inf 0.001 Inf 0.001])
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
B = 1×4
-18.9893 0.0001 19.7668 0.0001
rsdnrm = 0.1898
xv = linspace(min(xdata), max(xdata));
figure
plot(xdata, ydata, 'pg')
hold on
plot(xv, objfcn(B,xv), '-r')
hold off
grid
xlabel('x')
ylabel('y')
.
  2 Comments
Alex Sha
Alex Sha on 30 Apr 2022
if want D1 and D2 are all less than 0.001:
Sum Squared Error (SSE): 0.0116703699647364
Root of Mean Square Error (RMSE): 0.0204156539770838
Correlation Coef. (R): 0.995666057914124
R-Square: 0.991350898882251
Parameter Best Estimate
---------- -------------
a1 0.428678645876139
a2 0.534815808010459
d1 0.001
d2 8.58505177096869E-5
while, if want D1 and D2 are all less than 0.0008:
Sum Squared Error (SSE): 0.0140886399832964
Root of Mean Square Error (RMSE): 0.0224313555918754
Correlation Coef. (R): 0.994765584731217
R-Square: 0.989558568565641
Parameter Best Estimate
---------- -------------
a1 0.495993548397224
a2 0.458843635970715
d1 0.0008
d2 5.99343414652501E-5

Sign in to comment.

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!