help-octave
[Top][All Lists]

## Re: Nonlinear fitting with lg(a+x)

 From: reposepuppy Subject: Re: Nonlinear fitting with lg(a+x) Date: Mon, 16 Mar 2009 03:21:45 -0700 (PDT)

```Sorry for late reply due to lack of time...
Thanks for the replies!

Jaroslav Hajek-2 wrote:
>
> On Fri, Mar 13, 2009 at 9:20 AM, reposepuppy <address@hidden>
> wrote:
>>
>> Hello,
>> I'm a college student who is attending a physical chemistry class now,
>> and
>> we're required to process the data obtained from experiments we've done
>> to
>> reach a plausible conclusion every week.
>> This time, I've got 2 columns of data, and one of them is the
>> concentration
>> of a solution [c], the other the surface tension of the solution [r]; the
>> task is to draw the curve representing the function of F(c,r)=0, and use
>> it
>> to draw the curve representing the function of F(c, dr/dc)=0 to reach
>> some
>> conclusionã€‚I found out on the Internet that there is a function between
>> them
>> stated as :
>>                             r=r0[1-a*lg(1+c/b)]
>> where r0 is the surface tension when there is no solute in the solution,
>> or
>> in other words, c=0; it could be measured during the experiment. The
>> constants a and b could be determined by making a nonlinear fitting using
>> the data I've got.
>> That's the question: I don't know how to transform this nonlinear
>> function
>> into a linear one; I could only make it (1-r/r0)=a*lg(b+c)-a*lg(b) and
>> using
>> the trial-and-error method(*See below) to find out what a and b are. But
>> the
>> error is too much for me(about 30% away from the theoretical one).
>>
>> Does anyone know how to solve this fitting problem using solely Octave?
>> Thanks for help!
>>
>
> fsolve is now capable of solving over-determined nonlinear systems -
> in other words, nonlinear fitting (with moderate residuals).
>
> function r = model (r0, a, b, c)
>   r = r0*(1 - a*log(1 + c/b));
> endfunction
>
> ... get c, r
>
> model_error = @(par) r - model (par(1), par(2), par(3), c);
> par0 = [... initial guesses for r0, a, b]
>
> par = fsolve (model_error, par0, ...options)
>
> It will be available in 3.2, so your options are
> 1. wait for 3.2 release
> 2. compile development sources
> 3. send me your data and I can make the fit for you
>
>
>> * The trial-and-error method I used is here, using Microsoft Excel (I
>> could
>> use Octave only to deal with some integration problem and linear
>> fitting...):
>> First, input the data into Excel and draw a XY scattered diagram, where
>> X=(1-r/r0) and Y=lg(b+c). A linear distribution of dots would appear if
>> the
>> value of b was right.
>> Second, calculate the R^2(the square of correlation coefficient) . It
>> would
>> be a value very close to 1 if the value of b was right.
>> Third, change the b value until I can't make the R^2 higher. First try
>> from
>> 0 to 10, and find out the optimal value, and minimize the scale by 10
>> fold
>> and try again... I tried from 0 to 2, and found out the R^2 will be the
>> biggest when b was 0.6; and I tried from 0.50 to 0.70 and I found out
>> it's
>> 0.59~0.62(the R^2 will not change any more during this range).
>
> These seem to be fairly good guesses. I think fsolve should not have
> problems refining them.
> For a more automatized method of obtaining initial guesses, you can
> sample expected ranges of
> parameters and pick the best match.
>
> cheers
>
> --
> RNDr. Jaroslav Hajek
> computing expert & GNU Octave developer
> Aeronautical Research and Test Institute (VZLU)
> Prague, Czech Republic
> url: www.highegg.matfyz.cz
> _______________________________________________
> Help-octave mailing list
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>
>

Thanks for the suggestions, but I guess I can't wait for the 3.2 version and
I can't even understand the codes you've written(because I'm really a newbie
in Octave...), while the function produced from fitting is
Still, I want to find out what the codes represents, and I can't find what
"@par" and the codes below mean:
par0 = [... initial guesses for r0, a, b]

par = fsolve (model_error, par0, ...options)
Could you make a brief explanation for me?

Michael Creel wrote:
>
> You don't need to linearize anything, just use nonlinear least squares to
> minimize the function
>
>      sum_over_observations [ r-r0[1-a*lg(1+c/b)] ]^2  with respect to the
> unknown constants.
>
> If you linearize and then estimate, the resulting estimator is biased and
> inconsistent. All you need to do is write Octave code to calculate the
> objective function. Then you can use one of the available minimizers to
> get the estimates. Octave provides sqp, which can handle this problem.
>

I can't find the function sum_over_observations... Is it a function I have
to make up by myself, or some packages submitted somewhere other than
octave-forge?
And..."All you need to do is write Octave code to calculate the objective
function."
It might sound silly, but I really don't know (and I don't know where I can
find the tutorial) how to calculate it, and how to use the minimizers. I've
read the octave.pdf file and the function reference on octave-forge, and I
could only found one minimizer called polyfit(x,y,n), which is used to make
polynomial fittings...
Could you give me a detailed demonstration/code to work it out? The data has
been collected in the attachment.
http://www.nabble.com/file/p22535164/data%2Bc%2Band%2Br.txt data+c+and+r.txt
--
View this message in context:
http://www.nabble.com/Nonlinear-fitting-with-lg%28a%2Bx%29-tp22492005p22535164.html
Sent from the Octave - General mailing list archive at Nabble.com.

```