问题
I am receiving an error that I have never recognized before. For some background as to what this code is, I am writing a algorithm with Runge-Kutta to solve a second order differential equation (the angle of a pendulum in relation to time).
Even as I am typing this I already know there are probably many errors in this. This is part of my final project in my first ever coding course. Any help I can get, in the simplest language possible, could really help!
Here is the ERROR code
second_order.cpp: In function ‘double dxdt(double, double)’:
second_order.cpp:16:17: error: invalid operands of types ‘double’ and ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ to binary ‘operator/’
return theta/time;
^
second_order.cpp: In function ‘int main()’:
second_order.cpp:51:22: error: cannot convert ‘time_t (*)(time_t*)throw () {aka long int (*)(long int*)throw ()}’ to ‘double’ for argument ‘1’ to ‘double dxdt(double, double)’
kx1=dt*dxdt(time,x,v);
^
second_order.cpp:52:28: error: cannot convert ‘time_t (*)(time_t*)throw () {aka long int (*)(long int*)throw ()}’ to ‘double’ for argument ‘1’ to ‘double dvdt(double, double, double, double)’
kv1=dt*dvdt(time,x,v,coeff);
^
second_order.cpp:53:22: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
kx2=dt*dxdt(time+dt/2,x+kx1/2,v+kv1/2);
^
second_order.cpp:54:22: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
kv2=dt*dvdt(time+dt/2,x+kx1/2,v+kv1/2);
^
second_order.cpp:55:22: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
kx3=dt*dxdt(time+dt/2,x+kx2/2,v+kv2/2);
^
second_order.cpp:56:22: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
kv3=dt*dvdt(time+dt/2,x+kx2/2,v+kv2/2);
^
second_order.cpp:57:19: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
kx4=dt*dxdt(time+dt,x+kx3,v+kv3);
^
second_order.cpp:58:19: error: invalid operands of types ‘time_t(time_t*)throw () {aka long int(long int*)throw ()}’ and ‘double’ to binary ‘operator+’
kv4=dt*dvdt(time+dt,x+kx3,v+kv3);
^
make: *** [second_order.o] Error 1
Here is my code
#include <iostream>
#include <cmath>
//dvdt=-(g/L)*sin(theta)
//v=dxdt
double theta1;
double h;
double L;
double g=9.8;
double coeff=-1*(g/L);
double timepassed;
double dxdt( double timepassed, double theta )
{
return theta/time;
}
double v = dxdt(theta1, timepassed);
double x = theta1;
double dvdt( double theta, double coeff, double x, double v)
{
return coeff*sin(x);
}
double d2xdt2 = dvdt(timepassed, theta1, v, coeff);
int main(){
double theta;
double theta1;
double h;
double L;
double timepassed;
double time1;
std::cout << "Please input initial angle (in decimal radians), length of the pendulum and the time desired. Click ENTER key after each value."<<"\n";
std::cin >> theta1;
std::cin >> L;
std::cin >> timepassed;
double g=9.8;
double coeff=-1*(g/L);
double kx1,kv1;
double kx2, kv2;
double kx3, kv3;
double kx4, kv4;
double dt;
double x = theta1;
kx1=dt*dxdt(time,x,v);
kv1=dt*dvdt(time,x,v,coeff);
kx2=dt*dxdt(time+dt/2,x+kx1/2,v+kv1/2);
kv2=dt*dvdt(time+dt/2,x+kx1/2,v+kv1/2);
kx3=dt*dxdt(time+dt/2,x+kx2/2,v+kv2/2);
kv3=dt*dvdt(time+dt/2,x+kx2/2,v+kv2/2);
kx4=dt*dxdt(time+dt,x+kx3,v+kv3);
kv4=dt*dvdt(time+dt,x+kx3,v+kv3);
x = x + (1.0/6.0)*(kx1 + 2*kx2 + 2*kx3 + kx4);
v = v + (1.0/6.0)*(kx1 + 2*kv2 + 2*kv3 + kv4);
}
回答1:
There are several problems with your code:
You cannot use the variable
time
because it is not declared. Usetimepassed
instead.You can call function only within other functions. You cannot initialize a variable with a call to a run-time function if you are not in a function. Move these lines similar to
double v = dxdt(theta1, timepassed);
inside themain
.Do not initialize variable at global scope (it is allowed but I would discourage it because of ordering issues).
- Initialize variable before using it otherwise you get UB.
- You have variable declared at different scope with the same name i.e.
L
andh
for instance. When you useL
in themain
theL
declared at global scope will be shadowed.
来源:https://stackoverflow.com/questions/58934550/runge-kutta-rk4-2nd-order-de-in-c-error-code