Hey guys,
I am a new member here and I am having a problem with this Runge-Kutta program. I am programming in C with the Dev C++ v 4.9.9.2 compiler. For some reason my y1 and y2 values are not changing from 2. Does anyone know what is going on?
insert
Code:
#include <stdio.h>
#include <stdlib.h>
double f1(double y2);
double f2(double y1, double y2, double t);
FILE *print;
int main()
{
int j;
double t_max = 80, delta_t = .2;
//Define N (size)
int N = t_max / delta_t;
double y1[N], y2[N], t[N], k1[5][N], k2[5][N], e[N];
t[0] = 0;
y1[0] = 2;
y2[0] = 2;
print=fopen("Runge-Kutta.xls", "w");
fprintf(print, "Time (s)\ty1\ty2\tk1\tk2\tk3\tk4\terror\n");
for(j=0; j <= N; j++)
{
k1[1][j] = delta_t * f1(y2[j]);
k2[1][j] = delta_t * f2(y1[j], y2[j], t[j]);
k1[2][j] = delta_t * f1(y2[j]+(k2[1][j]/2));
k2[2][j] = delta_t * f2(y1[j]+(k1[1][j]/2), y2[j]+(k2[1][j]/2), t[j]+(delta_t/2));
k1[3][j] = delta_t * f1(y2[j]+(k2[2][j]/2));
k2[3][j] = delta_t * f2(y1[j]+(k1[2][j]/2), y2[j]+(k2[2][j]/2), t[j]+(delta_t/2));
k1[4][j] = delta_t * f1(y2[j]+k2[3][j]);
k2[4][j] = delta_t * f2(y1[j]+k1[3][j], y2[j]+k2[3][j], t[j]+delta_t);
y1[j+1] = y1[j] + (1/6)*(k1[1][j] + 2 * k1[2][j] + 2 * k1[3][j] + k1[4][j]);
y2[j+1] = y2[j] + (1/6)*(k2[1][j] + 2 * k2[2][j] + 2 * k2[3][j] + k2[4][j]);
t[j+1] = t[j] + delta_t;
fprintf(print, "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", t[j], y1[j], y2[j], k2[1][j], k2[2][j], k2[3][j], k2[4][j], e[j]);
}
fprintf(print, "\n\n");
fclose(print);
//This line keeps the command prompt open so you have time to read the answer.
system("PAUSE");
return 0;
}
//------------------------------------------------------------------------------
double f1(double y2)
{
return y2;
}
double f2(double y1, double y2, double t)
{
double c = .75, m = 40, k = .7, f = 2, w = .2;
return (-c/m)*y2-(k/m)*y1+((f*sin(w*t))/m);
}