# forum.alglib.net

ALGLIB forum
 It is currently Sat Sep 23, 2023 4:07 am

 All times are UTC

### Forum rules

1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: lsfitlinear error?Posted: Fri Apr 26, 2013 5:41 pm

Joined: Fri Apr 26, 2013 5:38 pm
Posts: 2
Why does the following fail to find a simple linear fit??

Code:
double[] y = new double[] { 6.95, 8.57, 10.3, 12.2, 14.0 };
double[,] x = new double[,] {{1},{2},{3},{4},{5}};

int info;
double[] c;
alglib.lsfitreport rep;
alglib.lsfitlinear(y, x, out info, out c, out rep);

//rep.r2 value equals 0.25
//c[0] value equals 3.16
//Expected result is 1.77 ?????

Regards,
MarcF

Top

 Post subject: Re: lsfitlinear error?Posted: Fri Apr 26, 2013 6:48 pm

Joined: Fri May 07, 2010 7:06 am
Posts: 891
Why do you think that it fails? lsfitlinear is a GENERAL least squares solver. In your formulation it solves problem of fitting experimental points by y(x)=c[0]*x - fitting without constant term, because you've specified only linear term in x matrix. And it fits data as good as possible - for c[0]=3.16 we have RSS (residual sum of squares) equal to 23.5, which is many times lower than RSS=129.8 for c[0]=1.77

If you want to perform linear fit by y(x)=c[0]+c[1]*x, you have to pass [[1,1],[1,2],[1,3],[1,4],[1,5]], where first item in each row correspond to constant term, second item - to linear term. Alternatively, you can use http://www.alglib.net/translator/man/manual.cpp.html#sub_polynomialfit to perform polynomial fitting.

Top

 Post subject: Re: lsfitlinear error?Posted: Fri Apr 26, 2013 10:30 pm

Joined: Fri Apr 26, 2013 5:38 pm
Posts: 2
thanks for the clarification, it makes sense now. I don't think this is particularly clear in the documentation though.

Marc

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for: