help-octave
[Top][All Lists]

## Curve fitting

 From: lionel Subject: Curve fitting Date: Sat, 30 Jul 2011 04:36:10 -0700 (PDT)

```Hi all,

I am interested in curve fitting techniques in Octave but there is little
information around to tell how to do, so I propose this tread to regroup the
different techniques available. I can start with one that I have developed
http://octave.1599824.n4.nabble.com/Curve-fit-td3051324.html
They give an interesting way to program curve fitting, but its is a bit
unconvenient to define the fitting function every time you need it. It also
add more probability for copy errors and makes it harder to maintain. So my
idea is to use nested functions so that you can easily change it.

First you will need to define a function for your code so that you can use
nested functions to define your fit function.

Here is a general draft script, any suggestion to make it better, or
propositions for other methods are welcome.

function c = fit_exemple

x=[ ]; % copy your x-data here or take it from the thread above
y= []; % same for y

% here you give the initial guess for the fit. This may be done better
depening on the model
b0 = 3;
a0 = 1/10;
c0 = 1/500;
c_opt = [a0, b0,c0]; % regroup the guesses
tol = 1e-5; % this is not used yet, its just a draft

[c, fval, info, output] =  fsolve (@(c) (infunc(c,x)  - y),c_opt); % here
you use the nested function defined at the end of the script
y2 =  infunc(c,x); % now you can create the fitted curve
plot(x,y2,'linewidth',2,'r',x,y,'+','markersize',3,'linewidth',2) % and
plot

function p = infunc(c,x)  % here is the nested function. If you want to
change the model, do it here.
p =(1-exp(-c(1)*x))* c(2).*exp(-c(3)*x) ;
end

end

--
View this message in context:
http://octave.1599824.n4.nabble.com/Curve-fitting-tp3706182p3706182.html
Sent from the Octave - General mailing list archive at Nabble.com.

```