Aproksymacja trygonometryczna C++

0

Witam, mam do zrobienia program na metody numeryczne w C++. Ma być to program dokonujący aproksymacji trygonometrycznej. Kompiluje, robi jakieś wyliczenia, ale niestety wyniki wychodzą nie takie jak powinny. Wydaje mi się, że zrobiłem wszystko zgodnie z informacjami na tej stronie: http://kik.weii.tu.koszalin.pl/aproksymacja/aproks_tryg/page_dyp/aproksytr1.htm. Proszę o pomoc. W którym miejscu popełniam błąd? Siedzę już przy tym tyle, że mój mózg przestaje pracować na samą myśl o tym ;)

 
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <fstream>
#define pi 3.14159265
#define n 7

void pobierz_x(double x[n]);
void pobierz_y(double y[n]);
int stopien(int &s);
double a_0(double x[n],double &suma);
double a_i (double x[n], int numer);
double b_i (double x[n], int numer);
double wylicz_blad(double x[n], double y[n], double &blad);
double x[n],y[n];
int s;
double temp=0;
double blad=0;
double suma=0;

using namespace std;

int main()
{
pobierz_x(x);
pobierz_y(y);
stopien(s);
cout<<s<<endl;
a_0(x,suma);
cout<<"a0= "<<suma;
cout<<endl;
double** aib=new double*[s];

    for (int i=0; i<=s; i++){
        aib[i]=new double[2];
    }
    cout<<"Parametry a:"<<endl;
    for (int i=0; i<s; i++){
        aib[i][0]=a_i(x, i+1);
        cout<<"a"<<i+1<<": "<<aib[i][0]<<endl;
    }
        for (int i=0; i<s; i++){
        aib[i][1]=b_i(x, i+1);
        cout<<"b "<<i+1<<": "<<aib[i][1]<<endl;
    }

wylicz_blad(x,y,blad);
 cout << endl << "Blad aproksymacji: " << blad << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


void pobierz_x(double x[n])
{
     float bb=2*pi/n;
     x[0]=0;
     cout<<x[0]<<endl;
     for(int i=1 ; i<n ; i++)
     {
			
             x[i]=x[i-1]+bb;
             cout<<x[i]<<endl;
            
     }
     
     
};

void pobierz_y(double y[n])
{
     ifstream plik_y("c:/numerki/daney.txt");
     for(int i=0; i<n ;i++)
     {
             plik_y >> y[i];
             cout<<y[i]<<endl;    
     }
     
     
};

int stopien(int &s)
{
     do{
     cout<<"\nPodaj stopien wielomianu (pamietaj ze musi on spelniac nierownosc 2m+1<n: ";
     cin>>s;
     }while(n<2*s+1);
     return s;

}


double a_0(double x[n],double &suma)
{  suma=0;
       for (int i=0;i<n;i++)
       {
           suma+=x[i];
       }
       suma=suma/n;
       return suma;    
}

double a_i (double x[n], int numer)
{
       double suma = 0;
       for (int i=1; i<=n;i++)
       {
           suma+=x[i-1]*cos((2*pi*numer*(i))/n);
       }
       return (suma*2)/n;
}

double b_i (double x[n], int numer)
{
       double suma = 0;
       for (int i=1; i<=n;i++)
       {
           suma+=x[i-1]*sin((2*pi*numer*(i))/n);
       }
       return (suma*2)/n;
}



double wylicz_blad(double x[n], double y[n],double &blad)
{
	

    for(int i=0; i<n; i++)
    {
        temp+=pow(fabs(x[i]-y[i]),2);
    }
    blad=sqrt(temp /(n*(n-1)));
    return blad;
}
0

zacznij od zamiany twojego pi na prawdziwe M_PI.
Potem zlikwiduj zmienne globalne, jak to zrobisz samo zacznie działać.

0

Zmieniłem. Wydaje mi się, że o to chodziło. Dalej to samo, zwraca takie same wyniki jak wcześniej. Nie mam pojecia gdzie jest błąd

 #include <iostream>
#include <cstdlib>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <fstream>
#define n 7

void pobierz_x(double x[n]);
void pobierz_y(double y[n]);
double a_0(double x[n],double &suma);
double a_i (double x[n], int numer);
double b_i (double x[n], int numer);
double wylicz_blad(double x[n], double y[n], double &blad);
int stopien(int &s);




using namespace std;

int main()
{
int s;
double x[n],y[n];
double blad=0;
double suma=0;
pobierz_x(x);
pobierz_y(y);
stopien(s);
cout<<s;
a_0(x,suma);
cout<<suma;
cout<<endl;
double** aib=new double*[s];

    for (int i=0; i<=s; i++){
        aib[i]=new double[2];
    }
    cout<<"Parametry a:"<<endl;
    for (int i=0; i<s; i++){
        aib[i][0]=a_i(x, i+1);
        cout<<"a"<<i+1<<": "<<aib[i][0]<<endl;
    }
        for (int i=0; i<s; i++){
        aib[i][1]=b_i(x, i+1);
        cout<<"b "<<i+1<<": "<<aib[i][1]<<endl;
    }

wylicz_blad(x,y,blad);
 cout << endl << "Blad aproksymacji: " << blad << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


void pobierz_x(double x[n])
{
     float bb=2*M_PI/n;
     x[0]=0;
     cout<<x[0]<<endl;
     for(int i=1 ; i<n ; i++)
     {
			
             x[i]=x[i-1]+bb;
             cout<<x[i]<<endl;
            
     }
     
     
};

void pobierz_y(double y[n])
{
     ifstream plik_y("c:/numerki/daney.txt");
     for(int i=0; i<n ;i++)
     {
             plik_y >> y[i];
             cout<<y[i]<<endl;    
     }
     
     
};

int stopien(int &s)
{
     do{
     cout<<"\nPodaj stopien wielomianu (pamietaj ze musi on spelniac nierownosc 2m+1<n: ";
     cin>>s;
     }while(n<2*s+1);
     return s;

}


double a_0(double x[n],double &suma)
{  suma=0;
       for (int i=0;i<n;i++)
       {
           suma+=x[i];
       }
       suma=suma/n;
       return suma;    
}

double a_i (double x[n], int numer)
{
       double suma1 = 0;
       for (int i=1; i<=n;i++)
       {
           suma1+=x[i-1]*cos((2*M_PI*numer*(i))/n);
       }
       return (suma1*2)/n;
}

double b_i (double x[n], int numer)
{
       double suma2 = 0;
       for (int i=1; i<=n;i++)
       {
           suma2+=x[i-1]*sin((2*M_PI*numer*(i))/n);
       }
       return (suma2*2)/n;
}



double wylicz_blad(double x[n], double y[n],double &blad)
{
	
	double temp=0;
    for(int i=0; i<n; i++)
    {
        temp+=pow(fabs(x[i]-y[i]),2);
    }
    blad=sqrt(temp /(n*(n-1)));
    return blad;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1