You are not logged in.
Pages: 1
hello, I'm looking for someone to help me with this code..
i'm trying to resolve a second order equation..
(dv/dt)= 10 - (94.5/0.3)x - (13/0.3)v;
(dx/dt)=v;
this is what i have written so far, i think it's not bad i have one simple problem calling the RK (Runge Kutta) function
error : " reference to non-static function must be called "
#include<iostream>
#include<math.h>
#include<cmath>
using namespace std;
double RK(double (*f)(double a,double b),double t1,double t0,double x0){
double K1=f(t0,x0);
double dt=t1-t0;
double K2=f(t0+dt/2,x0+(dt/2)*K1);
double K3=f(t0+dt,x0+dt*K2);
return x0+(dt/6)*(K1+4*K2+K3);
}
class RKI{
public:
double v0;
double t0;
double x0;
RKI(){
x0=v0=t0=0;
}
double F_V(double t0,double v0){
return 10-(94.5/0.3)*x0-(13/0.3)*v0;
}
double F_X(double t0,double x0){
return v0;
}
};
int main(){
RKI r;
double x1;
double v1;
double t1;
for(int i=0;i<10;++i){
t1=r.t0+0.2;
v1=RK(r.F_V,t1,r.t0,r.v0); // Error here
x1=RK(r.F_X,t1,r.t0,r.x0); // Error here
cout<<"v1 = "<<v1<<endl;
cout<<"x1 = "<<x1<<endl;
cout<<"t1 = "<<t1<<endl;
r.x0=x1;
r.v0=v1;
r.t0=t1;
}
return 0;
}
Thank you for your help!
I am no cpp expert, but the one of RK's parameters seem like a pointer, did you want to do that?
hello, I'm looking for someone to help me with this code..
i'm trying to resolve a second order equation..
(dv/dt)= 10 - (94.5/0.3)x - (13/0.3)v;
(dx/dt)=v;this is what i have written so far, i think it's not bad i have one simple problem calling the RK (Runge Kutta) function
error : " reference to non-static function must be called "#include<iostream> #include<math.h> #include<cmath> using namespace std; double RK(double (*f)(double a,double b),double t1,double t0,double x0){ double K1=f(t0,x0); double dt=t1-t0; double K2=f(t0+dt/2,x0+(dt/2)*K1); double K3=f(t0+dt,x0+dt*K2); return x0+(dt/6)*(K1+4*K2+K3); } class RKI{ public: double v0; double t0; double x0; RKI(){ x0=v0=t0=0; } double F_V(double t0,double v0){ return 10-(94.5/0.3)*x0-(13/0.3)*v0; } double F_X(double t0,double x0){ return v0; } }; int main(){ RKI r; double x1; double v1; double t1; for(int i=0;i<10;++i){ t1=r.t0+0.2; v1=RK(r.F_V,t1,r.t0,r.v0); // Error here x1=RK(r.F_X,t1,r.t0,r.x0); // Error here cout<<"v1 = "<<v1<<endl; cout<<"x1 = "<<x1<<endl; cout<<"t1 = "<<t1<<endl; r.x0=x1; r.v0=v1; r.t0=t1; } return 0; }
Thank you for your help!
I guess you are sending a pointer to a member function whereas the function expects a pointer to a function.
you cannot just pass a member function as a parameter, you would need an object to apply it to.
In addition, I assume this is a homework, you might need to write it in a more legible way.
--
Last edited by venam (September 9 2016)
Pages: 1