#include < stdio.h >
#include < stdlib.h >
#include "nrutil.h"
#include "ran1.h"
#define NVAR 2
#define NPAR 6
#define H 0.001
#define FILEIN "par.ini"
#define FILECI "ci.ini"
#define NMAX 1000000
void rk4(double * p, double * x, double * dx, double t,
void( * deriv)(double * p, double * x, double * dx, double t));
void deriv(double * p, double * x, double * dx, double t);
int main() {
double * p, * x, * dx, t, dt, TTOT, DTSAV, seed = 0.0, xx = 0.0, y = 0.0;
long count;
int i, j;
FILE * fp1, * fp2, * fp3, * fp4;
p = dvector(1, NPAR);
x = dvector(1, NVAR);
dx = dvector(1, NVAR);
if (!(fp1 = fopen(FILEIN, "r"))) nrerror("No se encuentra el archivo par.ini");
for (i = 1; i <= NPAR; i++) fscanf(fp1, "%lg", & p[i]);
fscanf(fp1, "%lg %lg ", & TTOT, & DTSAV);
fclose(fp1);
printf("Introduce un número: ");
scanf("%lg", & seed);
if (seed > 0) {
seed = -seed;
}
for (i = 1; i <= NVAR; i++) {
xx = ran1( & seed);
y = xx * 15.0 - 65.0;
x[1] = y;
}
for (j = 1; j <= NVAR; j++) {
x[2] = 0.2 * x[1];
}
t = 0.0;
count = 0;
fp1 = fopen("vu.dat", "w");
while (t <= TTOT && count <= NMAX) {
fprintf(fp1, "%12.8g ", t);
for (i = 1; i <= NVAR; i++) {
fprintf(fp1, "%12.8g ", x[i]);
}
fprintf(fp1, "\n");
dt = 0.0;
while (dt <= DTSAV) {
rk4(p, x, dx, t + dt, deriv);
dt += H;
}
t += dt;
count++;
}
fclose(fp1);
free_dvector(p, 1, NPAR);
free_dvector(x, 1, NVAR);
free_dvector(dx, 1, NVAR);
return 1;
}
void deriv(double * p, double * x, double * dx, double t) {
dx[1] = 0.04 * x[1] * x[1] + 5 * x[1] + 140 - x[2] + p[1];
dx[2] = p[2] * (p[3] * x[1] - x[2]);
if (x[1] >= p[4]) {
x[1] = p[5];
x[2] = x[2] + p[6];
}
return;
}
void rk4(double * p, double * x, double * dx, double t,
void( * deriv)(double * p, double * x, double * dx, double t)) {
int i;
double * dxm, * dxt, * xt;
float hh, h6, mu;
dxm = dvector(1, NVAR);
dxt = dvector(1, NVAR);
xt = dvector(1, NVAR);
hh = H * 0.5;
h6 = H / 6.0;
( * deriv)(p, x, dx, t); /*dx=f(x0)*/
for (i = 1; i <= NVAR; i++) xt[i] = x[i] + hh * dx[i];
( * deriv)(p, xt, dxt, t + hh); /*xt es x1, dxt=f(x1)*/
for (i = 1; i <= NVAR; i++) xt[i] = x[i] + hh * dxt[i];
( * deriv)(p, xt, dxm, t + hh); /*xt es x2, dxm=f(x2)*/
for (i = 1; i <= NVAR; i++) {
xt[i] = x[i] + H * dxm[i]; /*xt es x3, dxm=f(x1)+f(x2)*/
dxm[i] += dxt[i];
}
( * deriv)(p, xt, dxt, t + H); /*dxt=f(x3)*/
for (i = 1; i <= NVAR; i++)
x[i] += h6 * (dx[i] + dxt[i] + 2.0 * dxm[i]);
free_dvector(xt, 1, NVAR);
free_dvector(dxt, 1, NVAR);
free_dvector(dxm, 1, NVAR);
return;
}
我通常使用gcc -O3 -o rk4 rk4.c -lm
进行编译以执行./rk4 > temp.dat &
我收到的消息是:
Undefined symbols for architecture x86_64:
"_dvector", referenced from:
_main in rk4-173408.o
_rk4 in rk4-173408.o
"_free_dvector", referenced from:
_main in rk4-173408.o
_rk4 in rk4-173408.o
"_nrerror", referenced from:
_main in rk4-173408.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
答案 0 :(得分:0)
您的代码似乎正在使用“数字食谱”中的库,而-lm
选项可能未链接到该库(我猜-lm
应该放在-o
选项之前)。您可能会找到二进制文件或从源代码中自己编译“数字配方”库,然后应添加选项-L/path/to/the/library
以使链接程序能够找到这些功能。