Nov 30, 2011 at 4:22am UTC
Hi Guy,
I am trying to write a simple Runge Kutta code in C++ but I keep getting this error: expected primary-expression before 'double'.
This is my c++ code:
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <math.h>
double f (double t, double y)
{
f = y + t;
}
double ComputeK (double t, double y, double h, double k1, double k2, double k3, double k4)
{
k1 = h*f(t,y);
k2 = h*f(t+0.5*h , y+0.5*k1);
k3 = h*f(t+.05*h , y+0.5*k2);
k4 = h*f(t+h , y+k3);
}
double ComputeRanjKuta (double *t, double *y, double T, double h, double t0, double y0)
{
double Nelem;
Nelem = T/h;
int i;
t[1] = t0;
y[1] = y0;
for(i=0; i <Nelem; i++)
{
t[i+1] = i*h;
}
for(i=0; i <Nelem; i++)
{
ComputeK(double t[i], double y[i], double h, double k1, double k2, double k3, double k4);
y[i+1] = y[i] + (1/6)*(k1 + 2*k2 + 2*k3 + k4);
}
}
int main ()
{
double t0 = 0;
double y0 = 1;
double T = 0.5;
double h = 0.01;
ComputeRanjKuta(double *t, double *y, double T, double h, double t0, double y0);
return 0;
}
Can somebody please help me out?
Nov 30, 2011 at 8:50am UTC
As punjabian mentioned, there is no need to mention the type of the variable when you are calling the function. Remove the type and compilation will happen but with errors as k1,k2,k3,k4 are not defined in ComputeRanjKuta and *t,*y not defined in main().
Correct the function definition of
double f (double t, double y)
{
f = y + t;
}