#include <iostream>
#include <conio.h>
using namespace std;

// This program solves the differential equation y''=-xy with various boundary conditions 
// using the metod of undetermined coefficients up to a given term.


main () {
    
    cout << "This program solves the differential equation y''=-xy" << endl 
         << "using the method of undetermined coefficients."
         << endl << endl
         << "Written by Yamac Pehlivan for the Numerical Analysis course." <<endl << endl ;
    
    int N;

// The number of terms to appear in the solution.

    cout << "Please enter how many terms you need in your solution." 
         << endl;
   
    Enter: 
    cout << "N=";
    
    cin >> N;
    
    if (N<=1) {
        cout << "Please enter a value larger than 1" << endl;
         goto Enter;
         }
    
    float c[N];

// Boundary Values:
    
    cout << endl<< "Please enter the boundary values:" << endl << "y(0)=";
    cin >> c[0];
    cout << endl << "y'(0)=";
    cin >> c[1];

    c[2]=0;

// Calculate the Coefficeints:

    int n;

    for (n=1; n<N-2; n++)
    {
     c[n+2]=-c[n-1]/((n+1)*(n+2));
    }

 // Find the first nonzero coefficient:

    int f=0;

    for (n=0; n<N; n++)
   {
    if (c[n]!=0)  break;
    f=f+1;
    }
    
 //  Print the results on the screen:

    cout << endl << "Press enter to continue";
    cin.ignore();
 
   cout << endl;
   
   cout << endl<< c[f] << "x^"<<f;

    for (n=f+1; n<N; n++)
    {
     if (c[n]!=0)
     {
         cout << "+("<<c[n]<<")x^"<<n;
     }   
    }    
    cout << endl << "Press enter to continue";
    cin.ignore();
    
}    
    
      
     
           

           
  
         

