Correction of code for Curve fitting

2 次查看(过去 30 天)
d=...
[1 0 1
2 0 1
3 0 1
4 0 1
5 0 1
6 0 1
7 0.000037 0.999963
8 0 1
9 0 1
10 0 1
11 0 1
12 0 1
13 0 1
14 0 1
15 0 1
16 0 1
17 0 1
18 0 1
19 0 1
20 0 1
21 0 1
22 0 1
23 0 1
24 0 1
25 0.000074 0.999926
26 0 1
27 0 1
28 0 1
29 0 1
30 0 1
31 0 1
32 0.000105 0.999895
33 0 1
34 0.000074 0.999926
35 0 1
36 0.000167 0.999833
37 0.000011 0.999989
38 0.000137 0.999863
39 0.000163 0.999837
40 0 1
41 0.000052 0.999948
42 0.000159 0.999841
43 0.000295 0.999705
44 0.001635 0.998365
45 0.012518 0.987482
46 0.029448 0.970552
47 0.022927 0.977073
48 0.014557 0.985443
49 0.008347 0.991653
50 0.005198 0.994802
51 0.003137 0.996863
52 0.002566 0.997434
53 0.004579 0.995421
54 0.009629 0.990371
55 0.0566 0.9434
56 0.11717 0.88283
57 0.136539 0.863461
58 0.077873 0.922127
59 0.097373 0.902627
60 0.213279 0.786721
61 0.439796 0.560204
62 0.679317 0.320683
63 0.79856 0.20144
64 0.835889 0.164111
65 0.854733 0.145267
66 0.870323 0.129677
67 0.883056 0.116944
68 0.89085 0.10915
69 0.896808 0.103192
70 0.901832 0.098168
71 0.906941 0.093059
72 0.912774 0.087226
73 0.916352 0.083648
74 0.923203 0.076797
75 0.926324 0.073676
76 0.930451 0.069549
77 0.933807 0.066193
78 0.935963 0.064037
79 0.938749 0.061251
80 0.941213 0.058787
81 0.944072 0.055928
82 0.946399 0.053601
83 0.948934 0.051066
84 0.950979 0.049021
85 0.952639 0.047361
86 0.954867 0.045133
87 0.955925 0.044075
88 0.959433 0.040567
89 0.95997 0.04003
90 0.962362 0.037638
91 0.963256 0.036744
92 0.965582 0.034418
93 0.965949 0.034051
94 0.967992 0.032008
95 0.969444 0.030556
96 0.970288 0.029712
97 0.971594 0.028406
98 0.972755 0.027245
99 0.974647 0.025353
100 0.975369 0.024631
101 0.977052 0.022948
102 0.977606 0.022394
103 0.9784 0.0216
104 0.979068 0.020932
105 0.980051 0.019949
106 0.981965 0.018035
107 0.981666 0.018334
108 0.981889 0.018111
109 0.983013 0.016987
110 0.983737 0.016263
111 0.984486 0.015514
112 0.984662 0.015338
113 0.985374 0.014626
114 0.986094 0.013906
115 0.986944 0.013056
116 0.988056 0.011944
117 0.987725 0.012275
118 0.989148 0.010852
119 0.988741 0.011259
120 0.989182 0.010818
121 0.990009 0.009991
122 0.990414 0.009586
123 0.991178 0.008822
124 0.99144 0.00856
125 0.992042 0.007958
126 0.992104 0.007896
127 0.992555 0.007445
128 0.992916 0.007084
129 0.993574 0.006426
130 0.993736 0.006264
131 0.994157 0.005843
132 0.994434 0.005566
133 0.994576 0.005424
134 0.994984 0.005016
135 0.995202 0.004798
136 0.99526 0.00474
137 0.995948 0.004052
138 0.995785 0.004215
139 0.996173 0.003827
140 0.996602 0.003398
141 0.996508 0.003492
142 0.996771 0.003229
143 0.997203 0.002797
144 0.997377 0.002623
145 0.997366 0.002634
146 0.997502 0.002498
147 0.997718 0.002282
148 0.997964 0.002036
149 0.997837 0.002163
150 0.998121 0.001879
151 0.998246 0.001754
152 0.99846 0.00154
153 0.998757 0.001243
154 0.998778 0.001222
155 0.998876 0.001124
156 0.999046 0.000954
157 0.999119 0.000881
158 0.999218 0.000782
159 0.999086 0.000914
160 0.999439 0.000561
161 0.999629 0.000371
162 0.999496 0.000504
163 0.999669 0.000331
164 0.999708 0.000292
165 1 0
166 0.999843 0.000157
167 0.999882 0.000118
168 1 0
169 1 0
170 1 0
171 1 0
172 1 0
173 1 0
174 1 0
175 1 0
176 1 0
177 1 0
178 1 0
179 1 0
180 1 0
181 1 0
182 1 0
183 1 0
184 1 0
185 1 0
186 1 0
187 1 0
188 1 0
189 1 0
190 1 0
191 1 0
192 1 0
193 1 0
194 1 0
195 1 0
196 1 0
197 1 0
198 1 0
199 1 0
200 1 0
];
x=d(:,1);
% y1=d(:,2);
y2=d(:,3);
% plot(x,y,'b*')
constant=lsqcurvefit(@L5P7,[1.0035631;0.000015740085;90;-15;0.15],x,y2);
A=constant(1);
B=constant(2);
C=constant(3);
D=constant(4);
E=constant(5);
xfit=0:1:200;
yfit=L5P7(constant,xfit);
figure
plot(x,y2,'g*')
hold on
plot(xfit,yfit,'b-','linewidth',2)
% grid on
disp(A)
disp(B)
disp(C)
disp(D)
disp(E)
*************************
function
**************************
function cf=L5P7(constant,x)
cf = constant(1)+((constant(2)-constant(1))./((1+((x./constant(3)).^constant(4))).^constant(5)));
end
***********************************************************************************************
I have written the above code to fit a the 5Parameter Logistic equation (L5P). However as can be seen in the graph, the fit is not inline with the data as expected. If someone could adjust or modify my code so as to fit the data accurately and let me have the modified data. Kindly not that the plot is for the x-values (the 1st column and one of the two y-column i.e. column 2 and 3. one is a reverse of the other.
And by the way, I posted a question on this platform about installation of Matlab, student version. Am glad to report that I was able to install it successfully thanks to the advice I got from friends on this platform.
  1 个评论
John D'Errico
John D'Errico 2018-6-21
What you don't seem to appreciate is this model simply does not fit your data. You cannot make it do so, by trying slightly other sets of parameters, by letting the optimizer try harder. This is now the second time you have asked the question.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2018-6-21
编辑:Walter Roberson 2018-6-21
If you increase the maximum number of function evaluations and switch to marquardt, you can do slightly better, reaching parameters
0.992753402229052 0.0165694036747047 50.6403017630613 -29.0747052519667 176.250137513041
which has pretty much no visible effect on the display.
There is a catch-basin near that set of parameters: with plotting you can see that there is a favored point quite near those parameters, with residue about 0.128.
However, if you do a global search, you reach parameters close to
0.015026059670688 0.992253891841139 59.2243915203201 100.091982688792 0.208908924049189
which is a catch basin that has slightly better results than the other one, with residue about 0.113.
Notice that all of the parameters are quite different than the other ones, except for C. See the recent discussion at https://www.mathworks.com/matlabcentral/answers/405549-custom-equation-in-curve-fitting#comment_578562 about why that kind of big difference can happen in curve fitting.
To my eye, the curve with these adjusted parameters is steeper and looks like it should be less of a fit on the rights side. However, with those repeated exact zeros at the end of your data, I can accept that plausibly it comes out marginally better on residue.
  7 个评论
Walter Roberson
Walter Roberson 2018-6-21
x0 = [0.0150260600194573 0.992253891130622 59.2243915462699 100.091986983342 0.208908919228146] .';
opts = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt','MaxFunctionEvaluations', 5000);
lb = []; ub = [];
constant = lsqcurvefit(@L5P7, x0, x, y2, lb, ub, opts);
Walter Roberson
Walter Roberson 2018-6-21
编辑:Walter Roberson 2018-6-21
My tests show that near that point, improvements in residue are due to numeric round-off instead of due to theoretical improvements in position. So the theoretical parameters might be near there rather than exactly there, but the above is pretty close to the best you can do numerically... at least with the order that the Symbolic Toolbox happened to do the calculations in when I set it up.
... And what that should tell you is that your model is visibly not the right one for your data.
Note that to produce a continuous curve that goes exactly through all those repeated 0 and 1, you would probably need to go for a polynomial of order 199... but that would fail since it would involve values on the order of 200^199 which is about 10^458.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by