Advertisement
tiberiup

CN - regresie liniara

Nov 3rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. ifstream f("distributie.txt");
  7.  
  8. void citire_distributie(int n,float x[],float y[]);
  9. float aproximatie_Lagrange(int n,float x[],float y[],float z);
  10. void coeficienti_regresie(int n,float x[],float y[],float *a,float *b);
  11. float aproximatie_regresie(float a,float b,float z);
  12. int main()
  13. {
  14. int n;
  15. float x[10],y[10],z,a,b;
  16. f>>n;
  17. citire_distributie(n,x,y);
  18. f>>z;
  19. /*
  20. cout<<"Valoarea aproximativa a lui "<<z<<" este "<<aproximatie_Lagrange(n,x,y,z);
  21. */
  22. coeficienti_regresie(n,x,y,&a,&b);
  23. cout<<"a="<<a<<endl<<"b="<<b<<endl;
  24. cout<<"Valoarea aproximativa in "<<z<<" prin regresie liniara este "<<aproximatie_regresie(a,b,z)<<endl;
  25. return 0;
  26. }
  27. void citire_distributie(int n,float x[],float y[])
  28. {
  29. int i;
  30. for(i=0;i<=n;i++)
  31. f>>x[i]>>y[i];
  32.  
  33. }
  34.  
  35. float aproximatie_Lagrange(int n,float x[],float y[],float z)
  36. {
  37. float s=0,p;
  38. int i,j;
  39. for(i=0;i<=n;i++)
  40. {
  41. p=y[i];
  42. for(j=0;j<=n;j++)
  43. if(i!=j)
  44. p=p*(z-x[j])/(x[i]-x[j]);
  45. s+=p;
  46. }
  47. return s;
  48.  
  49. }
  50.  
  51. void coeficienti_regresie(int n,float x[],float y[],float *a,float *b)
  52. {
  53. float Sx=0,Sy=0,Sxy=0,Sxx=0,d;
  54. int i;
  55. for(i=0;i<=n;i++)
  56. {
  57. Sx+=x[i];
  58. Sy+=y[i];
  59. Sxy+=(x[i]*y[i]);
  60. Sxx+=(x[i]*x[i]);
  61. }
  62. d=(n+1)*Sxx-Sx*Sx;
  63. if(d)
  64. {
  65. *a=((n+1)*Sxy-Sx*Sy)/d;
  66. *b=(Sy*Sxx-Sxy*Sx)/d;
  67. }
  68.  
  69. }
  70.  
  71. float aproximatie_regresie(float a,float b,float z)
  72. {
  73. return a*z+b;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement