Hello
all,
I'm using octave v4.0.3 on Fedora 25. I have some data, call it xraw and yraw which follows a 1/x curve. I'm attempting to minimize a function yraw-yfit, so as to get the parameters 'p'. function y=fitcal(p) global xraw yraw; yfit=p(1)+1.0./(p(2)*xraw); y = sqrt(sum((yraw-yfit).^2)); end I'm attempting to use fminsearch, such that my call to the function is: global xraw yraw; ... sx=[100,0.00015]; xraw=TXI; yraw=RXI; [sx,fxval]=fminsearch(@(sx)fitcal(sx),sx); Nothing that I try changes sx; i.e. the fitter always returns the initial values, so I'm assuming that my fit is failing. I've successfully done something like this before with trig function. So I'm wondering if my error is in the fact that I cannot set bounds on p(2); namely p(2)!=0.0. Is there something else that I'm missing? Else, is there something in Octave which mimicks Matlab fmincon? Thank you, Pierrick --
----------------------------------------- Join us March 12-15 at CERN near Geneva Switzerland for OctConf 2018. More info: https://wiki.octave.org/OctConf_2018 ----------------------------------------- |
Hello Pierre,
Your formulation has some conditioning issues. Plot the raw data and your initial data together and you will probably see they are pretty close together. To fix your approach add some tolerance options to fminsearch and turn verbosity on: function y=fitcal(p) global xraw yraw; yfit=p(1)+1.0./(p(2)*xraw); y = sqrt(sum((yraw-yfit).^2)); end global xraw yraw; sx0=[100,0.00015]; xraw=sort(rand(100,1))*3; yraw=80 + (1/0.00013)./xraw; ops = optimset; ops.Display = 'iter'; ops.TolX = 1e-9; [sx,fxval]=fminsearch(@fitcal,sx0,ops); Note also that p(2) would probably do better in the numerator. A better approach would be to fit yraw and 1./xraw using the polyfit function: P = polyfit(1./xraw, yraw, 1); which give accurate estimates. Willem On 2018-03-13 11:00 AM, [hidden email] wrote: > I have some data, call it xraw and yraw which followsa 1/x curve. > I'm attemptingto minimizea function yraw-yfit, so as to get the > parameters 'p'. > > function y=fitcal(p) > global xraw yraw; > yfit=p(1)+1.0./(p(2)*xraw); > y = sqrt(sum((yraw-yfit).^2)); > end > > I'm attempting to use fminsearch, such that my call to the function is: > > global xraw yraw; > ... > sx=[100,0.00015]; > xraw=TXI; > yraw=RXI; > [sx,fxval]=fminsearch(@(sx)fitcal(sx),sx); > > Nothing that I try changes sx; i.e. the fitter always returns the initial > values, so I'm assuming that my fit is failing. ----------------------------------------- Join us March 12-15 at CERN near Geneva Switzerland for OctConf 2018. More info: https://wiki.octave.org/OctConf_2018 ----------------------------------------- |
Free forum by Nabble | Edit this page |