Newest questions tagged fitting - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-20T19:07:23Z https://mathematica.stackexchange.com/feeds/tag?tagnames=fitting&sort=newest https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/206547 0 What are the differences between Nonlinear ModelFit and GeneralizedLinearModelFit, and how to get MLE out of each? Q.P. https://mathematica.stackexchange.com/users/27119 2019-09-20T11:44:27Z 2019-09-20T11:44:27Z <p>I would like to know the practical differences and implications between <code>NonlinearModelFit</code> and <code>GeneralizedLinearModelFit</code>. As I understand it if one wants to get errors out of a fit, the best way seems to be to use maximum likelihood estimation (MLE), whereby for a given parameter being scanned the ci-squares for each parameter value are plotted -- giving a parabolic function, one takes the minima of this and adds 1, the corresponding values for the parabolic curves at this point are give the error in this parameter.</p> <p>I remember reading in a post on here that <code>GeneralizedLinearModelFit</code> is the best way to go to get our MLE, but I can't figure out how, and, I don't know why (assuming this to be the case).</p> <p>So:</p> <ol> <li>What are te differences between <code>NonlinearModelFit</code> and <code>GeneralizedLinearModelFit</code>.</li> <li>When should one use them, is it valid to use <code>GeneralizedLinearModelFit</code> in all cases?</li> <li>How can one get MLE estimates for each fit parameter value for both <code>GeneralizedLinearModelFit</code> and <code>NonlinearModelFit</code> (if possible) in Mathematica?</li> </ol> https://mathematica.stackexchange.com/q/206515 0 Better fit for points with exaggerated deviations LCarvalho https://mathematica.stackexchange.com/users/37895 2019-09-19T16:55:09Z 2019-09-19T19:27:20Z <p>I used the <a href="https://reference.wolfram.com/language/ref/InterpolatingPolynomial.html" rel="nofollow noreferrer"><code>InterpolatingPolynomial</code></a> function to get a polynomial that meets my points.</p> <p>But I noticed that there is a deviation in the final intervals.</p> <pre><code>ClearAll["Global*"] dados16={{10,0.37},{15,0.47},{20,0.54},{25,0.61},{30,0.70},{40,0.80},{50,0.90},{60,1.01},{70,1.10},{80,1.20},{90,1.31},{100,1.42},{110,1.53}}; B16[l_]=InterpolatingPolynomial[dados16,l]//Expand; Plot[B16[l],{l,0,110},Epilog-&gt;{{Red,PointSize[.02],Point[dados16]}}] </code></pre> <p><a href="https://i.stack.imgur.com/Cgtcl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Cgtcl.png" alt="enter image description here"></a></p> <p>What would be the best function to have a better result?</p> https://mathematica.stackexchange.com/q/206472 0 How do I set a multi "pars" constraint for a NonlinearModelFit? unseenmisfit https://mathematica.stackexchange.com/users/67441 2019-09-19T02:19:46Z 2019-09-19T03:30:03Z <pre><code>data = Table[{x, (((x - 1.6)^2)/(16.456*(1 + Sqrt[1 - (1 + 0.65)*((x - 1.6)^2)/(16.456^2)]))) + 0.00032*(x - 1.6)^4 + .6554}, {x, -0.65, 4.073, .001}]; fit = NonlinearModelFit[ data, {(((x - g)^2)/(c*(1 + Sqrt[1 - (1 + k)*((x - g)^2)/(c^2)]))) + a*(x - g)^4 + p,((1 + k)*((x - g)^2)/(c^2)) &lt; 1}, {a, c, k, g, p}, x] Show[ListPlot[data], Plot[fit[x], {x, -0.65, 4.073}, PlotStyle -&gt; Red], Frame -&gt; True] </code></pre> <p>Is my input and I get no answer because the square root making imaginary values so i tried to add </p> <pre><code>((1 + k)*((x - g)^2)/(c^2)) &lt; 1 </code></pre> <p>but it doesn't seem to be fixing the problem.</p> https://mathematica.stackexchange.com/q/206471 0 How can I fit two out of phase data sets to an envelope? Stef https://mathematica.stackexchange.com/users/67483 2019-09-19T00:21:08Z 2019-09-19T00:21:08Z <p>My situation is similar to having a Cos and Sin functions that would combine to the profile of the envelope but the envelope is offset horizontally by some dt.</p> <p>It would be something like a<em>Cos(t+dt)+ b</em>Sin(t+dt)=Envelope(t), I want to find the coefficients and the time shift {a,b,dt}.</p> <p>I have 2 data sets(lists) for the two input data sets and the data set for the envelope. How can I fit this? Using a NonLinearModelFit cannot fit this many functions at once. </p> https://mathematica.stackexchange.com/q/206352 0 NonlinearModelFit failing because varible range [on hold] unseenmisfit https://mathematica.stackexchange.com/users/67441 2019-09-17T00:25:09Z 2019-09-18T18:25:19Z <p>I'm trying to fit a specific equation to a plot of points but its failing because the equation has a square root in it and I need to keep the values of x between a certain range. I know how to do this for the constants but how do I do this for a variable? </p> <pre><code>data = Table[{x, (((x - 1.6)^2)/(16.456*(1 + Sqrt[1 - (1 + 0.65)*((x - 1.6)^2)/(16.456^2)]))) + 0.00032*(x - 1.6)^4 + .6554}, {x, -0.65, 4.073, .001}]; fit = NonlinearModelFit[ data, (((x - g)^2)/(c*(1 + Sqrt[1 - (1 + k)*((x - g)^2)/(c^2)]))) + c*(x - g)^4 + p, {{a, 0.00032}, {c, 16.456}, {k, 0.65}, {g, 1.6}, {p, .6554}}, x, MaxIterations -&gt; 100] Show[ListPlot[data], Plot[fit[x], {x, -0.65, 4.073}, PlotStyle -&gt; Red], Frame -&gt; True] </code></pre> <p>I'm telling it exactly where to start and it still can't find the fit.</p> https://mathematica.stackexchange.com/q/206170 1 Parameter Estimation for Gaussian Function in Gaussian noise Seyhmus Güngören https://mathematica.stackexchange.com/users/4256 2019-09-12T23:15:27Z 2019-09-15T08:58:24Z <p>Mathematical model can be given as follows.</p> <p><span class="math-container">$$x[n]=a\exp[-(n - k)^2/s^2]+w[n]$$</span></p> <p>Here w[n] is a zero mean Gaussian noise process with variance <span class="math-container">$3\cdot10^{-5}$</span>. The problem is to estimate the parameters <span class="math-container">$a$</span>, <span class="math-container">$k$</span> and <span class="math-container">$s$</span> as good as possible. The data samples are collected sequentially. Therefore, it is expected that the algorithm is capable of making estimations on the fly and the estimation quality improves with the increase of the total number of data samples.</p> <p>I just started for the case where the noise is simply non-existence.</p> <p>I used the following code, for the below given data and it works pretty well. The problem is when I add a littlle bit of noise to this data in order to simulate the above given mathematical model.</p> <pre><code>nlm = NonlinearModelFit[mydata[[1 ;; 300]], a*Exp[-(x - k)^2/s^2], {a, k, s}, x] </code></pre> <p>for example:</p> <pre><code>mydata2 = mydata + RandomVariate[NormalDistribution[0, Sqrt[3*10^-5]], 400]; nlm = NonlinearModelFit[mydata2[[1 ;; 300]], a*Exp[-(x - k)^2/s^2], {a, k, s}, x] </code></pre> <p>or </p> <pre><code>nlm = NonlinearModelFit[mydata2[[1 ;; 400]], a*Exp[-(x - k)^2/s^2], {a, k, s}, x] </code></pre> <p>both do not produce meaningful results. </p> <blockquote> <p>What must one do to be able to deal with this problem using NonlinearModelFit? Or in general for example with Maximum likelihood estimation (MLE)? I tried MLE but I cannot get anything in closed form except for the Amplitude a.</p> </blockquote> <pre><code>mydata={{1, 0.0004696016896143079}, {2, 0.0004754998605167475}, {3, 0.00048146795030926896}, {4, 0.0004875067327344009}, {5, 0.000493616989397984}, {6, 0.0004997995098406223}, {7, 0.0005060550916096482}, {8, 0.0005123845403316832}, {9, 0.0005187886697856992}, {10, 0.0005252683019766862}, {11, 0.0005318242672098824}, {12, 0.0005384574041655253}, {13, 0.0005451685599742198}, {14, 0.0005519585902928752}, {15, 0.0005588283593811737}, {16, 0.0005657787401786761}, {17, 0.0005728106143824625}, {18, 0.0005799248725254053}, {19, 0.0005871224140549774}, {20, 0.0005944041474126807}, {21, 0.0006017709901140973}, {22, 0.0006092238688294729}, {23, 0.0006167637194649574}, {24, 0.0006243914872444208}, {25, 0.0006321081267919121}, {26, 0.0006399146022146664}, {27, 0.0006478118871867838}, {28, 0.0006558009650335242}, {29, 0.0006638828288161673}, {30, 0.0006720584814175511}, {31, 0.0006803289356282413}, {32, 0.0006886952142332748}, {33, 0.0006971583500995931}, {34, 0.0007057193862640794}, {35, 0.0007143793760222815}, {36, 0.0007231393830176956}, {37, 0.0007320004813317731}, {38, 0.0007409637555745676}, {39, 0.0007500303009759804}, {40, 0.0007592012234777229}, {41, 0.0007684776398259019}, {42, 0.0007778606776643128}, {43, 0.0007873514756283212}, {44, 0.0007969511834394849}, {45, 0.0008066609620008387}, {46, 0.0008164819834928088}, {47, 0.0008264154314698429}, {48, 0.0008364625009577514}, {49, 0.0008466243985516465}, {50, 0.0008569023425146643}, {51, 0.0008672975628773075}, {52, 0.0008778113015375558}, {53, 0.0008884448123615773}, {54, 0.0008991993612852293}, {55, 0.0009100762264162467}, {56, 0.000921076698137077}, {57, 0.000932202079208501}, {58, 0.0009434536848739328}, {59, 0.0009548328429644679}, {60, 0.000966340894004572}, {61, 0.0009779791913185954}, {62, 0.0009897491011379713}, {63, 0.0010016520027091057}, {64, 0.0010136892884020524}, {65, 0.001025862363819943}, {66, 0.0010381726479090703}, {67, 0.0010506215730697898}, {68, 0.0010632105852681447}, {69, 0.001075941144148263}, {70, 0.0010888147231454353}, {71, 0.0011018328096000214}, {72, 0.001114996904872117}, {73, 0.0011283085244569028}, {74, 0.001141769198100848}, {75, 0.0011553804699186276}, {76, 0.0011691438985108552}, {77, 0.0011830610570825238}, {78, 0.0011971335335622672}, {79, 0.0012113629307224428}, {80, 0.0012257508662998923}, {81, 0.0012402989731175613}, {82, 0.0012550088992069328}, {83, 0.0012698823079311588}, {84, 0.0012849208781090693}, {85, 0.001300126304139942}, {86, 0.0013155002961291102}, {87, 0.0013310445800143}, {88, 0.0013467608976928378}, {89, 0.0013626510071496933}, {90, 0.0013787166825862166}, {91, 0.0013949597145498226}, {92, 0.0014113819100643969}, {93, 0.0014279850927616303}, {94, 0.0014447711030130214}, {95, 0.0014617417980628403}, {96, 0.0014788990521619123}, {97, 0.0014962447567021162}, {98, 0.0015137808203518409}, {99, 0.0015315091691922803}, {100, 0.0015494317468544429}, {101, 0.0015675505146571435}, {102, 0.0015858674517457525}, {103, 0.0016043845552318901}, {104, 0.0016231038403338208}, {105, 0.0016420273405178289}, {106, 0.0016611571076404561}, {107, 0.0016804952120914654}, {108, 0.0017000437429378116}, {109, 0.0017198048080683862}, {110, 0.0017397805343397233}, {111, 0.001759973067722423}, {112, 0.001780384573448571}, {113, 0.0018010172361600572}, {114, 0.0018218732600576001}, {115, 0.0018429548690508228}, {116, 0.0018642643069091724}, {117, 0.0018858038374136323}, {118, 0.0019075757445094173}, {119, 0.0019295823324595143}, {120, 0.0019518259259991753}, {121, 0.001974308870491162}, {122, 0.001997033532082018}, {123, 0.0020200022978592415}, {124, 0.00204321757600922}, {125, 0.0020666817959762093}, {126, 0.0020903974086221343}, {127, 0.002114366886387389}, {128, 0.0021385927234523765}, {129, 0.0021630774359001114}, {130, 0.0021878235618797226}, {131, 0.002212833661770739}, {132, 0.0022381103183484164}, {133, 0.0022636561369500053}, {134, 0.0022894737456417603}, {135, 0.0023155657953870647}, {136, 0.0023419349602153542}, {137, 0.0023685839373920855}, {138, 0.0023955154475894373}, {139, 0.002422732235058113}, {140, 0.0024502370678000586}, {141, 0.0024780327377419684}, {142, 0.0025061220609098843}, {143, 0.002534507877604617}, {144, 0.00256319305257824}, {145, 0.0025921804752112995}, {146, 0.0026214730596911488}, {147, 0.0026510737451912337}, {148, 0.0026809854960511046}, {149, 0.002711211301957591}, {150, 0.002741754178126894}, {151, 0.002772617165487437}, {152, 0.002803803330863877}, {153, 0.00283531576716194}, {154, 0.002867157593554324}, {155, 0.002899331955667316}, {156, 0.0029318420257686026}, {157, 0.002964691002955967}, {158, 0.002997882113346801}, {159, 0.0030314186102687224}, {160, 0.003065303774451082}, {161, 0.0030995409142175016}, {162, 0.00313413336567916}, {163, 0.0031690844929292495}, {164, 0.003204397688238352}, {165, 0.00324007637225061}, {166, 0.0032761239941810146}, {167, 0.0033125440320136814}, {168, 0.0033493399927008493}, {169, 0.0033865154123630826}, {170, 0.003424073856490291}, {171, 0.0034620189201438464}, {172, 0.003500354228159393}, {173, 0.0035390834353508653}, {174, 0.0035782102267154447}, {175, 0.0036177383176392553}, {176, 0.0036576714541042455}, {177, 0.003698013412895921}, {178, 0.0037387680018121402}, {179, 0.0037799390598726064}, {180, 0.003821530457529599}, {181, 0.0038635460968796066}, {182, 0.003905989911875701}, {183, 0.003948865868541081}, {184, 0.003992177965183619}, {185, 0.004035930232611049}, {186, 0.004080126734347439}, {187, 0.004124771566850392}, {188, 0.00416986885972943}, {189, 0.004215422775964974}, {190, 0.004261437512128586}, {191, 0.004307917298604109}, {192, 0.00435486639980953}, {193, 0.004402289114420054}, {194, 0.004450189775591967}, {195, 0.00449857275118758}, {196, 0.004547442444000855}, {197, 0.004596803291984207}, {198, 0.00464665976847628}, {199, 0.004697016382430332}, {200, 0.004747877678643866}, {201, 0.004799248237989198}, {202, 0.004851132677644647}, {203, 0.004903535651326949}, {204, 0.004956461849524496}, {205, 0.005009915999731568}, {206, 0.0050639028666832585}, {207, 0.00511842725259155}, {208, 0.005173493997382378}, {209, 0.005229107978933174}, {210, 0.005285274113311835}, {211, 0.0053419973550162415}, {212, 0.005399282697215}, {213, 0.005457135171988643}, {214, 0.005515559850572118}, {215, 0.005574561843598148}, {216, 0.005634146301341157}, {217, 0.005694318413962399}, {218, 0.005755083411756057}, {219, 0.005816446565395737}, {220, 0.005878413186182366}, {221, 0.005940988626292675}, {222, 0.006004178279028781}, {223, 0.006067987579068253}, {224, 0.006132422002715424}, {225, 0.006197487068153544}, {226, 0.0062631883356974215}, {227, 0.0063295314080473175}, {228, 0.006396521930543493}, {229, 0.006464165591421823}, {230, 0.0065324681220697645}, {231, 0.006601435297283679}, {232, 0.0066710729355268766}, {233, 0.006741386899188109}, {234, 0.006812383094841357}, {235, 0.006884067473506359}, {236, 0.006956446030909557}, {237, 0.0070295248077463105}, {238, 0.007103309889943699}, {239, 0.007177807408924284}, {240, 0.007253023541870322}, {241, 0.00732896451198904}, {242, 0.007405636588778868}, {243, 0.007483046088295842}, {244, 0.007561199373421404}, {245, 0.007640102854130552}, {246, 0.00771976298776116}, {247, 0.007800186279283448}, {248, 0.007881379281570702}, {249, 0.00796334859567074}, {250, 0.008046100871077617}, {251, 0.008129642806004526}, {252, 0.008213981147657484}, {253, 0.008299122692509127}, {254, 0.008385074286573856}, {255, 0.00847184282568334}, {256, 0.008559435255762958}, {257, 0.008647858573108479}, {258, 0.00873711982466387}, {259, 0.008827226108299748}, {260, 0.008918184573092054}, {261, 0.009010002419601842}, {262, 0.009102686900155536}, {263, 0.009196245319125947}, {264, 0.009290685033213592}, {265, 0.009386013451728938}, {266, 0.009482238036875396}, {267, 0.009579366304032336}, {268, 0.009677405822039234}, {269, 0.009776364213480477}, {270, 0.00987624915497003}, {271, 0.009977068377437481}, {272, 0.010078829666414185}, {273, 0.01018154086232032}, {274, 0.010285209860751873}, {275, 0.01038984461276873}, {276, 0.010495453125183327}, {277, 0.010602043460849177}, {278, 0.01070962373895057}, {279, 0.010818202135292367}, {280, 0.010927786882590718}, {281, 0.011038386270763546}, {282, 0.011150008647222046}, {283, 0.011262662417162849}, {284, 0.011376356043859661}, {285, 0.011491098048956299}, {286, 0.011606897012759966}, {287, 0.011723761574534348}, {288, 0.011841700432793744}, {289, 0.01196072234559729}, {290, 0.012080836130843974}, {291, 0.012202050666567049}, {292, 0.012324374891229834}, {293, 0.012447817804021589}, {294, 0.012572388465153093}, {295, 0.012698095996153359}, {296, 0.012824949580166162}, {297, 0.012952958462247309}, {298, 0.013082131949661404}, {299, 0.013212479412179602}, {300, 0.013344010282377671}, {301, 0.013476734055933404}, {302, 0.013610660291925196}, {303, 0.013745798613130704}, {304, 0.013882158706325}, {305, 0.014019750322579735}, {306, 0.014158583277561974}, {307, 0.014298667451833773}, {308, 0.014440012791150826}, {309, 0.014582629306762357}, {310, 0.014726527075710786}, {311, 0.014871716241130982}, {312, 0.015018207012550263}, {313, 0.015166009666188132}, {314, 0.015315134545256497}, {315, 0.015465592060258998}, {316, 0.015617392689291267}, {317, 0.01577054697834125}, {318, 0.015925065541588505}, {319, 0.016080959061704408}, {320, 0.0162382382901525}, {321, 0.016396914047487634}, {322, 0.016556997223656153}, {323, 0.01671849877829549}, {324, 0.016881429741034147}, {325, 0.01704580121179054}, {326, 0.0172116243610727}, {327, 0.017378910430277787}, {328, 0.017547670731990553}, {329, 0.017717916650282543}, {330, 0.017889659641010587}, {331, 0.0180629112321157}, {332, 0.018237683023920606}, {333, 0.018413986689427973}, {334, 0.018591833974618544}, {335, 0.018771236698747706}, {336, 0.018952206754643035}, {337, 0.019134756109001325}, {338, 0.019318896802684327}, {339, 0.019504640951015263}, {340, 0.019692000744074168}, {341, 0.019880988446993795}, {342, 0.02007161640025356}, {343, 0.020263897019974386}, {344, 0.020457842798212975}, {345, 0.02065346630325453}, {346, 0.020850780179906252}, {347, 0.02104979714978936}, {348, 0.02125053001163171}, {349, 0.02145299164155823}, {350, 0.021657194993382013}, {351, 0.021863153098895007}, {352, 0.022070879068156565}, {353, 0.02228038608978288}, {354, 0.022491687431235516}, {355, 0.022704796439108162}, {356, 0.02291972653941392}, {357, 0.023136491237871225}, {358, 0.023355104120189686}, {359, 0.023575578852353802}, {360, 0.023797929180907142}, {361, 0.024022168933235784}, {362, 0.024248312017849494}, {363, 0.02447637242466357}, {364, 0.02470636422527892}, {365, 0.024938301573262146}, {366, 0.025172198704423065}, {367, 0.025408069937092777}, {368, 0.02564592967240078}, {369, 0.025885792394549478}, {370, 0.026127672671089334}, {371, 0.026371585153192822}, {372, 0.026617544575925807}, {373, 0.026865565758519682}, {374, 0.027115663604641082}, {375, 0.027367853102661762}, {376, 0.027622149325925435}, {377, 0.02787856743301484}, {378, 0.028137122668018034}, {379, 0.028397830360791194}, {380, 0.028660705927222665}, {381, 0.028925764869493876}, {382, 0.029193022776340737}, {383, 0.029462495323311303}, {384, 0.02973419827302401}, {385, 0.030008147475424414}, {386, 0.030284358868038947}, {387, 0.030562848476228626}, {388, 0.030843632413441655}, {389, 0.031126726881462462}, {390, 0.03141214817066134}, {391, 0.031699912660241324}, {392, 0.03199003681848457}, {393, 0.03228253720299529}, {394, 0.03257743046094255}, {395, 0.03287473332930184}, {396, 0.033174462635092716}, {397, 0.03347663529561699}, {398, 0.03378126831869377}, {399, 0.03408837880289416}, {400, 0.03439798393777186}} </code></pre> https://mathematica.stackexchange.com/q/206111 0 How is CurvatureConfidenceRegion in FittedModel from NonlinearModelFit calculated? Kevin Ausman https://mathematica.stackexchange.com/users/54593 2019-09-11T20:32:56Z 2019-09-12T22:42:26Z <p>I have a <code>FittedModel</code> from <code>NonlinearModelFit</code> for which the determination of the <code>FitCurvatureTable</code> can be extremely slow. These curvature diagnostics are part of the output in a large dynamic interface, and I would prefer to not have to tell the user to use <kbd>Alt</kbd><kbd>.</kbd> to interrupt the calculation. On the plus side, it seems that I can use a (seemingly) undocumented feature of <code>FittedModel</code>, <code>EvaluationMonitor</code>, to interrupt the calculation using the technique I employed in answering <a href="https://mathematica.stackexchange.com/questions/203966/interrupt-a-fit-minimization-without-interrupting-overall-evaluation">this question</a> for interrupting a fit. However, if the user is willing to sit through the long calculation, I would like for them to be able to update the <code>CurvatureConfidenceRegion</code> portion of the table for a modified <code>ConfidenceLevel</code> without having to go back through the whole calculation. </p> <p>I have implemented something similar for the <code>MeanPredictionBands</code> and <code>SinglePredictionBands</code> by storing the <code>MeanPredictionErrors</code> and <code>SinglePredictionErrors</code> and multiplying by:</p> <pre><code>Quantile[NormalDistribution[0, 1], (1 + confidenceLevel)/2]*{-1,1} </code></pre> <p>and then adding the fit values. However, I don't see how to do something similar for <code>CurvatureConfidenceRegion</code>, because I don't know how it is calculated relative to the <code>ConfidenceLevel</code>.</p> <p>So, my question: if I have a <code>CurvatureConfidenceRegion</code> for a given value of <code>ConfidenceLevel</code>, is there a straightforward way to calculate that <code>CurvatureConfidenceRegion</code> for a different value of <code>ConfidenceLevel</code>?</p> https://mathematica.stackexchange.com/q/205004 0 goodness of fit test for grouped data Ferenc Beleznay https://mathematica.stackexchange.com/users/30237 2019-09-09T14:41:28Z 2019-09-09T16:49:13Z <p>I have the following grouped data:</p> <p><a href="https://i.stack.imgur.com/C9MuQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C9MuQ.png" alt="enter image description here"></a></p> <p>Can someone please tell me how to run a goodness of fit test to test if this data is from a normal distribution?</p> <p>I would like to do this without estimating the parameters from the data.</p> <p>I could only find PearsonChiSquaredTest, but it is for raw data, not grouped data.</p> https://mathematica.stackexchange.com/q/204957 1 fitting a monotonic function? Kagaratsch https://mathematica.stackexchange.com/users/5517 2019-09-08T04:22:48Z 2019-09-08T23:48:48Z <p>Let's say I have a few points and derivatives given for a function and I'd like to find the best fit through the data, which is <strong>monotonic</strong>. For example, if I take a polynomial ansatz, I get</p> <pre><code>f[x_] = Sum[a[i] x^i, {i, 0, 4}]; constraints = f[x1] == y1 &amp;&amp; f'[x1] == 0 &amp;&amp; f[x2] == 0 &amp;&amp; f[x3] == y3 &amp;&amp; f'[x3] == 0; result = f[x] /. Solve[constraints, Table[a[i], {i, 0, 4}]][] // FullSimplify; </code></pre> <p>which for some <code>xi</code> and <code>yi</code> sometimes leads to an interpolation that is not monotonic:</p> <p><a href="https://i.stack.imgur.com/luu3k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/luu3k.png" alt="enter image description here"></a></p> <p>as can be seen by the fact that the minimum is not at the region boundary.</p> <p>How can I produce a fit (with the simplest model possible) that would satisfy all constraints and simultaneously return a result that is monotonic?</p> <p>EDIT:</p> <p>More details on the data:</p> <p>The data is always given in terms of points at the far left and far right boundary of a range (positive on the left and negative on the right), slopes at these points are zero, and the position at which the function crosses the x-axis is also known, so it looks something like:</p> <p><a href="https://i.stack.imgur.com/VyUFt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VyUFt.png" alt="enter image description here"></a></p> <p>Example values might be:</p> <pre><code>Xleft=0; Xright=1; Xmid=0.41; </code></pre> <p>As a list of points:</p> <pre><code>fvalues={{0,1180},{0.41,0},{1,-570}}; fprimes={{0,0},{1,0}}; </code></pre> <p>for which fourth degree polynomial fit produces the non-monotonic function plotted above. Basically, I'm wondering what would be the simplest function template to fit 3 given points and 2 given slopes to, such that the function is monotonic?</p> https://mathematica.stackexchange.com/q/204835 1 What is the most efficient way to use the fitted models? MassDefect https://mathematica.stackexchange.com/users/42264 2019-09-05T15:25:01Z 2019-09-05T19:30:25Z <p>I'm generating a simple linear model (<span class="math-container">$y=mx+b$</span>) by fitting to the background of some data, and then trying to plug in the x-values of the data I'm actually interested in.</p> <p>I'm a bit surprised that <code>FittedModel</code>s aren't listable, so I tried using <code>Map</code>. This turned out to be exceedingly slow. For example:</p> <pre><code>lm = LinearModelFit[Table[{x, 3 x + 6.2}, {x, 100}], x, x]; AbsoluteTiming[lm/@Range;] AbsoluteTiming[3 Range + 6.2;] AbsoluteTiming[3 # + 6.2&amp;/@Range;] AbsoluteTiming[Normal[lm]/.x -&gt; Range;] (* {25.037, Null} *) (* {0.002096, Null} *) (* {0.010351, Null} *) (* {0.004413, Null} *) </code></pre> <p>Using <code>Normal</code> seems to be the fastest way of using the linear model. Is this really the best way to extract values from a <code>FittedModel</code>?</p> <p><strong>What is the best (or most usual) way to extract many values from a model?</strong></p> <p>This seems like it would be a common problem, so I'm certain there must be something about it either here or elsewhere, but perhaps I'm not using the correct search terms.</p> https://mathematica.stackexchange.com/q/204737 0 Cannot fit data Veronika https://mathematica.stackexchange.com/users/67054 2019-09-03T18:48:13Z 2019-09-04T13:30:30Z <p>First up, I'm quite new to Mathematica so any hints on better code would be greatly appreciated. I have already asked the question connected to this code, but later I've found a mistake. Now I need to fit my data. I have two sets of data: one for positive value of <code>F</code> -<code>ArrayFVelp</code> and another for negative value of <code>F</code> -<code>ArrayFVelm</code>. I have a fitting function <code>newModel</code>, and I would like to know the parameters <code>m, A, G</code> of the model. I've tried to use NonLinearModelFit, FindFit, and Method -> "NMinimize", but all my attempts were unsuccessful. </p> <p>Thanks in advance!</p> <pre><code>ArrayFVelp ={{2.57701, 22.7458}, {3.83219, 29.6856}, {5.30692, 36.3019}, {6.93287, 41.1253}, {8.31929, 35.6894}, {9.31232, 35.5811}}; ArrayFVelm = {{1.04636, 17.0544}, {1.69469, 79.1147}, {2.17592, 129.16}, {2.17978, 173.519}}; L = 4.0*^-3; kT = 1.38*^-5*300; diff = 30.0; newModel[m_?NumericQ, A_?NumericQ, G_?NumericQ, F_?NumericQ] := ( f := G*Exp[A (1 - 1/(1 - #^2))] &amp;; g := 2 ((# + L/2)/(L/2*2))^(-(Log/Log[1/2 + m/2])) - 1 &amp;; well := f[g[#], m, A, G] &amp;; V := Piecewise[{{well[#, m, A, G], # &gt;= -L/2 &amp;&amp; # &lt;= L/2}, {well[# - L, m, A, G], # &gt; L/2 &amp;&amp; # &lt;= L/2 + L}}] &amp;; n = diff/ NIntegrate[(Exp[V[y] - F*y/kT]*Exp[F*x/kT - V[x]]) // Evaluate, {x, -L/2, L/2}, {y, x, x + L}, WorkingPrecision -&gt; 5]; Abs[L*n*(1 - Exp[-F*L/kT])] ); fit := FindFit[ ArrayFVelm, {newModel[m, A, G, F], -1 &lt; m &lt; 0, 0 &lt;= A &lt;= 32, -15 &lt;= G &lt;= -0}, {m, A, G}, F] </code></pre> <p><a href="https://i.stack.imgur.com/5jAvQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5jAvQ.png" alt="enter image description here"></a> <a href="https://i.stack.imgur.com/bVCwd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bVCwd.png" alt="enter image description here"></a> <a href="https://i.stack.imgur.com/QqYVN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QqYVN.png" alt="enter image description here"></a></p> https://mathematica.stackexchange.com/q/204445 4 Plotting perpendicular lines to a contour plot (electric field from equipotential curves) Stefan Quandt https://mathematica.stackexchange.com/users/67162 2019-08-28T03:45:07Z 2019-09-07T22:15:59Z <p>I'm currently working on a experiment about Electric Fields and Equipotential Curves. The problem is that I want to plot (estimate) the curves of electric field using the fact that every one of these curves must be perpendicular to equipotential ones. </p> <p>To make it short, I have values (V) in a plane, and everywhere there's the same value of V, I have to conect these points and get a curve. The problem is that these V values are experimental, so they aren't exactly the same, but I can get a relation with a contour plot: </p> <pre><code>ListContourPlot[Data, InterpolationOrder -&gt; 7, PlotLegends -&gt; Automatic, PlotRange -&gt; All] </code></pre> <p>where Data is an array with 15x8 intensity V values:</p> <pre><code>Data = {{3.3, 2.97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.8, 4.2}, {3.85, 3.65, 3.76, 3.41, 3.31, 3.33, 3.35, 3.3, 3.38, 3.44, 3.6, 3.65, 4., 4.2, 4.45}, {4.48, 4.35, 4.45, 4.28, 4.22, 4.25, 4.31, 4.3, 4.32, 4.37, 4.45, 4.46, 4.6, 4.67, 4.76}, {5.08, 5.09, 5.17, 5.25, 5.23, 5.27, 5.27, 5.28, 5.3, 5.3, 5.35, 5.3, 5.27, 5.23, 5.22}, {5.75, 5.85, 5.25, 6.18, 6.31, 6.30, 6.45, 6.44, 6.65, 6.62, 6.52, 6.41, 6.17, 6.02, 5.86}, {7.5, 7.22, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6.33, 6.22}, {6.56, 6.74, 7.01, 7.28, 7.41, 7.52, 7.52, 7.53, 7.51, 7.45, 7.36, 7.22, 6.95, 6.63, 6.5}, {6.9, 7.03, 7.08, 7.22, 7.3, 7.32, 7.35, 7.34, 7.29, 7.23, 7.18, 7.12, 7.0, 6.84, 6.7}} </code></pre> <p>Before anyone wonders why, I've used an Interpolation because the data is not enough to create curved shapes, and from theory I know that these lines aren't usually straight lines. </p> <p>The problem is: I need to get the perpendicular vector plot of this contourplot, or parametrize the contour curves to apply a gradient relation (E=-grad(V)). </p> <p>What could I do? Any suggestions? I'm adding pictures of the result I got and what I want it to be, for this same specific configuration (2 parallel charged plates).</p> <p><a href="https://i.stack.imgur.com/hKVrh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hKVrh.png" alt="This is the result of the contour plot"></a></p> <p><a href="https://i.stack.imgur.com/oxZ71.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oxZ71.png" alt="This is the exact configuration I&#39;m trying to work with, but without all the errors involved"></a></p> https://mathematica.stackexchange.com/q/204435 0 FindFit gives poor regression Toadfuture https://mathematica.stackexchange.com/users/67158 2019-08-27T23:43:17Z 2019-08-27T23:57:28Z <p>My code is </p> <pre><code>data = {{0, 0.00355}, {6, 0.00343}, {9, 0.00331}, {48, 0.00319}, {173, 0.00308}, {200, 0.00308}}; fun= FindFit[data, a*Exp[b*(x)] + c, {a, b, c}, x] p[x_] := a*Exp[b*x] /. fun </code></pre> <p>This gives me {a -> -3.2091*10^-91, b -> 1., c -> 0.003312}</p> <p>whereas when I use desmos it gives me: <a href="https://i.stack.imgur.com/jEHbf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jEHbf.png" alt="desmos"></a></p> <p>where a=0.000437, b=-0.0598, and c=0.00310.</p> <p>Desmos also gives the same answer as when I put it into wolfram alpha. I just want to know why my mathematica doesn't give me the same result and how I can make it give me the same as desmos. I have a lot of other data I would like to process in the same way and doing it all in desmos would be too painful.</p> https://mathematica.stackexchange.com/q/204334 1 Output of the trained neural network for a set of inputs Gizem Y. https://mathematica.stackexchange.com/users/66574 2019-08-26T15:05:10Z 2019-08-27T10:00:49Z <p>Sorry if this was too trivial but I want to get the output of the neural network I trained for the specific inputs that I feed. The notation I often saw was of TrainedNet[inp] kind. But when I use this I do not get the output but the result given in figure below:<a href="https://i.stack.imgur.com/3EBVK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3EBVK.png" alt="enter image description here"></a></p> https://mathematica.stackexchange.com/q/204298 1 What does the following error message mean, in the context of NonlinearModelFit? Q.P. https://mathematica.stackexchange.com/users/27119 2019-08-25T18:13:57Z 2019-08-25T18:13:57Z <p>I am deliberately not including data or a model for this question, as I want a more general answer so that I may be able to trouble shoot this myself in the future.</p> <p>When performing a fit, usually a fit with more than two parameters, I occasionally see the error message:</p> <blockquote> <p>The step size in the search has become less than the tolerance prescribed by the PrecisionGoal option, but the gradient is larger than the tolerance specified by the AccuracyGoal option. There is a possibility that the method has stalled at a point that is not a local minimum.</p> </blockquote> <p>More often than not, the fit looks good by eye, and the fitted parameter values are reasonable. </p> <p>So the questions are:</p> <ul> <li>What does this error really mean?</li> <li>Does it matter, especially if the fit looks good and the fit parameters are reasonable? That is, how trust worthy are the results?</li> <li>How can one prevent this error? I have often played with <code>PrecisionGoal</code> and <code>AccuracyGoal</code> but it doesn't seem to have much of an effect.</li> </ul> <p>I appreciate this problem will have solutions that vary depending on the individual case, but I often fit with very different models and would like some advice on how to avoid such errors, and trouble shoot them myself -- rather than just post the problem here.</p> https://mathematica.stackexchange.com/q/204200 1 Regression to fit data with an integral equation José Augusto Devienne https://mathematica.stackexchange.com/users/60402 2019-08-23T18:31:39Z 2019-08-24T01:40:03Z <p>I have a set data and I know that this data are best fitted by the following equation, assuming that f(<span class="math-container">$\tau$</span>) is a log normal distribution and <span class="math-container">$M_{eq}$</span> = 1.5:</p> <p><span class="math-container">$M(t,\tau)= M_{eq} \int (1-e^{-t/\tau}) f(\tau) d\tau$</span></p> <p>I was wondering if there is a way to perform a regression, i.e., by finding the best fitting curve for the given set of data (using the equation above), get the best <span class="math-container">$f(\tau)$</span> that improve the best fit as a by-product ?</p> <p>Thanks in advance</p> <p>PS.: The set of data that I'm talking about is: </p> <pre><code>data={{0.995, 0.142}, {3.003, 0.2}, {5.908, 0.25}, {10.525, 0.36}, {13.617, 0.498}, {24.321, 0.616}, {33.917, 0.599}, {47.843, 0.7}, {64.172, 0.835}, {91.353, 1.102}, {126.745, 1.083}, {174.118, 1.225}, {225.059, 1.133}, {292.998, 1.165}, {369.133, 1.298}, {640.295, 1.365}, {828.169, 1.298}, {1255.39, 1.373}, {1496.61, 1.409}, {1942.79, 1.538}} </code></pre> <p><a href="https://i.stack.imgur.com/zUPnh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zUPnh.png" alt="enter image description here"></a></p> https://mathematica.stackexchange.com/q/204138 5 What are the most reliable test of goodness of fit in Mathematica Q.P. https://mathematica.stackexchange.com/users/27119 2019-08-22T19:38:27Z 2019-08-24T22:06:45Z <p>I realise this is possibly a VERY open ended question, and potentially has answers dotted around e.g. <a href="https://mathematica.stackexchange.com/questions/67676/test-goodness-of-fit-for-findfit">here</a> . </p> <p>What are the most robust methods of testing the result of <code>NonlinearModelFit[...]</code> in Mathematica?</p> <p>I know already that there are some methods available in Mathematica such as <code>AdjustedRSquared</code>, <code>RSquared</code>, <code>AIC</code>, <code>AICc</code>, and <code>BIC</code>.</p> <p>The most familiar of these to me personally are <code>AdjustedRSquared</code> and <code>RSquared</code>, but I know many methods of testing exist.</p> <p>A sub-question to this is, when are these tests (and others, whatever they may be), most appropriate? Does this depend on the model fitted, the number of parameters, the amount of data, and so on?</p> <p>I strongly appreciate that some of these questions can only be answered subjectively, and I even anticipate some potential off-topic or close requests; after all if there was a catch-all test -- I wouldn't need to ask this question! But I feel having a question where goodness of fit specifically in Mathematica is discussed (especially where some of the more experienced members of the community can contribute) might be beneficial. </p> https://mathematica.stackexchange.com/q/204093 3 Fitting a function, a (x - 1)^b, to a given set of data user67082 https://mathematica.stackexchange.com/users/0 2019-08-21T22:06:35Z 2019-08-21T22:54:04Z <p>I have some data:</p> <pre><code>data = {{0.3, 0}, {0.5, 0}, {0.84, 0}, {1, 0}, {1.16, 159.1940}, {1.3, 218.835}, {1.5, 278.0620}, {1.8, 340.758}, {2.01, 374.9820}, {2.3, 416.09}} </code></pre> <p>which looks like:</p> <p><a href="https://i.stack.imgur.com/xd6PT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xd6PT.png" alt="enter image description here"></a></p> <p>It seems that some function of the form: <span class="math-container">$$a (x - 1)^b$$</span> should fit the data.</p> <p>However, if we use</p> <pre><code>FindFit[data, a (x - 1)^b, {a, b}, x] </code></pre> <p>Mathematica gives: "The Jacobian is not a matrix of real numbers at {a,b} = {1.,1.}".</p> <p>How can I fix this?</p> https://mathematica.stackexchange.com/q/204002 0 Problems with NonlinearModelFit: "..is not a real number at " [closed] Veronika https://mathematica.stackexchange.com/users/67054 2019-08-20T13:47:39Z 2019-08-20T14:02:15Z <p>I am a newcomer to Mathematica. Basically I just want to fit the data shown below (<code>ArrayFVel</code>). Unfortunately, the model contains rather complicated integral that cannot be solved analytically, but it can be solved numerically. Function <code>V</code> is the domain of function <code>well</code>. I get the following error:</p> <p><a href="https://i.stack.imgur.com/11fVm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/11fVm.png" alt="enter image description here"></a></p> <pre><code>ArrayFVel = {{1.04636, 17.0544}, {1.69469, 79.1147}, {2.17592, 129.16}, {2.17978, 173.519}}; L = 4.0*^-3; kT = 1.38*^-5*300; diff = 30.0; f[x_?NumericQ, A_?NumericQ, G_?NumericQ] := G*Exp[A (1 - 1/(1 - x^2))]; g[x_?NumericQ, m_?NumericQ] := 2 ((x + L/2)/(L/2*2))^(-(Log/Log[1/2 + m/2])) - 1; well[x_?NumericQ, m_?NumericQ, A_?NumericQ, G_?NumericQ] := f[g[x, m], A, G]; V[x_?NumericQ, m_?NumericQ, A_?NumericQ, G_?NumericQ] := Piecewise[ {{well[x, m, A, G], x &gt;= -L/2 &amp;&amp; x &lt;= L/2 &amp;&amp; NumericQ @ m &amp;&amp; NumericQ @ A &amp;&amp; NumericQ @ G}, {well[x - L, m, A, G], x &gt; L/2 &amp;&amp; x &lt;= L/2 + L &amp;&amp; NumericQ @ m &amp;&amp; NumericQ @ A &amp;&amp; NumericQ @ G}}]&amp;; n[m_?NumericQ, A_?NumericQ, G_?NumericQ, F_?NumericQ] = NIntegrate[ Exp[V[NumericQ[y], m, A, G] - F*NumericQ[y]/kT]* Exp[F*NumericQ[x]/kT - V[NumericQ[x], m, A, G]] // Evaluate, {x, -L/2, L/2}, {y, x, x + L}]; model[m_?NumericQ, A_?NumericQ, G_?NumericQ, F_?NumericQ] = Abs[L*diff/n[m, A, G, F]*(1 - Exp[-F*L/kT])]; fit = NonlinearModelFit[ ArrayFVel, {model[m, A, G][F], -1 &lt;= m &lt;= 0, 0 &lt;= A &lt;= 32, -14 &lt;= G &lt;= 0}, {{m}, {A}, {G}}, F] </code></pre> https://mathematica.stackexchange.com/q/203878 11 What is the best way to use errors in mathematica M12? Q.P. https://mathematica.stackexchange.com/users/27119 2019-08-17T18:29:47Z 2019-08-17T19:41:57Z <p>Since <code>ErrorListPlot</code> has been superseded by new functionality in <code>ListPlot</code> for Mathematica 12.0, what is the best way to handle errors efficiently? Previously I would simply format my data in a table as</p> <pre><code> Data = Table[ Stuf to calculate values and errors {{xValue, yValue, yError}}, {i, 1, Stop} ] </code></pre> <p>Which made it easy to stick into <code>ErrorListPlot</code> or use in a fit as</p> <pre><code>NonlinearModel[Data[[1;;,{1, 2}]], Function, Weights-&gt;1/Data[[1;;,3]]^2 ] </code></pre> <p>Now it seems you have to use <code>Around[yValue, yError]</code></p> <pre><code> Data = Table[ Stuf to calculate values and errors {{xValue, Around[yValue, yError]}}, {i, 1, Stop} ] </code></pre> <p>Which works fine in <code>ListPlot</code> and I can fit data with it, as in it produces a fit result, but I can't figure out how to use <code>Weights</code> with <code>Around</code> and I can't plot the result of the fit in the usual way. Can anyone recommend an efficient way to format data in line with the new updates?</p> https://mathematica.stackexchange.com/q/203770 1 Combining fitted parameters to make a variable and telling me the error in it Houndbobsaw https://mathematica.stackexchange.com/users/65346 2019-08-15T14:24:23Z 2019-08-15T15:23:41Z <p>I've fit my set of data to a function of the form: <span class="math-container">$a*\cos(2\pi t)+b*\sin(2\pi t)+c*\cos(4\pi t)+d*\sin(4\pi t)+e$</span> with a nonlinear model fit. The parameters <span class="math-container">$a,b,c,d,e$</span> have errors in it that Mathematica would give me, from the covariance matrix. I want to calculate the phase which is given by <span class="math-container">$\arctan(-b/a)$</span>, and I want Mathematica to tell me the error in the phase. Is this possible?</p> https://mathematica.stackexchange.com/q/203680 2 How can you set the fit tolerance in NonlinearModelFit? Q.P. https://mathematica.stackexchange.com/users/27119 2019-08-13T13:38:23Z 2019-08-13T13:38:23Z <p>In <code>NonlinearModelFit</code> how can you set the tolerances of where the fitting terminates. I know you can set <code>MaxIterations</code> but how can how set the tolerances of parameters in a similar way to Matlab as indicated <a href="https://uk.mathworks.com/help/matlab/math/setting-options.html#bt00l89-1" rel="nofollow noreferrer">here</a></p> https://mathematica.stackexchange.com/q/203638 0 Nonlinear fitting does not perform appropriately Inzo Babaria https://mathematica.stackexchange.com/users/33967 2019-08-12T17:46:40Z 2019-08-12T21:41:35Z <p>I have a list as below one:</p> <pre><code> mustbefitted={{0., 1.}, {4.4, 0.982211}, {8.9, 0.961575}, {13.3, 0.942571}, {17.8, 0.923857}, {22.2, 0.906203}, {25.1, 0.046994}}; </code></pre> <p>I wish to fit the list with a function as <code>a - b t^c</code></p> <pre><code>NonlinearModelFit[mustbefitted, a - b t^c, {a, b, c}, t]; </code></pre> <p>But unfortunately the above fitting command does not perform correctly and I could not understand the mean of the massage after running the function i.e., <code>The Jacobian is not a matrix of real numbers at {a,b,c} = {1.,1.,1.}</code>!!!</p> https://mathematica.stackexchange.com/q/203625 0 Fitting data with NIntegration related function Gopal Verma https://mathematica.stackexchange.com/users/43205 2019-08-12T09:00:57Z 2019-08-12T16:30:25Z <p>I am trying to fit experimental data with <a href="https://drive.google.com/file/d/1xYkM0I8KbIrK5tqV-If3xQtqAuHBKwhz/view?usp=sharing" rel="nofollow noreferrer">data file</a> given below function, but I am not able to find the best fit. Constant value is <code>A=6.26</code> and <code>B=0.0041684</code></p> <pre><code>data = Import["https://pastebin.com/raw/WB4RX3Bi", "RawJSON"]; γ1 = 72*10^-3; d = 1.0*10^3; g = 9.8; w = 175*10^-6; lc = Sqrt[γ1/(g*d)]; B = (w/lc)^2; c = 3*10^8; n1 = 1; n2 = 1.33; P1 = ((4*3)/(c*Pi*w^2))*(n1)*((n2 - n1)/(n2 + n1)); cd[(az_)?NumericQ, (t_)?NumericQ] := 1*10^9*P1*w^2/(4*γ1)* Quiet[NIntegrate[ Exp[-(x^2)/ 8]*(1/(1 + B/x^2))*(1 - Exp[-(t/az)*x (1 + B/x^2)/(1 + B)])/ x, {x, 0, 10}]]; fit3 = NonlinearModelFit[data, {cd[az, t], az &gt; 0}, az, t] Show[DiscretePlot[fit3[t], {t, 0, .15, .005}, PlotRange -&gt; All, Frame -&gt; True, ImageSize -&gt; 560, PlotStyle -&gt; Red, PlotRange -&gt; All], ListLinePlot[d5r, PlotStyle -&gt; Blue, Frame -&gt; True, PlotRange -&gt; All]] </code></pre> <p><a href="https://i.stack.imgur.com/Mqn4T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Mqn4T.png" alt="plots that I want to fit"></a></p> https://mathematica.stackexchange.com/q/203207 5 Assign Error bars for y-intercept lol https://mathematica.stackexchange.com/users/40838 2019-08-03T02:19:07Z 2019-08-04T18:27:52Z <p>I have some data (x,y) with error bars in y direction:</p> <pre><code> {{{1/10, 4.92997}, ErrorBar[0.00875039]}, {{1/20, 4.90374}, ErrorBar[0.00912412]}, {{1/25, 4.89318}, ErrorBar[0.00707122]}, {{1/30, 4.89534}, ErrorBar[0.00870608]}, {{1/40, 4.87807}, ErrorBar[0.00829155]}, {{1/50, 4.84442}, ErrorBar[0.0226886]}, {{1/100, 4.83867}, ErrorBar[0.0973819]}} </code></pre> <p>Now I am trying to find a linear fit to the data, and I want the y-intercept of this linear fit (when x=0). How do I get the uncertainty (error bar) for the y-intercept due to those error bars in the data?</p> https://mathematica.stackexchange.com/q/203158 0 No progress on validation NN training set Helseth Hlaalu https://mathematica.stackexchange.com/users/66833 2019-08-02T09:20:19Z 2019-08-02T09:55:03Z <p>So in short my issue is that I'am training a neural network and while the training loss reduces over time, the loss of validation set remains constant.</p> <p>The progress of training looks like this:</p> <p><a href="https://i.stack.imgur.com/ygasI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ygasI.png" alt="Training progress"></a></p> <p>I am using the modified architecure of <a href="https://resources.wolframcloud.com/NeuralNetRepository/resources/Single-Image-Depth-Perception-Net-Trained-on-Depth-in-the-Wild-Data" rel="nofollow noreferrer">Single-Image depth perception</a> which is changed to use the 64x64 images and I've added the linear layer at the end which has a 50 channels thus the net output is 50 values, these values are the PCA coefficients of the depth images which I use as the dataset. So basically I took a bunch of depth images, performed a PCA analythis and converted all images to the first 50 coefficients of the PCA model with the highest eigen-values. There is no issue with the PCA model or vectors, it was verified multiple times, but I can't get the net to train on the coefficients properly.</p> <p>I've also tried to train on the depth images themselves which produced the expected training progress of gradual decline of both training loss and validation loss, but my goal is to produce the PCA values since the depth images produce by the net have too much noise which is really bad in my case. While the PCA coefficients produced by the net a pretty far off on the validation loss, the resulting depth image produced when converting PCA -> depth image is great in terms of low noise hence why I am training on the PCA coefficients.</p> <p>I've also tried to reduce the number of channels of the convolution layers and barely managed to reduce the validation loss from 0.13 down to 0.11, while the training loss went from 0.001 to 0.09 and the size of the net went from 24Mb down to 0.5Mb. My overall goal is to get 0.05-0.01 validation loss.</p> <p>Adding the L2Regularization either increases the trainig time or makes it so that loss never leaves the value of 1.</p> <p>My NetTrain call looks like this:</p> <pre><code>Net = NetTrain[Net, trainingData, ValidationSet -&gt; validationData, TargetDevice -&gt; "GPU", TimeGoal -&gt; 50*3600, Method -&gt; {"ADAM"}, TrainingProgressCheckpointing -&gt; {"Directory", NotebookDirectory[] &lt;&gt; "trained/PCA", "Interval" -&gt; Quantity[1, "Hours"]}] </code></pre> <p>So my question is what can I do to the net architecture or the NetTrain parameters to achieve lower validation loss?</p> https://mathematica.stackexchange.com/q/202824 0 Obtain reduced chi-squared for fit luiz https://mathematica.stackexchange.com/users/66172 2019-07-27T13:19:43Z 2019-07-28T08:15:09Z <p>Dear Colleagues, I have performed a fit according to the code below. The last step is to obtain the value for the reduced chi-squared for the goodness of fit. Does mathematica have a simple function that will give the answer directly or do I have to write the code to manipulate the data myself?</p> <pre><code>ClearAll["Global*"] E0 = 6 ; (* Electron energy in MeV*) r0 = 4.975 ; (* 10 MeV csda range in water cm*) mc2 = 0.51099895 ; (* Electron mass*c2 MeV*) q = 3 ; (* Electron Charge compatible with units of Mev, cm and teslas *) lambda = E0/mc2 ; (*constant*) m0 = 0.0186269157429903; (*All*) Zf[csda_] := (-1 + Exp[m0*csda])/m0; arc[B_, mg_, rho_] := mg*(rho - (2*E0/(q*B))*ArcSin[rho/(2*E0/(q*B))]); NZfprime[mg_, rho_, B_] := Exp[m0*rho + arc[B, mg, rho]]; alpha[mg_?NumericQ, csda_?NumericQ, B_?NumericQ] := NIntegrate[NZfprime[mg, rho, B], {rho, 0, csda}]/Zf[csda]; data = {{0.3, 0.999734507157122}, {0.4, 0.999385602795506}, {0.5, 0.999000073501766}, {0.6, 0.99853529076513}, {0.7, 0.998014296669706}, {0.8, 0.997441248453792}, {0.9, 0.996780680891339}}; error = {1.48598975699963*10^(-4), 1.51681330269219*10^(-4), 1.50344266461607*10^(-4), 1.58948671957842*10(^-4), 1.56780798669199*10^(-4), 1.61928577268932*10^(-4), 1.69830239808624*10^(-4)}; limit = Exp[rho*m0]; alpha[mg_?NumericQ, csda_?NumericQ, B_?NumericQ] := NIntegrate[If[B == 0, limit, NZfprime[mg, rho, B]], {rho, 0, csda}, Method -&gt; {"GlobalAdaptive", Method -&gt; "MultipanelRule"}]/Zf[csda]; solution = NonlinearModelFit[ data, {alpha[mg, csda, B] , mg &gt; 0, 0 &lt; csda}, {{mg, 0.40}, {csda, 1.}}, B, Weights -&gt; 1/error^2, Method -&gt; "NMinimize"] (*{csda-&gt;0.162831,l-&gt;0.934341}*) Show[Plot[solution[x], {x, 0, 1}], ListPlot[data]] </code></pre> <p>I understand that the question may seem trivial but I have been unable to find the function myself.</p> <p>Regards, Ricardo</p> https://mathematica.stackexchange.com/q/202730 0 How to fit data with superposition of CDFs? José Augusto Devienne https://mathematica.stackexchange.com/users/60402 2019-07-25T15:15:06Z 2019-08-24T21:02:55Z <p>With the help of @JimB, I could fit some experimental data with a CDF using <code>NonLinearFitModel</code>. The set of data is the following:</p> <pre><code>data ={{0.995, 0.142}, {3.003, 0.2}, {5.908, 0.25}, {10.525, 0.36}, {13.617, 0.498}, {24.321, 0.616}, {33.917, 0.599}, {47.843, 0.7}, {64.172, 0.835}, {91.353, 1.102}, {126.745, 1.083}, {174.118, 1.225}, {225.059, 1.133}, {292.998, 1.165}, {369.133, 1.298}, {640.295, 1.365}, {828.169, 1.298}, {1255.39, 1.373}, {1496.61, 1.409}, {1942.79, 1.538}} </code></pre> <p>and the suggestion of @JimB (code below) gives as a best fit the following result:</p> <pre><code>nlm = NonlinearModelFit[data, b CDF[NormalDistribution[c, d], Log10[t]], {b, c, d}, t]; rateOfChange = D[nlm[10^log10t], log10t] /. 10^log10t -&gt; t </code></pre> <p><a href="https://i.stack.imgur.com/9Ii1Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Ii1Z.png" alt="enter image description here"></a></p> <p>I'd like to know if there is a way to fit this data with not only one CDF, but the superposition of two (or, in most precise way, as many CDF as possible to reach te best fit). What I'm attempting to get as result is something like this:</p> <p><a href="https://i.stack.imgur.com/r3odb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r3odb.png" alt="enter image description here"></a> </p> <p>(Obs: These data above are not the same as the fitted data in the first figure. I just posted it as an ilustrative example of what I'm trying to do with my data)</p> <p>I know that it can be possible with the use of some algorithms as Maximum Likelihood Estimation (MLE) and Gaussian Mixture Model (GMM) to iteratively fit CDFs with the measured data to get the best fit. But I'm pretty new in programming and it have been certainly a challenge for me. </p> <p>EDIT:</p> <p>The simple replacement <code>b CDF[NormalDistribution[c, d]</code> to <code>b1 CDF[NormalDistribution[c1, d1] + b2 CDF[NormalDistribution[c2, d2]</code> proposed by @JimB gives as result: <a href="https://i.stack.imgur.com/DTL8W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DTL8W.png" alt="enter image description here"></a></p> <p>The black line represents the sum of both (red and blue) CDFs. The final result doesn't show a good improvement if compared with the case of only one CDF. I'd like to see a more precise fitting when using two CDFs instead of just one</p> <p>So if someone could help me, I'd be very grateful</p> <p>Cheers</p> https://mathematica.stackexchange.com/q/202477 2 Fitting two-dimensional data Vaggelis_Z https://mathematica.stackexchange.com/users/5052 2019-07-21T10:25:44Z 2019-07-21T11:25:04Z <p>For this question, I cannot use random sample data. So the actual data can be found <a href="http://www.mediafire.com/file/4hjs2rzch5dz3oo/L1.dat/file" rel="nofollow noreferrer">here</a>. The data file contains three columns, where the first two are the coordinates <span class="math-container">$(x,y)$</span>, while the third is the value of a function <span class="math-container">$f$</span>. Now we plot them, thus obtaining the shape of <span class="math-container">$f$</span></p> <pre><code>data = Import["L1.dat", "Table"]; </code></pre> <p>or </p> <pre><code>data = Import["https://pastebin.com/raw/YMCFB4mK", "TSV"] </code></pre> <p>Plot</p> <pre><code>L0 = ListPlot3D[data] </code></pre> <p><a href="https://i.stack.imgur.com/DJuY9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DJuY9.jpg" alt="enter image description here"></a></p> <p>My question is the following: is there a way to interpolate the data and obtain an analytical fitting function <span class="math-container">$f(x,y)$</span>? Taking into account that the distribution of <span class="math-container">$f$</span> is rather smooth, without peaks and holes, I suppose it should be rather easy to obtain its fitting function. Any ideas? </p> https://mathematica.stackexchange.com/q/202380 0 Fitting data points with a function obtained by ParametricNDSolve+Integrate dp87 https://mathematica.stackexchange.com/users/66608 2019-07-19T14:30:15Z 2019-07-19T14:30:15Z <p>My question concerns fitting data points to a function that is a solution of a set of parametric differential equations, then integrated over time. More specifically, I have these parametric differential equations:</p> <pre><code>picosec = 10^-12; ti = 0; (*initial time of the simulation*) tf = 12000*picosec; (*final time of the simulation*) Pump[t_, t0_, τ_] = (Sech[(t - t0)/τ])^2; (*Definition of the excitation pulse*) eqns = {xw'[t] == ηinj*(Peakpower*Pump[t, 1000 picosec, 10 picosec])/( 2.55*10^-19) - xw[t]/(10*picosec)*(1 - xp[t]/560.63) - xw[t]/ 10^-9 (*DIFFERENTIAL EQUATION 1*), xp'[t] == xw[t]/(10*picosec)*(1 - xp[t]/560.63) - 1.37*10^9*Gainfp*(2 yp[t]*(xp[t] - 280.315) + (xp[t])) - xp[t]/ 10^-9(*DIFFERENTIAL EQUATION 2*), yp'[t] == 1.37*10^9*Gainfp*(2 yp[t]*(xp[t] - 280.315) + (xp[t])) - yp[t]/( 4.07 picosec) (*DIFFERENTIAL EQUATION 3*), xw == 0, xp == 0, yp == 0 (*initial conditions*)}; solFIT = ParametricNDSolve[ eqns, {xw, xp, yp}, {t, ti, tf}, {ηinj, Gainfp, Peakpower},MaxSteps -&gt; Infinity, Method -&gt; "StiffnessSwitching"] </code></pre> <p>Now my experimental data is proportional to the integral of solution yp over time:</p> <pre><code>PhotonFunc[ηinj_,Gainfp_,Peakpower_,t_]={yp[ηinj,Gainfp,Peakpower][t]}/.solFIT; FittingFunction[ηinj_?NumericQ,ηcoll_?NumericQ,Gainfp_?NumericQ,Peakpower_?NumericQ]:=ηcoll/(4.07 picosec)*Integrate[PhotonFunc[ηinj,Gainfp,Peakpower,t],{t,ti,tf}]; </code></pre> <p>In other words, my fitting function has 3 free parameters: 2 of them are from the differential equation definitions (ηinj, Gainfp), and one introduced at the integration stage (ηcoll) to take into account the proportionality between data points and theoretical function. Peakpower represents the independent variable of the model, since every data point is taken for a specific value of Peakpower.</p> <p>My data is:</p> <pre><code> FITDATA = {{3.3*^-6, 3.171234*^-7}, {3.4736842105263158*^-6, 3.3900810000000006*^-7}, {3.774736842105263*^-6, 3.678039*^-7}, {4.052631578947368*^-6, 4.066959*^-7}, {4.388421052631579*^-6, 4.1602330000000005*^-7}, {4.677894736842105*^-6, 4.688141*^-7}, {5.025263157894737*^-6, 5.029277000000001*^-7}, {5.268421052631579*^-6, 5.396957000000001*^-7}, {5.789473684210527*^-6, 5.850883*^-7}, {6.0210526315789475*^-6, 6.178703000000001*^-7}, {6.368421052631579*^-6, 6.590776*^-7}, {6.854736842105263*^-6, 6.949222000000001*^-7}, {7.329473684210526*^-6, 7.842048*^-7}, {7.665263157894737*^-6, 8.33995*^-7}, {8.105263157894736*^-6, 8.631146000000001*^-7}, {8.336842105263159*^-6, 9.144675*^-7}, {9.031578947368421*^-6, 1.0331121000000002*^-6}, {9.436842105263158*^-6, 1.1031741*^-6}, {9.9*^-6, 1.1531064*^-6}, {0.000010536842105263158, 1.2948182*^-6}, {0.000010861052631578949, 1.3427578000000001*^-6}, {0.00001144, 1.4727559000000002*^-6}, {0.000012389473684210526, 1.6650955*^-6}, {0.000014473684210526315, 2.1499883*^-6}, {0.000016789473684210526, 2.7969681000000004*^-6}, {0.000020957894736842108, 3.6925992000000005*^-6}, {0.000022347368421052632, 3.9389504*^-6}, {0.000023736842105263157, 4.1618265000000005*^-6}, {0.000025821052631578948, 4.574431400000001*^-6}, {0.000026747368421052634, 4.7048833000000005*^-6}}; </code></pre> <p>To fit the data, I wrote the line:</p> <pre><code>NonlinearModelFit[FITDATA, {FittingFunction[ηinj,ηcoll, Gainfp, Peakpower], 0 &lt; ηinj &lt; 1, 0 &lt;ηcoll &lt; 1}, {{ηinj,0.6}, {ηcoll,0.000000008}, {Gainfp, 0.3}}, PeakPower, Method -&gt; "NMinimize"] </code></pre> <p>But it give me several error, e.g.:</p> <blockquote> <p>NonlinearModelFit::nrnum: The function value 1/2 ((-4.70488*10^-6+FittingFunction[0.6,8.*10^-9,0.3,Peakpower])^2+(-4.57443*10^-6+FittingFunction[0.6,8.*10^-9,0.3,Peakpower])^2+&lt;&lt;26>>+(-3.39008*10^-7+FittingFunction[0.6,8.*10^-9,0.3,Peakpower])^2+(-3.17123*10^-7+FittingFunction[0.6,8.*10^-9,0.3,Peakpower])^2) is not a real number at {[Eta]inj,[Eta]coll,Gainfp} = {0.6,8.*10^-9,0.3}. >></p> </blockquote> <p>I tried to change fitting method, i tried to use FindFit, but I did not get any fit. Sometimes the error is related to the evaluation of the integral (in this case Integrate[] expects numbers instead of symbols), and sometimes is related to complex values in the fit functions like the posted one. I tried to implement solution given in other posts with similar issues but they did not work.</p> <p>Do you know if there is a smarter way to approach such a problem?</p> <p>Thanks in advance</p>