I am trying to solve equations of motion for an harmonic oscillator using 4th order runge kutta method, but as a result I get almost constant velocity and position; I feel that the problem is that I did not fully understood the method.
This is th code I am using (in c++)
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <fstream>
#include <iomanip>
using namespace std;
ofstream output;
void rk(double,double*,double*, double*,double*, int);
void print_file(double*, double*,double*,int n);
int main(int argc, char* argv[]){
double *t,*x,*v,*ic;
double h,in=0,fin=2*M_PI;
int n=10000;
char* file=argv[1];
output.open(file);
h=(fin-in)/n;
t=new double[n];
x=new double[n];
v=new double[n];
ic=new double[2];
ic[0]=1;//in. cond. pos.
ic[1]=0;//in. cond. vel.
rk(fun,h,ic,t,v,x,n);
for(int i=0;i<n;i+=500){cout << x[i] << " " << v[i] << endl;}
print_file(t,x,v,n);
delete[] t;
delete[] x;
delete[] v;
delete[] ic;
}
void rk(double step,double *ic,double* t, double* v, double* x, int n){
double k1,k2,k3,k4;
for(int i=1;i<n;i++){
t[i]=i*step;
x[0]=ic[0];
v[0]=ic[1];
k1=step*(-x[i-1]);
k2=step*(-x[i-1]+step/2);
k3=step*(-x[i-1]+step/2);
k4=step*(-x[i-1]+step);
v[i]=v[i-1]+(k1+2*k2+2*k3+k4)*step/6;
k1=step*(v[i-1]);
k2=step*(v[i-1]+step/2);
k3=step*(v[i-1]+step/2);
k4=step*(v[i-1]+step);
x[i]=x[i-1]+(k1+2*k2+2*k3+k4)*step/6;
}}
void print_file(double* t, double* x, double* v, int n){
int i;
for(i=0; i<=n; i++)
output<<setw(15)<<t[i]<<setw(15)<<x[i]<<setw(15)<<v[i]<<endl;
return;}
Where are my errors?