[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Matching Equation to Data
From: |
Thomas D. Dean |
Subject: |
Re: Matching Equation to Data |
Date: |
Fri, 15 Mar 2013 15:05:46 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3 |
On 03/15/13 04:25, Juan Pablo Carbajal wrote:
Almost all optimizers required code that looks more or less like this:
Assume "data" has your (x,y,z,1) points, one **column** per point.
Assume p is the vector of parameters to find, is given as a 1x4 row vector.
The following function gives the total square error:
function rhs = myfunc (p, data)
rhs = sum((p*data).^2);
endfunction
When you call the optimizer you create a handle to that function and
pass the data at construction time (you can also use global variables
or even persistent variables with a load command)
errfun = @(p) myfun(p,mydata)
You will have to provide a firts guess p0 and call the optimizer of
your choice like
p = optimizer (errfun, p0)
Now, since you are doing linear regression on the data I would
recommend to look at functions regress and regress_gp in the
statistics package.
I have been looking at this wrong. I want to fit an equation to data.
Equation of plane: a*x + b*y + c*z + d = 0
data: n rows of [et, x, y, z]
x, y, and, z depend on et
Y = data(:,2:3) <== fit a*x + b*y + c*z + d = 0 to these points
X = data(:,1) <== not applicable to my situation
Criteria: the distance from each point of data to the plane is minimal.
dist = ptopl(Y,guess), where Y is nx4 and guess is 1x4
I want a least squares fit, so
function [y]=ptopl_sq(x,p)
abc=p(1:3); ## check shape
if (size(abc) == [3,1])
abc=abc';
endif;
y=sum(((x*p').^2)/(abc*abc'));
endfunction;
Y=[A(:,2:4),ones(size(A,1),1)];
and Y*[a,b,c,d]' == 0 if I have an exact fit
I tried
Y=[A(:,2:4),ones(size(A,1),1)];
x=1:size(Y,1);
init=[1,1,1];
p=fmins('ptopl_sq',init, [], [], Y, init);
and, Y*p' has values on the order of mean(Y)!
Tom Dean