f

#### Best least-squares line fit

```I copied the best least squares line fit algorithm from Graphics Gems V book,
but now i'm having problems with vertical lines, although the book claims that
the algorithm can handle vertical lines.

For example using 0,0 and 0,2 as test points gives a=0 and b=0 and c=0 for the
line ax + by = c, although it should be something like a=1, b=0, c=0.

Here is my code:

void best_fit(void)
{
if(n_points<2)
{
a=1; // dummy values
b=1;
c=0;
return;
}

double sx=0;
double sy=0;
double sxx=0;
double syy=0;
double sxy=0;

for(point *p=points.first(); p; p=p->next)
{
sx += p->x;
sy += p->y;
}
double xmid = sx/n_points;
double ymid = sy/n_points;

for(p=points.first(); p; p=p->next)
{
double x1 = p->x-xmid;
double y1 = p->y-ymid;
sxx += x1*x1;
syy += y1*y1;
sxy += x1*y1;
}
double a1 = sxx-syy;
double b1 = sxy;
a = 2*b1;
b = - (a1 + sqrt(a1*a1 + 4*b1*b1));
c = - a*xmid - b*ymid;

if(a==0.0 && b==0.0) // try to solve the vertical line problem
{
a=1; c=-xmid;
}
}

```
 0
ukram (3)
11/18/2003 8:34:45 PM
comp.graphics.algorithms 6674 articles. 1 followers.

1 Replies
547 Views

Similar Articles

[PageSpeed] 58

```"Markku Poysti" <ukram@puosu.dna.fi_nospam> wrote in message
news:3fba8264\$0\$6509\$1b6aedd2@news.songnet.fi...
> I copied the best least squares line fit algorithm from Graphics Gems V
book,
> but now i'm having problems with vertical lines, although the book claims
that
> the algorithm can handle vertical lines.
<snip>

The derivation has problems.  For example, consider equation (3)
which has x*sin(theta)+y*cos(theta)+p = 0.  Figure 1 shows what
p and theta mean.  Now choose p = 0 and theta = 45 degrees.
The figure indicates a line y = x.  The equation says x + y = 0.
Does this give you warm fuzzies about the rest of the construction?

Let t = ((sxx+syy)-sqrt((sxx-syy)*(sxx-syy)+4*sxy*sxy))/2.
The least squares line contains the average (xmid,ymid) of the
points (using your notation).  The direction is chosen to be
either (-sxy, t-syy) or (t-sxx, -sxy), whichever has larger length.
(The GG5 construction essentially chose the wrong direction
that, as you noticed, was (0,0).)

Grundgy details in
http://www.magic-software.com/Documentation/LeastSquaresFitting.pdf

--
Dave Eberly
eberly@magic-software.com
http://www.magic-software.com
http://www.wild-magic.com

```
 0
eberly (231)
11/18/2003 9:41:23 PM

Similar Artilces:

best fit (least squares) of sphere
Hello, I have point cloud data that is in t,x,y,z format of a hemispherical surface. What I need to be able to do is calculate the center point and radius of the best fit sphere. Unfrotunately, I can not simple calculate the distance from a global origin. Anyone have any suggestions? Thanks. ...

Re: best fit (least squares) of sphere
On Fri, 6 Mar 2009 13:57:01 +0000 (UTC), Michael wrote: > Hello, > > I have point cloud data that is in t,x,y,z format of a hemispherical surface. What I need to be able to do is calculate the center point and radius of the best fit sphere. Unfrotunately, I can not simple calculate the distance from a global origin. > > Anyone have any suggestions? Write a functional which have to be minimised in least square sense. Then traditional methods (which rely on function derivation) can be used. doert <no_mail@mail.com> wrote in message <csmkmmp5fbso.13dwb7jdow8xv.dlg@...

get endpoints of line after least squares fitting
hi guys, I have some points on an image in pixels (u,v). I fit a line through it using least squares fit. But having trouble in determining the starting and endpoints of the line. can you guys suggest any possible way I should go about it? thanks a lot -- aiden I forgot to include my code so far : %------------------------------------------------------------------ function [a0 a1] =Linear_Regression(x,y) n = length(x); a1 = (n*sum(x.*y) - sum(x)*sum(y)) /(n*sum(x.^2)-(sum(x))^2); a0 = mean(y) -a1*mean(x); end %-------------------------------------------------------------...

help with least squares line fitting through points...
I am attempting to do a least squares fit of a line through a set of data points (and plot the line and points), and opted to use polyfit/polyval instead of the "\" operator. I thought this would be rather easy, but when I use the code on the following data, the result is not as I expected. Can anyone run the following, and point out my error (the real code is just a couple of lines...)? Thanks. -Steve close all clear all data=[ 64.7100 56.8700 64.6701 56.2000 64.6200 55.5300 64.5300 54.1800 64.3800 53.3600 64.4400 52.8400 64.4100 52.6800 64.4...

Least-squares fitting a square pulse
I am trying to fit a square pulse to data in the least-squares sense in Matlab. The parameters that are being optimized are the height, start, and end - x(1), x(2) and x(3) in the program, respectively. However, the least-squares program is only varying the height, for some reason. Why is this occurring? How else could I fit a square pulse using least-squares? Although in this example the pulse would be easy to fit by inspection, for the purpose of my project, it is not an option. Code: %FITTING xdata=0:0.1:10; ydata=-.05+.1*rand(1,numel(xdata))+heaviside1(xdata-1)-heaviside1(xdata-4);...

Help with orthogonal least squares line fit with slope constraint...
If I have a group of points, I know I can do a least-squares line fit in the orthogonal sense by doing an eigenvector-type approach. The code I've been using for this is: Xm = mean(X); A = cov(X); [V,D] = eig(A); [ignore,imx] = max(diag(D)); % Choose eigenvector with max. eigenvalue t=4; x = Xm + V(:,imx)'*t; t=-t; x2 = Xm + V(:,imx)'*t; slope=(x2(2)-x(2))/(x2(1)-x(1)); b=((x2(2)+x(2))-(slope.*(x(1)+x2(1))))./2; Now, I am looking to do a best fit line through the data, again in an (orthogonal) least-squares sense, but this time where the s...

Best fit line for an exponential line
Hi, I am new to using GNUPLOT and am trying to find the best fit line (to determine the slope) of a line that follows Power Law (is an exponential line). I realize I have to use a function f(x) = a*b**x. But, I do not know what to do next. Any help in this matter would be greatly appreciated. Thanks, Divya Divya Rao <divyarao09@yahoo.com> wrote: > Hi, > I am new to using GNUPLOT and am trying to find the best fit line (to > determine the slope) of a line that follows Power Law (is an > exponential line). First, a terminology issue: an exponential curve doesn't have a "slope" to speak of. More precisely, the slope is different at every place, so it's not a property of the curve as such, but of each point on the curve. > I realize I have to use a function f(x) = a*b**x. That's one way of expressing it. f(x)=a*exp(x/k) or f(x)=exp(b1*(x-x0)) and any variations of those may be just as valid choices. > But, I do not know what to do next. Any help in this matter would be > greatly appreciated. Read through "help fit" and you should be on your way. -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain. On Tuesday 04 May 2004 12:53, Divya Rao (divyarao09@yahoo.com) held forth in comp.graphics.apps.gnuplot (<69dbc103.0405040853.25260afb@posting.google.com>): > Hi, > I am new to using GNUPLOT and am try...

Adding constraints to linear fit parameters of separable least squares fit
I was wondering if anyone can help me to figure out how to add equality and inequality constraints to the linear parts of a separable least squares fit. I saw fminspleas is able to do it for the nonlinear fit parameters (using transformations from a constrained problem to an unconstrained one), but not for the linear fit parameters. Here is my code for the separable least squares fit. Thanks. function[NLP,LP,res]=seplstsq(funlist,start_point,xdata,ydata,options) function [sse,LP,res]=seplstsq_sse(NLP) DM=zeros(length(ydata),length(funlist)); for ii=1:length(funlist); f...

least square fit
Hi I have many file with extension (.dat). I need to open them in IDL and have least square fit. Is any one have idea on who to that. omerphysics@gmail.com writes: > I have many file with extension (.dat). I need to open them in IDL and > have least square fit. > Is any one have idea on who to that. Well, rest assured, the code will be less than 10 lines long. But, *which* 10 lines will depend on more details. What do you know about this problem? You can find a lot of hints at the web page below. Cheers, David -- David Fanning, Ph.D. Fanning Software Consulting, Inc. Coyote'...

least squares algorithm
Hello All! Any one know where can I find some php code (or "php-able" code) implementing least squares method? Thx in advance and pls excuse my poor eng J.SanTanA schreef: > Hello All! > Any one know where can I find some php code (or "php-able" code) > implementing least squares method? > > Thx in advance and pls excuse my poor eng http://www.google.nl/search?hl=nl&q=php+least+squares JW On 13 Feb, 12:42, Janwillem Borleffs <j...@jwscripts.com> wrote: > J.SanTanA schreef: > > > Hello All! > > Any one know where can I find s...

Best Fit Line
Hello, I have a problem that is puzzling me for very long.I got this problem of fitting in 80 over text files and within each txt file,it contains about 120 datas.I want to from these datas generate a best fit line to cut through all these points.The program I am now using is Matlab.Please help and reply asap,as I need the info urgently. Thanks. In article <a488ubsdofj7@legacy>, kellyblue19@yahoo.com (Eid) writes: >Hello, > I have a problem that is puzzling me for very long.I got this >problem of fitting in 80 over text files and within each t...

Line of best fit
I'm a total newbie at this guys. I have the following data in an .m file: x = 0:2:20; y = [0.02 0.26 0.35 0.53 0.66 0.78 0.88 0.96 1.03 1.06 1.14]; plot(x,y,'rx'); How would i fit a line of best fit to this graph? I know this is rudimentary stuff but as i said i'm a total newbie!!! Cheers for any help help polyfit /Johan Jamie Sutton wrote: > I'm a total newbie at this guys. > I have the following data in an .m file: > > x = 0:2:20; > y = [0.02 0.26 0.35 0.53 0.66 0.78 0.88 0.96 1.03 1.06 1.14]; > plot(x,y,'rx'); > > How would i fit...

Least square fitting
Hi, I have this graph: x = [0.000235 0.00025 0.0003 0.0004 0.0005 0.0006 0.00075] y = [54.9 43.1 62.7 86.3 86.3 90.2 96.1] plot (x, y) xlabel ('Amplitude') ylabel ('Correct Detection [%]') legend ('Psychometric Function') and I want to fit it to this equation: P(A, 'theta') = [1/sqrt(2*pi*sigma²)] * integral e^-[(A-'theta'²)/2*sigma²] how can i do this? thank you. "xety89 " <sow.chan@warwick.ac.uk> wrote in message <hqouqh\$snf\$1@fred.mathworks.com>... > Hi, > > I have this graph: > > x = [0.000235 0.00025 0.0003 0.0004 0.0005 0.0006 0.00075] > y = [54.9 43.1 62.7 86.3 86.3 90.2 96.1] > > plot (x, y) > xlabel ('Amplitude') > ylabel ('Correct Detection [%]') > legend ('Psychometric Function') > > and I want to fit it to this equation: > > P(A, 'theta') = [1/sqrt(2*pi*sigma²)] * integral e^-[(A-'theta'²)/2*sigma²] > > how can i do this? Hmm. I have a funny feeling that your expression is wrong. Everything about that expression is a simple, cumulative normal, except for that you square theta, as opposed to squaring the difference (A-theta). Beyond that, your expression is not meaningful in terms of the data that you have. You are talking about doing some sort of a nonlinear least squares fit to that expression, but that expression cannot ...

Least square fitting
Hi all, I have a problem with a rather complicated function depending on four parameters which I try to find using least square fitting and I don't know exactly how to do it. The basic problem is the following: I have an astronomical image of a star field and try to relate the sky coordinates (right ascension, declination) of the stars to the pixel coordinates (x, y). The function to relate the two depends on the not accurately known parameters focal length of the lens (f), the rotation of the field of view with respect of the north direction (beta) and the center sky coordinates of the...

Line of best fit
I am new to python so apologies for the ignorance with this question. How would I apply a line of best fit to a plot? My data are netCDF4 data files and this is essentially what I have done so far: swh1=netCDF4.Dataset('filename','r') hs1=swh1.variables['hs'] swh2=netCDF4.Dataset('filename'.'r') hs2=swh2.variables['hs'] plt.plot(hs1,hs2,'.') Cheers, Jamie --001a113a6020e6901104f5e5a9f1 Content-Type: text/plain; charset=UTF-8 None of these are in the standard library but why re-invent the wheel? Using numpy:...

Least squares fitting
Hello everybody, I am working on an inverse problem where I need to use least squares fitting. I am working on characterization of impedance of a piezoelectric crystal. A PZT crystal can be modeled as a series RLC circuit in parallel with a parasitic capacitor. Thus, I can derive the transfer function of its impedance and admittance and simulate it (see the code below) I am trying to extract R,L,C and C0 parameters from this simulated admittance curves using least squares fitting method.( Also, I need to inversely calculate these from the admittance curves that are constructed using ...

least squares fit
I'm trying to solve a system of 5+ equations for 3 unknowns, preferably using the least-squares method. I've Solve[] will not return solutions for more equations than you have unknowns. Fit[] does not work because my equations have 4 sets of data. Si=a*Ui + b*Vi + c*Wi, where i goes from 1 to 5 U, V, and W all come from a table, and S is from experimental data. I need to know how to solve for a,b,c given more than 3 equations. Background: The problem is actually trying to solve for Judd-Ofelt parameters of a laser crystal. S is the experimental line strength and U, V...

Least Square Fit
I tried to use lsqlin to do a least square fit with 3 constraints. I had 7 variables. However it didn't seem to take into account the last 2 cosntraints. I am trying to use fmincon but keep getting an error 'Inner matrix dimensions must agree'- I checked the dimensions of the constraint and Obj function matrices.but Ican't figure out why this is happening. Help! In article <eee31fe.-1@webx.raydaftYaTP>, "Shar P" <sprabhu@ieor.berkeley.edu> writes: >I tried to use lsqlin to do a least square fit with 3 constraints. I >had 7 variables. However it ...

Best fit line?
Hi. Is there a function to calculate/plot the best fit line given a few (approx 3) points? I don't have any toolboxes, nor can I afford any. Can any function in the basic matlab application do this? Thanks! "Amir " <amirschricker.DELETE@DELETE.gmail.DELETE.com> wrote in message <g4o8va\$aa7\$1@fred.mathworks.com>... > Hi. Is there a function to calculate/plot the best fit line > given a few (approx 3) points? I don't have any toolboxes, > nor can I afford any. > > Can any function in the basic matlab application do this? Yes. help polyfit J...

least square fitting
dear Gnuplot users, I am using the following command to fit a set of data: f(x)=a*x+b; fit f(x) 'dataset' via a,b then I get the value of a and b with only one decimal digit like a=0.1, b=0.9 how can I get five decimal digits like a=0.00001, b=0.91112 ? Thanks David <dagao@exchange.uta.edu> wrote: > dear Gnuplot users, > I am using the following command to fit a set of data: > f(x)=a*x+b; > fit f(x) 'dataset' via a,b > then I get the value of a and b with only one decimal digit like a=0.1, > b=0.9 "get the value" --- where? All the outputs of 'fit' I'm aware of print with a good deal more than 1 significant digit. Well, unless all the others following it *are* exactly zero, that is. -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain. Below is the copy: Final set of parameters Asymptotic Standard Error ======================= ========================== a = 0.0751221 +/- 0.005317 (7.078%) b = -29512.8 +/- 0.001603 (5.432e-06%) correlation matrix of the fit parameters: a b a 1.000 b -0.905 1.000 you can notice b= -29512.8 only has one decimal value. How to change it to have more than 1 decimal digit? Thanks David <dagao@exchange.uta.edu> wrote: > Below is the c...

least square fit
I have the following data file: http://www.cs.kuleuven.ac.be/~bartv/gnuplot/sobol.dat Now I want to do a least square fit on the data of the first and second column. The data in the first-column represent the x-values, the data in the second column represent the y-values. I don't need the rest of the columns. Now I now that x and y are related somehow as: y = a/(x**b) and I want to find a and b as accurate as possible. The file that plots my data to an .eps file is at http://www.cs.kuleuven.ac.be/~bartv/gnuplot/relative_error.pl If I don't specify an initial value for b_sob, the fit fails. If I do specify a starting value of 0.9 the fit works, but the result is not satisfying as can be seen from the .eps file at http://www.cs.kuleuven.ac.be/~bartv/gnuplot/relative_error.eps I have also tried to fit on log(\$2)=a-b*log(\$1) and then plot exp(a)/(x**b) but this didn't give me satisfying results either. How can I increase the quality of my fit? Regards, Bart -- "Share what you know. Learn what you don't." Bart Vandewoestyne wrote: > I have the following data file: > > http://www.cs.kuleuven.ac.be/~bartv/gnuplot/sobol.dat > > Now I want to do a least square fit on the data of the first and > second column. The data in the first-column represent the x-values, the > data in the second column represent the y-values. I don't need the rest > of the columns. > &...

least square curve fit
I have an equation of y=k/a(1 -exp(-at)),and a set of data y=[1 2....];t=[2 3...]. is it possible to find the value of k and a by least square curve fitting? You could use something like: <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=138&objectType=file> annie wrote: > > > I have an equation of y=k/a(1 -exp(-at)),and a set of data y=[1 > 2....];t=[2 3...]. is it possible to find the value of k and a by > least square curve fitting? In article <eeec072.-1@webx.raydaftYaTP>, annie <annie_e2004@yahoo.com> writes: >I have an...

Weighted Line of Best Fit
At the moment I am writing code to find the weighted line of best fit. I can calculate the line of best fit using the code given below, where sortWLAN corresponds to the entries in an array, which I want to extract. onedist = sortWLAN(3,1:100); onerssi = sortWLAN(4,1:100); onewgt = sortWLAN(5,1:100); coeffs = polyfit(onedist,onerssi, coefficient); curvefit = polyval(coeffs, distance); hold on plot(onedist, onerssi, 'o'); plot(distance,curvefit, 'r'); hold off Anybode have any ideas? Cheers Brad wrote: > At the moment I am writing code to find the weighted line of best &...

nonlinear best-fit line
Good afternoon, I am having trouble trying to figure out how to fit a nonlinear best fit line to my data in SAS. Any help would be greatly appreciated. Regards, Ligia ...

Web resources about - Best least-squares line fit - comp.graphics.algorithms

Least squares - Wikipedia, the free encyclopedia
The method of least squares is a standard approach to the approximate solution of overdetermined systems , i.e., sets of equations in which there ...

Least Squares for iPhone, iPod touch, and iPad on the iTunes App Store
Get Least Squares on the App Store. See screenshots and ratings, and read customer reviews.

How The Witcher 3’s economy was saved by polynomial least squares
For a game as complex and as huge as The Witcher 3 , it's hard to imagine that one of its core gameplay elements—one that ties the entire game ...

Resources last updated: 3/7/2016 8:29:25 PM