Plotting using fitnlm- custom function

Hello,
I am trying to fit the model- y = exp(-kx) to my data using fitnlm. I get the alogrithm to converge and the p value also seems to be pretty good but however when I plot my data against the fitted value, it gives me a negative linear fit. Can someone please assist me with this? Following is my code:
clc
clear
close all
ydata = [1,0.997236949247573,0.993216252587368,0.990582467367220,0.987935216987460,0.986050094641751,0.985182938362725,0.981795104204292,0.981910904576958,0.979112844409541,0.978527109966411,0.978116422598238,0.976826460307388,0.974431008412376,0.975117731552599,0.972595707157232,0.971631601728998,0.971754134681468,0.971864548990289,0.967534153659002,0.967923296771795,0.966797609428214,0.968006780761392,0.966978042567018,0.965560161259852,0.966723551050347,0.963551159445711,0.964820923997143,0.965389153732777,0.966883786449734,0.965231611365315,0.965336639610290,0.964132854340959,0.963501338355146,0.964194794075175,0.964038598223673,0.966930914508376,0.964819577481181,0.965562854291775,0.964613560539114,0.964520650937790,0.967340255360587,0.967373918259617,0.965183136790711,0.966190330729704,0.964132854340959,0.965906215861887,0.965662496472906,0.965313748838949,0.964350989926676,0.964707816656400,0.964101884473850,0.962709586969948,0.963155283753112,0.962682656650723,0.964567778996433,0.961581206594445,0.963135086013694,0.961289012630860,0.962367571915798,0.962258504122940,0.961375189652379,0.961258042763753,0.961649878908467,0.961317289466046,0.961408852551409,0.960680387416388,0.961617562525398,0.959607214195296,0.959955961829252,0.959389078609577,0.960471677442399,0.960174097414970,0.958077572063348,0.958412854537692,0.960094652973257,0.959394464673421,0.959191140763278,0.960054257494420,0.958280895973493,0.959473909115135,0.957825773578599,0.959515651109932,0.957715359269781,0.959012054140435,0.957025943097635,0.957673617274982,0.957234653071624,0.957420472274273,0.958621564511682,0.958337449643864,0.958126046637952,0.957841931770134,0.959113042837527,0.956850896022676,0.956118391339772,0.957327562672949,0.955154285911538,0.954839201176612,0.956111658759966,0.956845509958831,0.956069916765169,0.952655152287512,0.953304172980820,0.955477449742231,0.954025905536034,0.955653843333151,0.956178984558027,0.954934803809859,0.954879596655449,0.955485528837998,0.954269624925016,0.953370152262921,0.955511112641261,0.954790726602009,0.954440632452091,0.955226997773444,0.954167289711964,0.954351762398650,0.954468909287277,0.953775453567247,0.954758410218939,0.955360302853605,0.954591442239747,0.956150707722842,0.954353108914611,0.953298786916976,0.954786687054125,0.953833353753580,0.953775453567248,0.953547892369802,0.954183447903497,0.955618833918159,0.953554624949608,0.955130048624235,0.954332911175194,0.954296555244240,0.954953655033316,0.954011093860461,0.954349069366727,0.954502572186307,0.955492261417805,0.953861630588765,0.953868363168572,0.952803269043246,0.953298786916975,0.955042525086756,0.954161903648118,0.954102656945824,0.953401122130029,0.952123278482830,0.953652920614777,0.953857591040882,0.953857591040881,0.953283975241402,0.953911451679330,0.951960350051522,0.952095001647644,0.953413240773679,0.953248965826411,0.953108928166444,0.952727864149418,0.952900218192455,0.952671310479048,0.953259737954100,0.953737751120334,0.953775453567247,0.951864747418275,0.953170867900660,0.954468909287276,0.952828852846509,0.951925340636530,0.951994012950553,0.952962157926670,0.952764220080370,0.952152901833977,0.953938381998556,0.952664577899241,0.951913221992879,0.953438824576943,0.953328410268122,0.953177600480466,0.954062261466988,0.955027713411183,0.954050142823336,0.953209916863535,0.951187449889782,0.952797882979401,0.953613871651902,0.954783994022201,0.954590095723786,0.952536658882924,0.953308212528704,0.953581555268832,0.952811348139014,0.953134511969706,0.953195105187961,0.951463485661831,0.952951385798981,0.952614756808675,0.952006131594203,0.953772760535325,0.952186564733007,0.951670849119859,0.952042487525156,0.951957657019599,0.952353532712198,0.952338721036625,0.951289785102835,0.950599022414727,0.952202722924542,0.951406931991461,0.950111583636766,0.949805924513568,0.950763297361996,0.951872826514042,0.952166366993589,0.951009709782900,0.949644342598222,0.951183410341897,0.950050990418511,0.950993551591366,0.951060877389427,0.951225152336695,0.950057722998317,0.950413203212079,0.951273626911300,0.950426668371692,0.951703165502929,0.951735481885998,0.948926649590891,0.950643457441448,0.950970660820025,0.948946847330311,0.951621028029295,0.952143476222247,0.949486800230759,0.950244888716926,0.950460331270721,0.949906913210660,0.952131357578597,0.948917223979163,0.949387158049629,0.948285707993350,0.948121433046081,0.950864286059089,0.948695048845562,0.949546046933053,0.949477374619030,0.947730943417327,0.949031677835866,0.948428438685240,0.949313099671762,0.948117393498198,0.948459408552347,0.948269549801815,0.948633109111346,0.948769107223429,0.946797807856202,0.948855284244946,0.947415858682401,0.947145208974196,0.947332374692805,0.947386235331255,0.946994399186539,0.947739022513095,0.947231385995713,0.947456254161238,0.948284361477390,0.947600331369089,0.947123664718816,0.947749794640784,0.946943231580013,0.947702666582141,0.947780764507893,0.949415434884815,0.946901489585216,0.946407318227447,0.947448175065471,0.947106160011321,0.947992167513804,0.948147016849345,0.946482723121276,0.946496188280888,0.946004709955042,0.946906875649060,0.947883099720945,0.947219267352064,0.947300058309736,0.946424822934943,0.949380425469823,0.949092271054121,0.947959851130735,0.948831046957645,0.948179333232414,0.946995745702501,0.946910915196944,0.947360651527992,0.946743947217753,0.947374116687604,0.948342261663722,0.944837280616664,0.946342685461309,0.946707591286800,0.946299596950548,0.947378156235488,0.945624992453977,0.946971508415198,0.944889794739152,0.945401470804415,0.945334145006353,0.946913608228866,0.946665849292000,0.945320679846742,0.944038796651659,0.945740792826643,0.945300482107324,0.944743024499378,0.946006056471002,0.945343570618082,0.945463410538632,0.946304983014395,0.944802271201672,0.945055416202380,0.946201301285381,0.945072920909876,0.946520425568190,0.945894295646222,0.945914493385640,0.947102120463437,0.946752026313520,0.945906414289873,0.944501998142320,0.942880792925009,0.946012789050810,0.946334606365541,0.944265011333145,0.944262318301222,0.945962967960244,0.945491687373817,0.947515500863532,0.947799615731349,0.948736790840359,0.948392082754287,0.949177101559678,0.949241734325817,0.949041103447595,0.948204917035677,0.948520001770603,0.948961659005883,0.949546046933053,0.949323871799451,0.947896564880558,0.949240387809856,0.948604832276160,0.949671272917447,0.948727365228630,0.948538852994060,0.948522694802525,0.948642534723073,0.948509229642913,0.948917223979162,0.948439210812929,0.946799154372162,0.948680237169988,0.947578787113710,0.948036602540524,0.946430208998788,0.949655114725912,0.945579210911296,0.947386235331254,0.947069804080367,0.946329220301696,0.947283900118201,0.946979587510966,0.945416282479989,0.947897911396519,0.946843589398883,0.948832393473605,0.949186527171407,0.946758758893326,0.948054107248020,0.948932035654736,0.948510576158873,0.950236809621159,0.948758335095739,0.948452675972541,0.948638495175190,0.947441442485664,0.947962544162658,0.948091809694935,0.947425284294131,0.946388467003990,0.947114239107088,0.948063532859749,0.948781225867079,0.948532120414254,0.947034794665376,0.946430208998788,0.948559050733479,0.947673043230994,0.945497073437662,0.949229615682166,0.947635340784080,0.947090001819786,0.946986320090772,0.944079192130496,0.947363344559913];
xdata = [15.4149999618530,33.1199998855591,50.7449998855591,68.3190000057221,86.0669999122620,103.736999988556,121.440000057220,139.190999984741,156.878999948502,174.553999900818,192.220000028610,209.914000034332,227.637000083923,245.506999969482,263.266999959946,281.039999961853,298.882999897003,316.533999919891,334.249000072479,352.088999986649,369.869999885559,387.470999956131,405.052999973297,422.776999950409,440.328000068665,458.059999942780,475.711999893189,493.420000076294,511.114000082016,528.819000005722,546.431999921799,564.240000009537,581.898000001907,599.667000055313,617.407000064850,635.055999994278,652.782000064850,670.559999942780,688.154000043869,705.759999990463,723.497999906540,741.266000032425,758.967999935150,776.647000074387,794.427000045776,812.029999971390,829.740999937058,847.542999982834,865.196000099182,882.786000013351,900.529000043869,918.333999872208,936.116999864578,953.729000091553,971.394999980927,989.089999914169,1006.84699988365,1024.39499998093,1042.15400004387,1059.85699987412,1077.51799988747,1095.24099993706,1113.09999990463,1130.88100004196,1148.83599996567,1166.59400010109,1184.46900010109,1202.23799991608,1219.85599994659,1237.51099991798,1255.09999990463,1272.86400008202,1290.50799989700,1308.24699997902,1325.86999988556,1343.61100006104,1361.21300005913,1378.83500003815,1396.58999991417,1414.23000001907,1432.12500000000,1449.89199995995,1467.70499992371,1485.23600006104,1502.83800005913,1520.62400007248,1538.24799990654,1556.00099992752,1573.53799986839,1591.32299995422,1609.04299998283,1626.75300002098,1644.49900007248,1662.19099998474,1679.99600005150,1697.83500003815,1715.55900001526,1733.28399991989,1751.10599994659,1768.72399997711,1786.38400006294,1804.05200004578,1821.72900009155,1839.44600009918,1856.94700002670,1874.85500001907,1892.49300003052,1910.12400007248,1927.79500007629,1945.58899998665,1963.34400010109,1980.99399995804,1998.77200007439,2016.40400004387,2034.09899997711,2051.70600008965,2069.27399992943,2087.07699990273,2104.80999994278,2122.48000001907,2140.20799994469,2157.99199986458,2175.67700004578,2193.47199988365,2211.18000006676,2229.06599998474,2246.86199998856,2264.43000006676,2282.13599991798,2299.92499995232,2317.62500000000,2335.34500002861,2353.02099990845,2370.67100000381,2388.32399988174,2405.95899987221,2423.70600008965,2441.50399994850,2459.18300008774,2476.83500003815,2494.65400004387,2512.45499992371,2530.01399993897,2547.66000008583,2565.35400009155,2583.05500006676,2600.72099995613,2618.36999988556,2636.05200004578,2653.72300004959,2671.41199994087,2689.12599992752,2706.69400000572,2724.51499986649,2742.22300004959,2760.06099987030,2777.40000009537,2794.50399994850,2811.64499998093,2829.11899995804,2846.39700007439,2863.47499990463,2880.57599997520,2897.80900001526,2914.52200007439,2932.16199994087,2949.92100000381,2967.61400008202,2985.20700001717,3002.29399991035,3019.60699987412,3036.96499991417,3054.08999991417,3071.38700008392,3088.65899991989,3105.84400010109,3122.50999999046,3139.17100000381,3155.84999990463,3172.64800000191,3189.89400005341,3207.48499989510,3224.94499993324,3242.34899997711,3259.82999992371,3277.17799997330,3294.67300009728,3312.17799997330,3329.65799999237,3346.84299993515,3363.82699990273,3381.47699999809,3399.20899987221,3416.82299995422,3434.50099992752,3452.31099987030,3470.05999994278,3487.75799989700,3505.50399994850,3523.26099991798,3540.93700003624,3558.78500008583,3576.35400009155,3593.64800000191,3611.03299999237,3628.37899994850,3646.08999991417,3663.88999986649,3681.49199986458,3699.16000008583,3716.65899991989,3734.41299986839,3751.91400003433,3769.46600008011,3787.25799989700,3804.68099999428,3822.33800005913,3839.82999992371,3857.31399989128,3874.81900000572,3892.29900002480,3909.99900007248,3927.65499997139,3945.42199993134,3963.16000008583,3980.90899991989,3998.53999996185,4016.25900006294,4033.87299990654,4051.54099988937,4069.23799991608,4086.96000003815,4104.65700006485,4122.42000007629,4140.07500004768,4157.88299989700,4175.66499996185,4193.32200002670,4210.91199994087,4228.58500003815,4246.30999994278,4263.87500000000,4281.60100007057,4299.31200003624,4316.93000006676,4334.56599998474,4352.44600009918,4370.23000001907,4387.89899992943,4405.48900008202,4423.34400010109,4441.11599993706,4458.86800003052,4476.49499988556,4494.19600009918,4511.81099987030,4529.32599997520,4547.14499998093,4564.84299993515,4582.60400009155,4600.30200004578,4617.95799994469,4635.66499996185,4653.36400008202,4671.13400006294,4688.95899987221,4706.64299988747,4724.39800000191,4742.26499986649,4759.83500003815,4777.21099996567,4794.63899993897,4812.30900001526,4829.85500001907,4847.52900004387,4865.38599991798,4883.18099999428,4900.98900008202,4918.61599993706,4936.21199989319,4953.85500001907,4971.70700001717,4989.39299988747,5007.17899990082,5025.05399990082,5042.70700001717,5060.30200004578,5077.90899991989,5095.51399993897,5113.33800005913,5130.98399996758,5148.58599996567,5166.21099996567,5183.85800004005,5201.57299995422,5219.18899989128,5236.74199986458,5254.45499992371,5272.16599988937,5289.92899990082,5307.54500007629,5325.17300009728,5342.89800000191,5360.52600002289,5378.21199989319,5396.01999998093,5413.79099988937,5431.38599991798,5449.13499999046,5466.95300006867,5484.54600000382,5502.11699986458,5519.89599990845,5537.55800008774,5555.19400000572,5572.92700004578,5590.49699997902,5608.04600000382,5625.61800003052,5643.32899999619,5661.11699986458,5678.78900003433,5696.46399998665,5714.10199999809,5731.70799994469,5749.43599987030,5767.16400003433,5784.84699988365,5802.51799988747,5820.29699993134,5838.02699995041,5855.67100000382,5873.48900008202,5891.12700009346,5908.84899997711,5926.50200009346,5944.40499997139,5962.07999992371,5979.74699997902,5997.46499991417,6015.32200002670,6033.06500005722,6050.82299995422,6068.71300005913,6086.49199986458,6104.14599990845,6121.81399989128,6139.56299996376,6157.19499993324,6174.74900007248,6192.45099997520,6210.12599992752,6227.84100008011,6245.52900004387,6263.02799987793,6280.55599999428,6298.29399991036,6315.95000004768,6333.73000001907,6351.39599990845,6369.09800004959,6386.83899998665,6404.72600007057,6422.49099993706,6440.24000000954,6458.05500006676,6475.68799996376,6493.48000001907,6511.21799993515,6528.80099987984,6546.67599987984,6564.33399987221,6582.04600000382,6599.74199986458,6617.59699988365,6635.39800000191,6653.13599991798,6670.80500006676,6688.45899987221,6706.25600004196,6723.86699986458,6741.45600008965,6759.21000003815,6776.74300003052,6794.45000004768,6812.08699989319,6829.79999995232,6847.62800002098,6865.36899995804,6883.05599999428,6900.68400001526,6918.27699995041,6935.81099987030,6953.17000007629,6970.88299989700,6988.48099994659,7005.82899999619,7023.33500003815,7041.16000008583,7058.77600002289,7076.43400001526,7094.17899990082,7111.57200002670,7129.17000007629,7146.50000000000,7164.36899995804,7181.72900009155,7199.10199999809];
% f0 = fit(transpose(x),transpose(y),'exp2');
% plot(f0,x,y)
%
modelfun = @(k,x)exp(-k(1)*x);
beta0 = 10;
mdl = fitnlm(xdata,ydata,modelfun,rand(1,1)/10000) ;
parms = mdl.Coefficients.Estimate;
plot(xdata, ydata)
hold on
plot(xdata, modelfun(parms,xdata))
This is the fit I am getting:

답변 (1개)

Star Strider
Star Strider 2021년 3월 11일

0 개 추천

I added a scaling term and an offset term:
modelfun = @(k,x)k(1).*exp(-k(2).*x)+k(3);
beta0 = 10;
mdl = fitnlm(xdata,ydata,modelfun, [1 1E-3 min(ydata)]) ;
parms = mdl.Coefficients.Estimate;
plot(xdata, ydata)
hold on
plot(xdata, modelfun(parms,xdata))
text(2000, 0.98, sprintf('$y(x) = %.4f e^{-%.6f}+%.4f$', parms), 'Interpreter','latex', 'FontSize',14)
producing:
.

댓글 수: 4

sparsh agarwal
sparsh agarwal 2021년 3월 11일
Thanks for the reply. To give you more background of what I am doing. I am trying to fit a first order kinetcis model- dS/dt = -k[S] with initial condition being S[0] = 1. When I solved this equation I got S = exp(-kt).
What I am trying to get at it is that I can't just randomly add scaling and offset terms to my model as it doesn't make any sense scientifically.
Do you have any other suggestions to fit this data?
Thank you once again for all the help.
Star Strider
Star Strider 2021년 3월 11일
편집: Star Strider 2021년 3월 12일
My pleasure!
The kinetics model apparently assumes that whatever you are doing will have a lower limit of zero, so no offset or scaling of any sort is necessary. The reality may very well be different for a first-order model, or it could be that a higher-order moodel (for example with a second and much slower disappearance rate) may actually be the situation. I have no way of knowing that, since I have no idea what you are doing.
All that aside, use the first-order exponential parameter () as the initial parameter estimate for your first-order model and see if the result is appropriate.
EDIT — (12 Mar 2021 at 2:02)
Adding a second exponential provides an excellent fit:
So I strongly suspect the kinetics are definitely not first-order.
sparsh agarwal
sparsh agarwal 2021년 3월 12일
Yeah I just tried that as well and the two term exponetial is a great fit to the my data. I will have to think of a way to justify it. Do you know of any systems which use two term exponential model.
Thank you once again for all the help.
Star Strider
Star Strider 2021년 3월 12일
I am not certain what you are modeling.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

질문:

2021년 3월 11일

댓글:

2021년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by