fmincon returning results with large errors

I am using fmincon to do some fitting. The fitted parameters are A and B. I do 500 simulations for varying values of A (B is always 1). I do the fittings 500 times. In most cases, fitted A and B are accurate. In some cases where Y (and A) is very small (~0), B is ~2.5 times bigger.
Delete.png
How can I improve my fits? Why is B in the plot above doing these weird spikes? Example code where fitted B is not accurate.
global X Y deltaX
X = linspace(0,25,1000);
Y = [0,6.88363868053220e-06,2.61850086044514e-05,7.31760916473311e-05,0.000169703525107875,0.000335095365782447,0.000570492105509620,0.000848334324266810,0.00112111070650578,0.00134616301294618,0.00150637795687461,0.00161103712983892,0.00168157211368303,0.00173645598272875,0.00178586388577830,0.00183385079765603,0.00188133039133770,0.00192820866515512,0.00197531256730628,0.00202495694380148,0.00207931456044459,0.00213768017818540,0.00219701992890083,0.00225523654851226,0.00231059493361869,0.00236274290280846,0.00241225288555139,0.00245952660700232,0.00250526005739466,0.00254982927731439,0.00259379301196966,0.00263704176625230,0.00268006945437819,0.00272282025528020,0.00276533860178240,0.00280774203039247,0.00284990694888583,0.00289193218896916,0.00293375224159887,0.00297539229362766,0.00301687646156951,0.00305819262887141,0.00309933318385231,0.00314029812651220,0.00318108745685109,0.00322170117486898,0.00326213928056586,0.00330240177394174,0.00334248865499662,0.00338239992373049,0.00342213558014336,0.00346169562423523,0.00350108005600609,0.00354028887545594,0.00357932208258480,0.00361825212432555,0.00365701700530112,0.00369561562408123,0.00373404798066589,0.00377231407505510,0.00381041390724885,0.00384834747724715,0.00388611478505000,0.00392371583065739,0.00396115061406933,0.00399841913528582,0.00403552139430685,0.00407245739113243,0.00410922712576256,0.00414584722157688,0.00418236613869793,0.00421872887837678,0.00425493544061343,0.00429098582540786,0.00432688003276010,0.00436261806267012,0.00439819991513794,0.00443362559016356,0.00446889508774697,0.00450400840788817,0.00453896555058717,0.00457376651584397,0.00460841130365855,0.00464289991403093,0.00467726557986839,0.00471152168503671,0.00474563118934736,0.00477959409280035,0.00481341039539566,0.00484708009713329,0.00488060319801326,0.00491397969803555,0.00494720959720017,0.00498029289550712,0.00501322959295640,0.00504601968954801,0.00507866318528194,0.00511116008015821,0.00514351037417680,0.00517574009128327,0.00520784217947271,0.00523980694091487,0.00527163497746057,0.00530332689096067,0.00533488328326600,0.00536630475622739,0.00539759191169569,0.00542874535152174,0.00545976567755637,0.00549065349165042,0.00552140939565473,0.00555203399142014,0.00558252788079749,0.00561289166563762,0.00564312594779136,0.00567323132910956,0.00570320841144305,0.00573305779664267,0.00576278008655926,0.00579237588304366,0.00582184578794671,0.00585119040311925,0.00588041033041211,0.00590950617167613,0.00593847852876216,0.00596732800352102,0.00599605519780357,0.00602465518980816,0.00605312684019562,0.00608147672119711,0.00610970539106717,0.00613781340806028,0.00616580133043096,0.00619366971643374,0.00622141912432310,0.00624905011235357,0.00627656323877966,0.00630395906185587,0.00633123813983672,0.00635840103097672,0.00638544829353037,0.00641238048575220,0.00643919816589670,0.00646590189221839,0.00649249222297179,0.00651896971641139,0.00654533493079172,0.00657158842436727,0.00659773075539258,0.00662376248212213,0.00664968416281045,0.00667549635571204,0.00670119961908142,0.00672679451117310,0.00675228159024158,0.00677765748679504,0.00680291309244907,0.00682806075934081,0.00685310098461930,0.00687803426543361,0.00690286109893277,0.00692758198226583,0.00695219741258184,0.00697670788702984,0.00700111390275888,0.00702541595691801,0.00704961454665628,0.00707371016912273,0.00709770332146641,0.00712159450083636,0.00714538420438164,0.00716907292925129,0.00719266117259436,0.00721614943155989,0.00723953820329693,0.00726282798495453,0.00728601927368173,0.00730911256662759,0.00733210836094114,0.00735500715377145,0.00737780944226754,0.00740051572357848,0.00742312649485330,0.00744564204747113,0.00746804590573406,0.00749035393145055,0.00751256655973064,0.00753468422568437,0.00755670736442181,0.00757863641105298,0.00760047180068795,0.00762221396843676,0.00764386334940945,0.00766542037871608,0.00768688549146670,0.00770825912277135,0.00772954170774008,0.00775073368148294,0.00777183547910998,0.00779284753573124,0.00781377028645677,0.00783460416639663,0.00785534961066086,0.00787600705435950,0.00789657693260261,0.00791705968050023,0.00793745573316242,0.00795776552569921,0.00797798949322067,0.00799812807083683,0.00801818169365774,0.00803815079679346,0.00805802434759900,0.00807780975707944,0.00809751037005942,0.00811712656893272,0.00813665873609315,0.00815610725393450,0.00817547250485058,0.00819475487123519,0.00821395473548212,0.00823307247998517,0.00825210848713814,0.00827106313933482,0.00828993681896903,0.00830872990843455,0.00832744279012519,0.00834607584643474,0.00836462945975700,0.00838310401248577,0.00840149988701485,0.00841981746573804,0.00843805713104914,0.00845621926534194,0.00847430425101024,0.00849231247044785,0.00851024430604856,0.00852810014020616,0.00854588035531447,0.00856358533376727,0.00858120595119917,0.00859874351318242,0.00861620554506136,0.00863359239094699,0.00865090439495034,0.00866814190118241,0.00868530525375423,0.00870239479677679,0.00871941087436111,0.00873635383061821,0.00875322400965910,0.00877002175559479,0.00878674741253629,0.00880340132459462,0.00881998383588080,0.00883649529050582,0.00885293603258071,0.00886930640621648,0.00888560675552415,0.00890183742461471,0.00891799875759919,0.00893409109858861,0.00895011479169396,0.00896607018102628,0.00898195761069656,0.00899777742481582,0.00901352996749508,0.00902921558284534,0.00904483461497762,0.00906038740800294,0.00907587430603230,0.00909129565317672,0.00910665179354722,0.00912194307125479,0.00913716983041047,0.00915233241512525,0.00916742156383574,0.00918243591837096,0.00919738545908871,0.00921227048559165,0.00922709129748247,0.00924184819436382,0.00925654147583840,0.00927117144150885,0.00928573839097786,0.00930024262384810,0.00931468443972224,0.00932906413820296,0.00934338201889291,0.00935763838139478,0.00937183352531123,0.00938596775024495,0.00940004135579859,0.00941405464157483,0.00942800790717634,0.00944190145220580,0.00945573557626587,0.00946951057895923,0.00948322675988854,0.00949688441865649,0.00951048385486574,0.00952402536811896,0.00953750925801882,0.00955093582416800,0.00956430536616917,0.00957761818362500,0.00959087457613815,0.00960407484331131,0.00961721928474714,0.00963030820004832,0.00964334188881751,0.00965632065065739,0.00966923686071867,0.00968208648977616,0.00969488047174929,0.00970761906305416,0.00972030252010687,0.00973293109932354,0.00974550505712027,0.00975802464991317,0.00977049013411835,0.00978290176615192,0.00979525980242998,0.00980756449936865,0.00981981611338402,0.00983201490089222,0.00984416111830934,0.00985625502205149,0.00986829686853478,0.00988028691417533,0.00989222541538923,0.00990411262859259,0.00991594881020153,0.00992773421663215,0.00993946910430056,0.00995115372962287,0.00996278834901518,0.00997437321889360,0.00998590859567424,0.00999739473577321,0.0100088318956066,0.0100202203315906,0.0100315603001412,0.0100428520576745,0.0100540958606068,0.0100652919653540,0.0100764406283322,0.0100875421059577,0.0100985910035385,0.0101095815345089,0.0101205242524124,0.0101314193763292,0.0101422671253391,0.0101530677185221,0.0101638213749583,0.0101745283137276,0.0101851887539101,0.0101958029145856,0.0102063710148343,0.0102168932737361,0.0102273699103710,0.0102378011438190,0.0102481871931601,0.0102585282774743,0.0102688246158415,0.0102790764273417,0.0102892839310551,0.0102994473460614,0.0103095668914408,0.0103196427862733,0.0103296752496387,0.0103396645006172,0.0103496107582886,0.0103595142417331,0.0103693751700305,0.0103791937622609,0.0103889702375043,0.0103987048148407,0.0104083977133500,0.0104180491521122,0.0104276593502074,0.0104372285267155,0.0104467569007166,0.0104562446912905,0.0104656883935702,0.0104750812689372,0.0104844330534946,0.0104937439351362,0.0105030141017557,0.0105122437412467,0.0105214330415030,0.0105305821904182,0.0105396913758860,0.0105487607858002,0.0105577906080543,0.0105667810305421,0.0105757322411573,0.0105846444277936,0.0105935177783445,0.0106023524807040,0.0106111487227655,0.0106199066924229,0.0106286265775697,0.0106373085660998,0.0106459528459067,0.0106545596048842,0.0106631290309259,0.0106716613119256,0.0106801566357769,0.0106886151903735,0.0106970371636091,0.0107054227433774,0.0107137721175721,0.0107220854740868,0.0107303630008153,0.0107386048856512,0.0107468113164883,0.0107549824812202,0.0107631185677406,0.0107712197639432,0.0107792828078109,0.0107872969152359,0.0107952755834700,0.0108032189774655,0.0108111272621747,0.0108190006025498,0.0108268391635431,0.0108346431101070,0.0108424126071937,0.0108501478197555,0.0108578489127446,0.0108655160511134,0.0108731493998141,0.0108807491237991,0.0108883153880206,0.0108958483574308,0.0109033481969821,0.0109108150716268,0.0109182491463172,0.0109256505860054,0.0109330195556439,0.0109403562201849,0.0109476607445806,0.0109549332937834,0.0109621740327456,0.0109693831264194,0.0109765607397571,0.0109837070377110,0.0109908221852334,0.0109979063472765,0.0110049596887927,0.0110119823747342,0.0110189745700534,0.0110259364397025,0.0110328681486337,0.0110397698617994,0.0110466417441519,0.0110534839606434,0.0110602966762262,0.0110670800558526,0.0110738342644750,0.0110805594670455,0.0110872558285164,0.0110939235138401,0.0111005626879688,0.0111071735158549,0.0111137561624505,0.0111203107927080,0.0111268187015467,0.0111332957028482,0.0111397438459460,0.0111461632686234,0.0111525541086636,0.0111589165038502,0.0111652505919664,0.0111715565107955,0.0111778343981210,0.0111840843917262,0.0111903066293944,0.0111965012489091,0.0112026683880535,0.0112088081846109,0.0112149207763649,0.0112210063010987,0.0112270648965956,0.0112330967006391,0.0112391018510124,0.0112450804854990,0.0112510327418822,0.0112569587579453,0.0112628586714717,0.0112687326202447,0.0112745807420478,0.0112804031746642,0.0112862000558773,0.0112919715234705,0.0112977177152271,0.0113034387689305,0.0113091348223641,0.0113148060133111,0.0113204524795549,0.0113260743588789,0.0113316717890665,0.0113372449079010,0.0113427938531657,0.0113483187626440,0.0113538197741193,0.0113592970253749,0.0113647506541942,0.0113701807983605,0.0113755875956572,0.0113809711838677,0.0113863317007752,0.0113916692841631,0.0113969840718149,0.0114022642975316,0.0114075134092839,0.0114127388836678,0.0114179408328051,0.0114231193688178,0.0114282746038276,0.0114334066499567,0.0114385156193267,0.0114436016240597,0.0114486647762776,0.0114537051881022,0.0114587229716555,0.0114637182390593,0.0114686911024356,0.0114736416739063,0.0114785700655932,0.0114834763896183,0.0114883607581035,0.0114932232831706,0.0114980640769417,0.0115028832515385,0.0115076809190830,0.0115124571916971,0.0115172121815026,0.0115219460006216,0.0115266587611759,0.0115313505752874,0.0115360215550779,0.0115406718126695,0.0115453014601840,0.0115499106097433,0.0115544993734693,0.0115590678634840,0.0115636161919091,0.0115681444708667,0.0115726528124786,0.0115771413288668,0.0115816101321530,0.0115860593344593,0.0115904890479076,0.0115948993846197,0.0115992904567175,0.0116036623763230,0.0116080152555580,0.0116123492065445,0.0116166643414043,0.0116209607722594,0.0116252336029074,0.0116294764108911,0.0116336998257152,0.0116379039384046,0.0116420888399847,0.0116462546214804,0.0116504013739170,0.0116545291883194,0.0116586381557130,0.0116627283671227,0.0116667999135738,0.0116708528860912,0.0116748873757003,0.0116789034734260,0.0116829012702935,0.0116868808573279,0.0116908423255544,0.0116947857659981,0.0116987112696841,0.0117026189276375,0.0117065088308834,0.0117103810704470,0.0117142357373534,0.0117180729226277,0.0117218927172951,0.0117256952123806,0.0117294804989094,0.0117332486679066,0.0117369998103974,0.0117407340174068,0.0117444513799600,0.0117481519890821,0.0117518359357983,0.0117555033111336,0.0117591542061132,0.0117627887117621,0.0117664069191056,0.0117700089191688,0.0117735948029767,0.0117771646615546,0.0117807185859274,0.0117842566671204,0.0117877789961587,0.0117912856640673,0.0117947767618715,0.0117982523805963,0.0118017126112669,0.0118051572945299,0.0118085742093490,0.0118119752056256,0.0118153603577533,0.0118187297401255,0.0118220834271356,0.0118254214931770,0.0118287440126433,0.0118320510599279,0.0118353427094242,0.0118386190355256,0.0118418801126256,0.0118451260151176,0.0118483568173952,0.0118515725938516,0.0118547734188805,0.0118579593668751,0.0118611305122290,0.0118642869293356,0.0118674286925884,0.0118705558763807,0.0118736685551061,0.0118767668031579,0.0118798506949297,0.0118829203048149,0.0118859757072068,0.0118890169764990,0.0118920441870849,0.0118950574133580,0.0118980567297116,0.0119010422105393,0.0119040139302344,0.0119069719631905,0.0119099163838009,0.0119128472664591,0.0119157646855586,0.0119186687154928,0.0119215594306551,0.0119244369054390,0.0119273012142379,0.0119301524314453,0.0119329906314546,0.0119358158886592,0.0119386282774527,0.0119414278722284,0.0119442147473798,0.0119469889773003,0.0119497506363834,0.0119524877515692,0.0119552072296836,0.0119579135986478,0.0119606069216232,0.0119632872617714,0.0119659546822539,0.0119686092462322,0.0119712510168677,0.0119738800573222,0.0119764964307569,0.0119791002003336,0.0119816914292136,0.0119842701805585,0.0119868365175297,0.0119893905032890,0.0119919322009976,0.0119944616738172,0.0119969789849092,0.0119994841974353,0.0120019773745568,0.0120044585794353,0.0120069278752324,0.0120093853251095,0.0120118309922281,0.0120142649397498,0.0120166872308361,0.0120190979286485,0.0120214970963485,0.0120238847970977,0.0120262610940574,0.0120286260503894,0.0120309797292550,0.0120333221938157,0.0120356535072332,0.0120379737326689,0.0120402829332844,0.0120425811722410,0.0120448685127005,0.0120471450178242,0.0120494107507737,0.0120516657747105,0.0120539101527961,0.0120561439481921,0.0120583672240599,0.0120605800435611,0.0120627824698572,0.0120649745661096,0.0120671563954800,0.0120693280211298,0.0120714895062205,0.0120736409139137,0.0120757823073708,0.0120779137497534,0.0120800353042231,0.0120821470339412,0.0120842490020694,0.0120863412717691,0.0120884239062019,0.0120904969685292,0.0120925605219127,0.0120946146295137,0.0120966593544938,0.0120986947600146,0.0121007209092376,0.0121027378653242,0.0121047456914359,0.0121067313293323,0.0121087002409876,0.0121106592907407,0.0121126085281479,0.0121145480027655,0.0121164777641500,0.0121183978618576,0.0121203083454446,0.0121222092644675,0.0121241006684826,0.0121259826070461,0.0121278551297146,0.0121297182860442,0.0121315721255913,0.0121334166979123,0.0121352520525636,0.0121370782391014,0.0121388953070821,0.0121407033060621,0.0121425022855976,0.0121442922952451,0.0121460733845608,0.0121478456031012,0.0121496090004225,0.0121513636260811,0.0121531095296334,0.0121548467606357,0.0121565753686442,0.0121582954032155,0.0121600069139057,0.0121617099502713,0.0121634045618686,0.0121650907982539,0.0121667687089836,0.0121684383436141,0.0121700997517016,0.0121717529828024,0.0121733980864731,0.0121750351122698,0.0121766641097490,0.0121782851284669,0.0121798982179800,0.0121815034278445,0.0121831008076168,0.0121846904068532,0.0121862722751101,0.0121878464619439,0.0121894130169108,0.0121909719895672,0.0121925234294695,0.0121940673861740,0.0121956039092370,0.0121971330482149,0.0121986548526640,0.0122001693721407,0.0122016766562013,0.0122031767544021,0.0122046697162995,0.0122061555914499,0.0122076344294095,0.0122091062797347,0.0122105711919819,0.0122120292157074,0.0122134804004675,0.0122149247958186,0.0122163624513171,0.0122177827506136,0.0122191880594766,0.0122205859348006,0.0122219764136908,0.0122233595332526,0.0122247353305910,0.0122261038428114,0.0122274651070190,0.0122288191603191,0.0122301660398168,0.0122315057826174,0.0122328384258261,0.0122341640065482,0.0122354825618890,0.0122367941289536,0.0122380987448472,0.0122393964466752,0.0122406872715428,0.0122419712565551,0.0122432484388175,0.0122445188554351,0.0122457825435133,0.0122470395401572,0.0122482898824720,0.0122495336075631,0.0122507707525356,0.0122520013544948,0.0122532254505460,0.0122544430777942,0.0122556542733449,0.0122568590743032,0.0122580575177744,0.0122592496408637,0.0122604354806763,0.0122616150743175,0.0122627884588924,0.0122639556715065,0.0122651167492648,0.0122662717292727,0.0122674206486353,0.0122685635444579,0.0122697004538457,0.0122708314139040,0.0122719564617380,0.0122730756344529,0.0122741889691540,0.0122752965029465,0.0122763982729356,0.0122774943162267,0.0122785846699248,0.0122796693711353,0.0122807484569634,0.0122818219645144,0.0122828899308934,0.0122839523932057,0.0122850093885565,0.0122860609540511,0.0122871071267947,0.0122881479438926,0.0122891834424499,0.0122902136595719,0.0122912386323639,0.0122922583979311,0.0122932729933788,0.0122942824558121,0.0122952868223362,0.0122962789960220,0.0122972587846239,0.0122982329450698,0.0122992015049422,0.0123001644918234,0.0123011219332958,0.0123020738569418,0.0123030202903438,0.0123039612610843,0.0123048967967457,0.0123058269249102,0.0123067516731604,0.0123076710690786,0.0123085851402473,0.0123094939142488,0.0123103974186656,0.0123112956810800,0.0123121887290744,0.0123130765902313,0.0123139592921331,0.0123148368623621,0.0123157093285007,0.0123165767181314,0.0123174390588366,0.0123182963781986,0.0123191487037999,0.0123199960632228,0.0123208384840498,0.0123216759938632,0.0123225086202456,0.0123233363907791,0.0123241593330464,0.0123249774746297,0.0123257908431115,0.0123265994660742,0.0123274033711001,0.0123282025857718,0.0123289971376715,0.0123297870543816,0.0123305723634847,0.0123313530925630,0.0123321292691991,0.0123329009209752,0.0123336680754738,0.0123344307602773,0.0123351890029680,0.0123359428311285,0.0123366922723411,0.0123374373541881,0.0123381781042521,0.0123389145501153,0.0123396467193603,0.0123403746395693,0.0123410983383249,0.0123418178432093,0.0123425331818051,0.0123432443816946,0.0123439514704602,0.0123446544756843,0.0123453534249493,0.0123460483458376,0.0123467392659316,0.0123474262128138,0.0123481092140665,0.0123487882972720,0.0123494634900130,0.0123501304393226,0.0123507875795575,0.0123514404514590,0.0123520890757864,0.0123527334732991,0.0123533736647565,0.0123540096709180,0.0123546415125428,0.0123552692103903,0.0123558927852200,0.0123565122577912,0.0123571276488632,0.0123577389791954,0.0123583462695472,0.0123589495406780,0.0123595488133470,0.0123601441083137,0.0123607354463374,0.0123613228481776,0.0123619063345934,0.0123624859263444,0.0123630616441899,0.0123636335088892,0.0123642015412017,0.0123647657618868,0.0123653261917038,0.0123658828514122,0.0123664357617712,0.0123669849435402,0.0123675304174786,0.0123680722043458,0.0123686103249010,0.0123691447999038,0.0123696756501134,0.0123702028962892,0.0123707265591906,0.0123712466595769,0.0123717632182075,0.0123722762558418,0.0123727857932391,0.0123732918511588,0.0123737944503603,0.0123742936116028,0.0123747893556459,0.0123752817032487,0.0123757706751708,0.0123762562921715,0.0123767385750101,0.0123772175444460,0.0123776932212385,0.0123781656261471,0.0123786347799311,0.0123791007033498,0.0123795634171627,0.0123800229421290,0.0123804792990081,0.0123809325085595,0.0123813825915424,0.0123818295687163,0.0123822734608405,0.0123827142886743,0.0123831520729771,0.0123835868345084,0.0123840185940274,0.0123844473722935,0.0123848731900660,0.0123852954979348,0.0123857136347460,0.0123861284702768,0.0123865399959290,0.0123869482031046,0.0123873530832056,0.0123877546276338,0.0123881528277914,0.0123885476750801,0.0123889391609021,0.0123893272766592,0.0123897120137533,0.0123900933635866];
deltaX = X(2);
A = 0.00111978493959457;
B = 1;
opts = optimset('TolX',1e-4,'TolFun',1e-4,'Diagnostics','on','MaxFunEvals',1000,'MaxIter',800,'Display','final','UseParallel','never');
low_bound = [0 0.0001];
up_bound = [5 5];
%% I do the fitting 3 times with varying starting paramaters A and B. Then I take the results with the smallest fitting error.
[fitted_params1,error1,exitflag1,output1] = fmincon(@fitting_function,[0 0.5],[],[],[],[],low_bound,up_bound,[],opts);
fitted_params1 % returns [0.0011 2.4454]
error1 % returns 1.1038e-06
% I GIVE THIS SOLVER THE ORIGINAL VALUES OF A AND B!!!
[fitted_params2,error2,exitflag2,output2] = fmincon(@fitting_function,[A B],[],[],[],[],low_bound,up_bound,[],opts);
fitted_params2 % returns [0.0011 2.2865]
error2 % returns 9.9851e-07
[fitted_params3,error3,exitflag3,output3] = fmincon(@fitting_function,[0.5 1],[],[],[],[],low_bound,up_bound,[],opts);
fitted_params3 % returns [0.0011 2.4455]
error3 % returns 1.1039e-06
%% ...
% my code takes the the fitted_params of the fitting with the smallest
% error (in this case fitted_params2)
function Error = fitting_function(initial)
A_fit=initial(1);
B_fit=initial(2);
global X Y deltaX
Y_fit = A_fit.*conv(helper(X)./0.6,exp(-A_fit./B_fit.*X)).*deltaX;
%Objective function
Error=sum((Y_fit(2:length(X))-Y(2:end)).^2);
end
%Helper function
function y = helper(x)
y = 0.809/(0.0563*sqrt(2*pi))*exp(-(x-0.17046).^2/(2*0.0563^2)) ...
+ 0.330/(0.132*sqrt(2*pi))*exp(-(x-0.365).^2/(2*0.132^2)) ...
+ 1.050*exp(-0.1685*x)./(1+exp(-38.078*(x-0.483)));
end
Results of Code:
A = 0.0011 AND A_fitted = 0.0011
B = 1 AND B_fitted = 2.2865 <= error is big in this case.
Delete2.png

Respuestas (2)

Matt J
Matt J el 5 de Mzo. de 2019
Editada: Matt J el 6 de Mzo. de 2019

0 votos

Notice that your actual fit is fine, despite the discrepancy in B. This occurs because as Y and A become small, the influence of B on the shape of the curve diminishes and B_fitted = 2.2865 gives approximately the same curve as B=1. You have more than one solution, in other words.

2 comentarios

Tara
Tara el 6 de Mzo. de 2019
Thanks Matt for your answer.
I realise I might have more than 1 solution for B. But in the second fitting i do, I start with the values of A and B that I used to generate the Y curves. Still, the fitting converges to another solution. Can I change the options for fmincon so that it converges to the original values of A and B?
If you look at the plot for B, most of my 500 fitting trials give B=1 except for that small chunck. I wonder why the fitting fails only for that small section.
Matt J
Matt J el 6 de Mzo. de 2019
Editada: Matt J el 6 de Mzo. de 2019
That "small chunk" is where the value of B has diminished influence over the shape of the curve (because A is small and everything is close to zero). When the curve is insensitive to the value of the parameter there is nothing you can do to ensure a certain result for that parameter.

Iniciar sesión para comentar.

Matt J
Matt J el 6 de Mzo. de 2019
Editada: Matt J el 6 de Mzo. de 2019
One thing I think would help is to pre-normalize Y prior to the fit so that it is never uniformly close to zero, e.g.,
Y=Y./sum(Y); %normalize
Similarly, reparametrize your model as
Y_fit = C_fit.*conv(helper(X)./0.6,exp(-D_fit.*X)).*deltaX;
After fitting the normalized Y, retrieve the original parameters from
A_fit=C_fit*sum(Y);
B_fit=A_fit/D_fit;

1 comentario

Matt J
Matt J el 6 de Mzo. de 2019
Editada: Matt J el 6 de Mzo. de 2019
I also recommend that you normalize your objective
%Objective function
Error=sum((Y_fit(2:length(X))-Y(2:end)).^2)/(numel(Y)-1);
You might also consider switching from fmincon to lsqcurvefit, since it appears that you don't have very complicated constraints.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Mzo. de 2019

Editada:

el 6 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by