PDA

View Full Version : سوال: مشکل در برنامه رگرسیون خطی



Nomad987
دوشنبه 21 دی 1388, 12:27 عصر
سلام دوستان .
یه مشکل دیگه با کامپایل این برنامه دارم.
سورس این برنامه رو از سایت
https://www.codecogs.com/d-ox/maths/regression/linear.php
گرفتم.]
توضیحات کامل رو تو سایت داده. با توجه به توضیحات پیش رفتم.ولی بازم به مشکل بر خوردم و نتوستم برنامه رو اجرا کنم. ممنون میشم مرحله به مرحله توضیح بدین که چیکار باید بکنم.
اینم کدهای برنامه


#ifndef MATHS_REGRESSION_LINEAR_H
#define MATHS_REGRESSION_LINEAR_H

#include <assert.h>
#include <math.h>

namespace Maths
{

namespace Regression
{

//! Given a set of points, this class calculates the linear regression parameters and evaluates the regression line at arbitrary abscissas.

class Linear
{
public:

//! Class constructor

Linear(int n, double *x, double *y)
{

// calculate the averages of arrays x and y
double xa = 0, ya = 0;
for (int i = 0; i < n; i++) {
xa += x[i];
ya += y[i];
}
xa /= n;
ya /= n;

// calculate auxiliary sums
double xx = 0, yy = 0, xy = 0;
for (int i = 0; i < n; i++) {
double tmpx = x[i] - xa, tmpy = y[i] - ya;
xx += tmpx * tmpx;
yy += tmpy * tmpy;
xy += tmpx * tmpy;
}

// calculate regression line parameters

// make sure slope is not infinite
assert(fabs(xx) != 0);

m_b = xy / xx;
m_a = ya - m_b * xa;
m_coeff = (fabs(yy) == 0) ? 1 : xy / sqrt(xx * yy);

}

//! Evaluates the linear regression function at the given abscissa.

double getValue(double x)
{
return m_a + m_b * x;
}

//! Returns the slope of the regression line
double getSlope()
{
return m_b;
}

//! Returns the intercept on the Y axis of the regression line
double getIntercept()
{
return m_a;
}

//! Returns the linear regression coefficient

double getCoefficient()
{
return m_coeff;
}

private:

double m_a, m_b, m_coeff;
};


//! A static function implementing the Linear Class for one off calculations

double Linear_once(int n, double *x, double *y, double a )
{
// This function is created to enable an Instant Calculator on CodeCogs.
// You probably shouldn't be using this function otherwise.

Maths::Regression::Linear A(n, x, y);
return A.getValue(a);
}

}

}

#endif

#include <iostream.h>
#include <iomanip.h>
using namespace std;

int main()
{
double x[7] = { 1.5, 2.4, 3.2, 4.8, 5.0, 7.0, 8.43 };
double y[7] = { 3.5, 5.3, 7.7, 6.2, 11.0, 9.5, 10.27 };

Maths::Regression::Linear A(7, x, y);

cout << " Slope = " << A.getSlope() << endl;
cout << "Intercept = " << A.getIntercept() << endl << endl;

cout << "Regression coefficient = " << A.getCoefficient() << endl;

cout << endl << "Regression line values" << endl << endl;
for (double i = 0.0; i <= 3; i += 0.6)
{
cout << "x = " << setw(3) << i << " y = " << A.getValue(i);
cout << endl;
}
return 0;

}


و مثال اجرایی برنامه:


#include <codecogs/maths/regression/linear.h>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
double x[7] = { 1.5, 2.4, 3.2, 4.8, 5.0, 7.0, 8.43 };
double y[7] = { 3.5, 5.3, 7.7, 6.2, 11.0, 9.5, 10.27 };

Maths::Regression::Linear A(7, x, y);

cout << " Slope = " << A.getSlope() << endl;
cout << "Intercept = " << A.getIntercept() << endl << endl;

cout << "Regression coefficient = " << A.getCoefficient() << endl;

cout << endl << "Regression line values" << endl << endl;
for (double i = 0.0; i <= 3; i += 0.6)
{
cout << "x = " << setw(3) << i << " y = " << A.getValue(i);
cout << endl;
}
return 0;

}

ممنون میشم راهنمایی کنید

nofilter
جمعه 28 خرداد 1389, 10:20 صبح
کسی نمی تونه مشکل این برنامه رو حل کنه ؟

majid_802
شنبه 17 دی 1390, 13:11 عصر
سلام دوست عزیز با چه نرم افزاری کامپایل کردی؟

b.saminjad
شنبه 17 دی 1390, 14:34 عصر
سلام
کد صحیحو زیر قرار دادم


#ifndef MATHS_REGRESSION_LINEAR_H
#define MATHS_REGRESSION_LINEAR_H

#include <assert.h>
#include <math.h>

namespace Maths
{

namespace Regression
{

//! Given a set of points, this class calculates the linear regression parameters and evaluates the regression line at arbitrary abscissas.

class Linear
{
public:

//! Class constructor

Linear(int n, double *x, double *y)
{

// calculate the averages of arrays x and y
double xa = 0, ya = 0;
for (int i = 0; i < n; i++) {
xa += x[i];
ya += y[i];
}
xa /= n;
ya /= n;

// calculate auxiliary sums
double xx = 0, yy = 0, xy = 0;
for (int i = 0; i < n; i++) {
double tmpx = x[i] - xa, tmpy = y[i] - ya;
xx += tmpx * tmpx;
yy += tmpy * tmpy;
xy += tmpx * tmpy;
}

// calculate regression line parameters

// make sure slope is not infinite
assert(fabs(xx) != 0);

m_b = xy / xx;
m_a = ya - m_b * xa;
m_coeff = (fabs(yy) == 0) ? 1 : xy / sqrt(xx * yy);

}

//! Evaluates the linear regression function at the given abscissa.

double getValue(double x)
{
return m_a + m_b * x;
}

//! Returns the slope of the regression line
double getSlope()
{
return m_b;
}

//! Returns the intercept on the Y axis of the regression line
double getIntercept()
{
return m_a;
}

//! Returns the linear regression coefficient

double getCoefficient()
{
return m_coeff;
}

private:

double m_a, m_b, m_coeff;
};


//! A static function implementing the Linear Class for one off calculations

double Linear_once(int n, double *x, double *y, double a )
{
// This function is created to enable an Instant Calculator on CodeCogs.
// You probably shouldn't be using this function otherwise.

Maths::Regression::Linear A(n, x, y);
return A.getValue(a);
}

}

}

#endif

#include <iostream.h>
#include <iomanip.h>
#include <conio>
using namespace Maths;
using namespace Maths::Regression;
int main()
{
double x[7] = { 1.5, 2.4, 3.2, 4.8, 5.0, 7.0, 8.43 };
double y[7] = { 3.5, 5.3, 7.7, 6.2, 11.0, 9.5, 10.27 };

Maths::Regression::Linear A(7, x, y);

cout << " Slope = " << A.getSlope() << endl;
cout << "Intercept = " << A.getIntercept() << endl << endl;

cout << "Regression coefficient = " << A.getCoefficient() << endl;

cout << endl << "Regression line values" << endl << endl;
for (double i = 0.0; i <= 3; i += 0.6)
{
cout << "x = " << setw(3) << i << " y = " << A.getValue(i);
cout << endl;
}

getch();
return 0;
}