GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Feb-2009, 11:38
ultrAslan ultrAslan is offline
New Member
 
Join Date: Feb 2009
Location: Oh, USA
Posts: 22
ultrAslan has a little shameless behaviour in the past

Linear Regression


CPP / C++ / C Code:
#include <iostream>     
#include <cmath>        
#include <iomanip>     
using namespace std; 

int i,n,x[100],y[100];


int main() 
{ 

	do {
		cout <<"Number of points: ";
		cin >> n;
	} while (!(n > 1));

for(i = 0; i < n; i++)
	{
		cout << "Enter a value for x_" << i+1 << ": ";
		cin >> x[i];
		
		cout << "Enter a value for y_" << i+1 << ": ";
		cin >> y[i];
}


Now I need to calculate m and b....

m =

and

b=

How can I do that?
  #2  
Old 15-Feb-2009, 14:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linear Regression


Quote:
Originally Posted by ultrAslan
...
Now I need to calculate m and b....
...

It's done with loops.

I'll get you started:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
    int i, n; 
    double x[100], y[100];

    do {
        cout <<"Enter the number of points: ";
        cin >> n;
    } while (!(n > 1));


    for (i = 0; i < n; i++) {
        cout << "Enter a value for x_" << i + 1 << ": ";
        cin >> x[i];

        cout << "Enter a value for y_" << i + 1 << ": ";
        cin >> y[i];
    }

    double sumx = 0.0;
    for (int i = 0; i < n; i++) {
        sumx += x[i];
    }
    cout << "The sum of the x_i values is equal to " << sumx << endl;

    return 0;
}

Here's a run:
Code:
Enter the number of points: 3 Enter a value for x_1: 1.1 Enter a value for y_1: 2.2 Enter a value for x_2: 3.5 Enter a value for y_2: 6.4 Enter a value for x_3: 4.9 Enter a value for y_3: 10.3 The sum of the x_i values is equal to 9.5
Notes on a couple of changes I made:

You probably want to use floating point variables for your data points even if the inputs all have integer values since you will be dividing by stuff that may make the results to be non-integers. Unless every calculation in your program always has an integer result, I'm thinking it might be better to use floating point data throughout.

Gratuitous use of global variables is generally frowned on, so I made the declarations inside main(). There are lots of reasons for this even though it may not be apparent just now. My point is that acquiring "good" habits from the beginning may be less stressful than trying to break "bad" habits later.

Regards,

Dave
  #3  
Old 15-Feb-2009, 18:08
ultrAslan ultrAslan is offline
New Member
 
Join Date: Feb 2009
Location: Oh, USA
Posts: 22
ultrAslan has a little shameless behaviour in the past

Re: Linear Regression


Thanks, that helped a lot...
but I'm having problem with that code

CPP / C++ / C Code:
// beginning of code for repetitive inputs
	for(i = 1; i < n+1; i++) {
		cout << "Enter a value for x_" << i << ": ";
		cin >> x[i]; 
		
		cout << "Enter a value for y_" << i << ": ";
		cin >> y[i];
	}
	// end of code for repetitive inputs
	
	// begining sum calculations
	double sumx, sumy, sumxy, sumsqx = 0.0;
	for (int i = 1; i < n+1; i++) {
		// sum of x values
		sumx += x[i];
		// sum of y values
		sumy += y[i];
		// sum of x*y values
		sumxy += ( x[i] * y[i]);
		// sum of suquare of x values
		sumsqx += pow(x[i],2);
	}
	// end of sum calculations

	
	cout << sumx << endl;
	cout << sumy << endl;
	cout << sumxy << endl;
	cout << sumsqx << endl;
Number observations: 2
Please enter values of (x,y)
Enter a value for x_1: 3
Enter a value for y_1: 6
Enter a value for x_2: 9
Enter a value for y_2: 12
12
18
126
90


However the output should be like that
12
18
99
729
  #4  
Old 15-Feb-2009, 22:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linear Regression


Quote:
Originally Posted by ultrAslan

Number observations: 2
Please enter values of (x,y)
Enter a value for x_1: 3
Enter a value for y_1: 6
Enter a value for x_2: 9
Enter a value for y_2: 12
12
18
126
90
Well, in the first place we can't see from your printout what those numbers are supposed to mean,

I would probably make the print statements a little more verbose. Maybe something like
CPP / C++ / C Code:
    cout << "The sum of the  x  values is equal to " << sumx << endl;
    cout << "The sum of the  y  values is equal to " << sumy << endl;
    cout << "The sum of the x*y values is equal to " << sumxy << endl;
    cout << "The sum of the x^2 values is equal to " << sumsqx << endl;

Then the output might look like
Code:
Enter the number of points: 2 Enter a value for x_1: 3 Enter a value for y_1: 6 Enter a value for x_2: 9 Enter a value for y_2: 12 The sum of the x values is equal to 12 The sum of the y values is equal to 18 The sum of the x*y values is equal to 126 The sum of the x^2 values is equal to 90

If there is any question about what numbers were used to arrive at those numbers, I might print out the numbers themselves.

Now: are these the correct values for the sums?

Hmmm... According to my abacus:

Sum of x values = 3+9 = 12 (check)
Sum of y values = 6+12 = 18 (check)
Sum of x*y values = 3*6+9*12 = 18+108 = 126 (check)
Sum of x*x values = 3*3+9*9 = 9+81 = 90 (check)

So I think the printout shows the correct values.

However...

There is some undefined behavior with your program. See footnote.

CPP / C++ / C Code:
    double sumx, sumy, sumxy, sumsqx = 0.0;

This statement guarantees that the initial value of sumsqx is equal to zero, but does not initialize the others. Apparently your compiler helpfully sets them to zero, but that is not guaranteed for other compilers (or, maybe not even for other programs using this statement with the same compiler or with later versions of the same compiler).

To guarantee the results, Initialize each one separately.

You can do it like this:
CPP / C++ / C Code:
    double sumx = 0.0, sumy = 0.0, sumxy = 0, sumsqx = 0.0;

Or, some people prefer to do each one on its own line since humans who want to check the code might be less likely to overlook a mistake like yours;
CPP / C++ / C Code:
    double sumx   = 0.0;
    double sumy   = 0.0;
    double sumxy  = 0.0;
    double sumsqx = 0.0;


Now, as a matter of style, arrays in C and C++ start with index zero, not 1. I mean there's nothing "wrong" with using x[1], x[2], y[1] and y[2], but I think it might be better to get used to the zero-based arrays. Wasting an element in each array (not using x[0] or y[0]) might not seem important for a trivial program like this, but it's just not the usual style.

So the loops might look like
CPP / C++ / C Code:
    for (i = 0; i < n; i++) {
        // do stuff like store values in x[i] and y[i]
    }

The fact that arrays are zero-based is probably the reason that the most common idiom in C and C++ programming for doing something "n" times is
CPP / C++ / C Code:
    for (i = 0; i < n; i++) {
    }

Regards,

Dave


One kind of undefined behavior is using variables that weren't initialized by the program. Some compilers always initialize variables to zero, some do not. Bottom line: don't count on it! (A little digital pun. Get it? Don't count... No? Oh, well)

The main implication:
Because of the possibility of undefined behavior: In general, you can't prove that a program is correct by testing alone.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MPI with C (Solving Linear Equation using Gauss-Seidel Iteration) trainz13 C Programming Language 7 01-May-2007 22:38
Linear Search eccoflame C Programming Language 3 19-Apr-2005 08:36
C++ Linear Regression with Outliers bartb C++ Forum 1 06-Mar-2005 08:27
Ineed some help with thos code - Linear Linked List sosy2001 C Programming Language 6 11-Nov-2004 10:23
Linear search on a multidimensional array. anignna C++ Forum 4 07-Mar-2004 20:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:27.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.