The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 how to write a linear fitting function ?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

70WorkRoom

China
Posts

Posted - 07/22/2007 :  01:25:32 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): Origin v7.5
Operating System:Windows 2000

when i write a test linear fit arithmetic to make a line:Y = kX + b.
but the result is always different from the origin linear fit.may be i miss something in my programe,can someone help me?Thanks very much.

My Code is as follow:
 
#include <stdio.h>

#define MAXNODE 24

double sum1(double a[],int n)
{
double y=0; int i;
for(i=0;i<n;i++)
y+=a[i];
return(y);
}
double sum2(double a[],int n)
{
double y=0; int i;
for(i=0;i<n;i++)
y+=a[i]*a[i];
return(y);
}
double sum3(double a[], double b[],int n)
{
double y=0; int i;
for(i=0;i<n;i++)
y+=a[i]*b[i];
return(y);
}
int main()
{
double a,b;
int N = 24;

double x[24] = {0.00186,
0.00185,
0.00184,
0.00184,
0.00183,
0.00182,
0.00181,
0.0018,
0.00179,
0.00179,
0.00178,
0.00177,
0.00176,
0.00176,
0.00175,
0.00174,
0.00173,
0.00172,
0.00172,
0.00171,
0.0017,
0.0017,
0.00169,
0.00168};

double y[24] = {-13.71611,
-13.6033,
-13.58863,
-13.48522,
-13.47886,
-13.39108,
-13.33716,
-13.27675,
-13.23764,
-13.16014,
-13.0996,
-13.05467,
-12.97989,
-12.95996,
-12.92921,
-12.84708,
-12.83526,
-12.77366,
-12.68855,
-12.66473,
-12.65777,
-12.60471,
-12.5354,
-12.45963};

a=(N*sum3(x,y,N)-sum1(x,N)*sum1(y,N))/(N*sum2(x,N)-sum1(x,N)*sum1(x,N));
b=(sum1(y,N)*sum2(x,N)-sum1(x,N)*sum3(x,y,N))/(N*sum2(x,N)-sum1(x,N)*sum1(x,N));

printf("Result is:\n y=%f x + %f\n",a,b);
return 0;
}



my result is:y=-6694.250717 x + -1.219209

And the origin result log is:
[2007-7-22 12:19 "/Graph1" (2454303)]
Linear Regression for Data3_B:
Y = A + B * X

Parameter Value Error
------------------------------------------------------------
A -1.27315 0.1525
B -6662.66738 86.18488
------------------------------------------------------------

R SD N P
------------------------------------------------------------
-0.99816 0.02291 24 <0.0001


Edited by - 70WorkRoom on 07/22/2007 01:31:33 AM

Mike Buess

USA
3037 Posts

Posted - 07/22/2007 :  10:11:58 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I'm not sure what you did but after copying your data to worksheet both the Analysis> Fit Linear and Tools>Linear Fit commands give me the Result Log report shown at the bottom. Note that slope and intercept are same as those obtained from your formula. This is how Origin does its linear regression...
http://www.originlab.com/www/helponline/origin/Linear_Regression.htm



Mike Buess
Origin WebRing Member
Go to Top of Page

70WorkRoom

China
Posts

Posted - 07/30/2007 :  05:16:13 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
thank you very much.
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000