[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: non-linear fitting , syntax-problem
From: |
Ben Abbott |
Subject: |
Re: non-linear fitting , syntax-problem |
Date: |
Fri, 12 Mar 2010 18:53:54 -0500 |
On Mar 12, 2010, at 5:29 PM, Stefan Neumann wrote:
> Hi,
>
> here is a working script using function leasqr() to fit a function to a
> data-set:
>
> filename = "tmp/non-lin.02.dat";
> fid = fopen (filename, "r");
> nc = 3 ; %Number of columns
> nr = 98 ; %Number of data points
> [val,count] = fscanf(fid,'%f %f %f',[nc,nr]); %Read matrix : x,y,z
> val = val'; %Transpose
> fclose (fid);
> x = val(1:nr,1:(nc-1)); %Independent variables
> y = val(1:nr,nc); %Dependent variable
> pin=ones(1,3); %There are 7 parameters
> F = inline(" p(1)*x(:,1) + p(2)*x(:,2) + p(3) ", "x", "p");
> [f,p,kvg,iter,corp,covp,covr,stdresid,Z,r2]=leasqr(x,y,pin,F);
>
> and here is one that doesn't work:
>
> filename = "tmp/non-lin.02.dat";
> fid = fopen (filename, "r");
> nc = 3 ; %Number of columns
> nr = 98 ; %Number of data points
> [val,count] = fscanf(fid,'%f %f %f',[nc,nr]); %Read matrix : x,y,z
> val = val'; %Transpose
> fclose (fid);
> x = val(1:nr,1:(nc-1)); %Independent variables
> y = val(1:nr,nc); %Dependent variable
> pin=ones(1,2); %There are 7 parameters
> % Function to fit
> F = inline(" p(1)*x(:,1)*x(:,2)+p(2) ", "x", "p");
> [f,p,kvg,iter,corp,covp,covr,stdresid,Z,r2]=leasqr(x,y,pin,F);
>
> The datafile 'tmp/non-lin.02.dat" contains 98 rows of 3 colums each, with
> column 3 (y) calculated from columns 1+2 (x1 and x2).
> (FYI: The script is adapted from the example here:
> http://knol.google.com/k/multidimensional-non-linear-least-squares-in-octave#
> )
>
> The first example assumes a linear function y = p1*x1 + p2*x2 + p3
>
> The second example assumes a non-linear function y = p1*x1*x2 + p2
>
> Obviously the second example does not work because of the multiplication of 2
> vectors x(:,1) * x(:,2)
>
> What is the correct syntax in this case, or how should I rewrite the script?
>
> TIA, stn
I haven't tried this with your example, but if I understand what you're trying
to do, just replace "x(:,1)*x(:,2)" with "x(:,1).*x(:,2)"
F = inline ("p(1)*x(:,1).*x(:,2)+p(2) ", "x", "p");
Ben