Optimization problem for curve fitting

3 次查看(过去 30 天)
I have two vectors that represent the coordinates of the graph by x 'dv' and y 'tv' (the same gn_nach) - this is a graph of a rapidly oscillating function, there are also several experimental points [mlbt1;mlbt2], I cut out some points from the initial graph and make a curve fit on it. I add the number 'a_0' to the original vector 'dv' and because of this, the fitted curve changes slightly. It is necessary to write an optimization so that the parameter 'a_0' is selected in such a way that the fitted curve is as close as possible to the experimental points. How can this be implemented?
I have attached all the necessary data to this letter
Thank you in advance!
My code:
load("data_for_question.mat")
format longG
cf = [0.988784475880488; -6.45423342769443; 13.1018505963881; -7.78863976068779; -7.26648864607634; 17.1887649536877; -13.0967066416688];
d = 0.4053/sqrt(3);
ii = 2;
a_0 = 0.2;
general_t_d(:,1) = gn_nach(:,1);
general_t_d(:,2) = gn_nach(:,2);
general_t_d(:,2) = general_t_d(:,2) + a_0; % addition a_0
while ii < size(general_t_d,1)
if abs(general_t_d(ii+1,2) - general_t_d(ii,2)) < d
general_t_d(ii+1,:) = [];
else
ii = ii + 1;
end
end
disp(general_t_d)
1.95056704469646 2.31198648022987 1.95183455637091 2.31200681924122 1.09372915276851 2.5512467534916 0.610807204803202 2.7882122778779 0.438425617078052 3.02226601248271 1.95056704469646 3.38937620554708 1.32695130086724 3.62902284919277 0.897264843228819 3.86969622363147 0.581897919397117 4.10527018012008 1.95056704469646 4.47541563303097 1.44229486324216 4.72425713071008 1.09265262350175 4.96717811874583 0.75530353569046 5.20240122442008 1.95056704469646 5.49942817690422 1.4917278185457 5.73344058764005 1.1418945963976 5.98109704279565 0.86050700466978 6.22360710072318 1.87578385590394 6.45777931117048 1.57031354236157 6.70344691875371 1.25216811207472 6.94276541083913 1.00394798305202 7.18999332888592 0.780653769179453 7.4302269022005 1.67171447631755 7.67173722561265 1.35737158105404 7.90773433071888 1.11706898932774 8.14262841894596 0.92768512341561 8.38929701604355 0.729953302201468 8.62910077048108 1.47525016677785 8.86313357047468 1.22808539026017 9.10125726086541 1.03162108072048 9.34906946417876 0.879972682923953 9.5942628418946 0.794596397598399 9.82832601596793 1.32695130086724 10.0707182673311 1.1444296197465 10.3101766066831 1.00414838906629 10.5468979319546 0.836424282855237 10.7960407404439 1.48539026017345 11.0335802002879 1.2590535100206 11.272715143429 1.06964643095397 11.5227494761058 0.947965310206804 11.7568774977058 0.821638451221217 11.9985323549033 1.40046697798532 12.2326199301088 1.19259506337558 12.4668290907927 1.04809873248833 12.7154148812944 0.947476109712108 12.9511674449633 0.803468979319546 13.1860786774367 1.29164586941845 13.4501667778519 1.1520346897932 13.6851697326721 1.00500333555704 13.9453478217707 0.906137424949967 14.1902150820273 1.36370913942628 14.4410375817797 1.22048032021348 14.6925036970735 1.07725150100067 14.9461885660246 0.958105403602402 15.186278131859 0.842761841227485 15.4355358664406 1.2917632485345 15.672981987992 1.12795196797865 15.9188725103273 1.02021347565043 16.1530129985366 0.935290193462308 16.3921562012548 0.794596397598399 16.626709542885 1.19386257505003 16.8625386315346 1.09372915276851 17.1162043888775 1.0039986729418 17.3514342895264 0.89599733155437 17.6015578579176 1.2927284856571 17.8359081955753 1.14398283255486 18.0772515010007 1.04302868579053 18.3249813757778 0.978492955771051 18.5762508338893 0.869379586390927 18.8103188667458 1.22935290193462 19.0443407468806 1.11713171767693 19.3020680453636 1.01514342895264 19.5450721572932 0.923882588392262 19.7953255091084 0.836424282855237 20.0300591349773 1.1469646430954 20.2802045670537 1.09275362231964 20.5268845897265 1.00246831220814 20.7772120029345 0.911207471647765 21.0162515103287 1.22737372137219 21.2527018012008 1.09372915276851 21.5004298766757 1.00500333555704 21.7478286647003 0.973315543695797 21.9873837946967 0.880787191460974 22.223912240154 1.17104736490994 22.4683446322955 1.09297878658882 22.7043362241494 1.00406025435067 22.9765176784523 0.916277518345564 23.2151905768332 0.985990660440294 23.4495394146435 1.14386558653589 23.7023348899266 1.05823882588392 23.9574088631971 0.985280566944701 24.2013342228152 0.911207471647765 24.4388774232133 1.20146764509673 24.6886979027086 1.11710195567996 24.9271514342895 1.04429619746498 25.1820520783688 0.966207432799916 25.4261507671781 0.868112074716478 25.6626334496424 1.16619704663891 25.9251501000667 1.08865910607071 26.1702621506924 1.02274849899933 26.4046661044226 0.947090968670842 26.650967311541 1.16724482988659 26.9160190407823 1.06330887258172 27.1618910492436 1.04084402491104 27.4221480987325 0.969513008672448 27.6898964903651 0.887124749833222 27.9303180537316 1.10640426951301 28.1680981805406 1.02781854569713 28.407785628618 0.966705574444937 28.6469646430954 0.936557705136758 28.8945417798852 1.03035356904603 29.1287567046509 1.09261910355634 29.3727818545697 1.00500333555704 29.6383130859414 0.947103557565579 29.9171447631755 0.865577051367578 30.1662147529427 1.11702874109905 30.416144096064 1.0472964922284 30.6883255503669 0.974583055370247 30.9489026665336 0.92768512341561 31.1850320650277 1.16470980653769 31.4532466682974 1.06964643095397 31.6874846011911 1.01007338225484 31.9413178575794 0.96621648820001 32.1853235490327 0.885857238158773 32.4421780830674 1.11760845567516 32.6843228819213 1.04809873248833 32.9202426918961 1.00426896463719 33.1833222148099 0.93022014676451 33.4437169882574 1.1469646430954 33.678473646572 1.0926210490398 33.9545030020013 1.02592917770098 34.2266844563042 0.966012311659512 34.4988659106071 0.889659773182121 34.743870185533 1.11692068955546 34.9978652434957 1.0472222438287 35.2700466977985 0.985217595280125 35.5422281521014 0.929087340209799 35.8144096064043 1.11781187458306 36.0523690428751 1.06862732042935 36.3134089392929 1.02021347565043 36.5550942891773 0.965831750326961 36.8124082721815 0.92895263509006 37.0464096318012 1.10133422281521 37.2841099487719 1.04747911819037 37.5382254836558 1.00373582388259 37.7723644842437 0.947090873758793 38.0372248165444 1.1418945963976 38.3036540133173 1.07851901267512 38.5380411523073 1.02580945477192 38.8084056037358 0.983455637091394 39.0425021721766 0.911207471647765 39.2817255244518 1.11678803692452 39.5342228152101 1.04755930935892 39.806404269513 1.00420643579178 40.0785857238159 0.946828201057508 40.3507671781187 1.14316210807205 40.6005794710519 1.06903840980496 40.8497665110073 1.02546142877015 41.1219479653102 0.966217915144011 41.3941294196131 1.16344229486324 41.6354849268512 1.09259579756649 41.8931287525017 1.04723151800266 42.1653102068045 0.993595730486991 42.3996574934904 0.941627751834556 42.6444029685343 1.11729045156234 42.8911274182788 1.06869659041629 43.1633088725817 1.01641094062708 43.4090096014438 0.984820974011997 43.6623082054703 1.16597731821214 43.9186417781824 1.09313442697235 44.1613075383589 1.04705881196496 44.4334889926618 0.985990660440294 44.6818857170395 0.946697798532355 44.9243858418283 1.11754651871733 45.1593062041361 1.06887400818029 45.431487658439 1.02559256428957 45.7036691127418 0.966239483899075 45.9758505670447 1.09372915276851 46.2173437989638 1.08114935022548 46.4748498999333 1.04695095771273 46.7470313542362 0.994863242161441 46.9814549360937 0.931487658438959 47.2160812994478 1.03669112741828 47.4574063893863 1.02580167660156 47.6996664442962 1.01514342895264 47.9339132147304 0.985371521902603 48.1986657771848 1.03795863909273 48.4506085785074 0.985990660440294 48.6920739205454 0.973315543695797 48.9552208125053 0.963818776361313 49.196664442962 0.953072782466872 49.4688458972648 1.00431177912949 49.7410273515677 0.965841983289118 50.0132088058706 0.923882588392262 50.282799346099 0.913742494996664 50.5370247632567 1.02597958288182 50.784389593062 0.977118078719146 51.0498237832294 0.934022681787859 51.3158574555225 0.897136665895079 51.5555703802535 0.889659773182121 51.79237614563 1.00439879878524 52.0545697131421 0.966151017166871 52.326751167445 0.920080053368913 52.5905632531156 0.880491671299985 52.8257505003336 1.02586603680582 53.0979319546364 0.984838779387761 53.3701134089393 0.946969160657516 53.6422948632422 0.897264843228819 53.8792422221989 1.02655103402268 54.1358155025525 1.00381599009053 54.4134756504336 0.947965310206804 54.6803100427865 0.92768512341561 54.9166802218577 0.875717144763175 55.1597154898391 1.0045381718894 55.4114743162108 0.966977985323549 55.6664646723951 0.947402494114771 55.9104736490994 0.912694687002191 56.1826551034023 1.03795863909273 56.4172379037751 0.985628132893589 56.6816544362909 0.947965310206804 56.9160066323558 0.929042470294818 57.1806537691795 0.889659773182121 57.4147645254477 1.00500333555704 57.6723047412867 0.985064421543486 57.9064709806538 0.946997330650962 58.1786524349566 0.908672448298866 58.4303159972684 1.04708162753758 58.6776517678452 1.00373582388259 58.9267548935662 0.966457492013145 59.1766511007338 0.929552600674061 59.4488325550367 0.870647098065377 59.7092742022347 1.02541691528452 59.9478318879253 0.984896603989016 60.2200133422282 0.946934708855775 60.492194796531 0.912667361229559 60.7643762508339 1.04302868579053 60.9984434765843 0.987258172114743 61.2599906628788 0.966090271778981 61.5355570380253 0.929161392496581 61.8077384923282 1.06837891927952 62.0420586457787 1.00458760751953 62.3067378252168 0.984859946205431 62.5789192795197 0.946880944605532 62.8511007338225 0.89599733155437 63.1087803668797 1.02591172366694 63.3501000667112 0.985454947849998 63.622281521014 0.96444296197465 63.8660649507295 0.929369606621262 64.1212808539026 1.02587776356795 64.3934623082055 1.00373582388259 64.6513660607754 0.982188125416945 64.8863290835257 0.946697798532355 65.1544827910671 0.896354807740449 65.3914609739826 1.00460306597311 65.6636424282855 0.975850567044696 65.9011910713247 0.958105403602402 66.1415074782017 0.929559054364037 66.3894596397598 1.02655103402268 66.6235532990256 1.00246831220814 66.8818629856442 0.966354748619496 67.1606404269513 0.929743949733587 67.4328218812542 0.916277518345564 67.669869955382 1.0044738932542 67.9318212141428 0.983455637091394 68.1963601282025 0.947781215809683 68.4308205470313 0.920080053368913 68.6726223153197 1.04429619746498 68.9072316117933 1.00412951746991 69.1566377585057 0.966391200967495 69.4288192128085 0.941627751834556 69.6737192972134 0.89634015422985 69.9278185456971 1.01260840560374 70.1874801270982
%load("mlbt1","mlbt1")
%load("mlbt2","mlbt2")
t_v = general_t_d(:, 1);
d_v = general_t_d(:,2);
%f = fit(d_v,t_v,'rat33');
f = fit(d_v, t_v, 'rat33','StartPoint', [0.988784475880488; -6.45423342769443; 13.1018505963881; -7.78863976068779; -7.26648864607634; 17.1887649536877; -13.0967066416688]);
coeffs = coeffvalues(f);
disp(coeffs);
0.988785541575274 -7.04537769622258 15.7903911803895 -10.6589722455175 -7.86450736176917 20.2042639088148 -16.817942493974
figure
hold on
plot(f,d_v,t_v);%mlbt1, mlbt2,'*black')
%plot(mlbt1, mlbt2,'oblack', mlbt_dr1, mlbt_dr2, 'ro');
grid on
yfit = feval(f,d_v);
figure
hold on
plot(d_v,yfit);%mlbt1, mlbt2,'*black')
plot(mlbt1, mlbt2,'oblack')
grid on
  2 个评论
Torsten
Torsten 2023-3-28
You seriously want to fit a curve to the cloud of blue data points ? You are kidding, aren't you ?
Dmitry
Dmitry 2023-3-29
No, I want to fit the curve so that it is as close as possible to the black dots, but the fit is carried out by the blue dots.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Least Squares 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by