下面的3个函数可以通过数组指针ODE [3]访问:
double voltage(double v) {
return Iapp - (gL*(v - EL)) - (gNa * pow(m, 3) * h * (v - ENa)) - (gK * pow(n, 4) * (v - EK));
}
double hdot(double h) {
ah = 0.07*exp(-(v + 58) / 20);
bh = 1 / (exp(-0.1*(v + 28)) + 1);
return (phi * (ah*(1 - h) - bh*h));
}
double ndot(double n) {
an = -0.01*(v + 34) / (exp(-0.1*(v + 34)) - 1);
bn = 0.125 * exp(-(v + 44) / 80);
return (phi * (an*(1 - n) - bn*n));
}
typedef double(*eqpointer)(void);
eqpointer ODE[3];
这样
ODE[0] = voltage;
ODE[1] = hdot;
ODE[2] = ndot;
在另一项功能中, midvalues , oneloop 和 k1 是指向数组的指针。
for (int j{ 0 }; j <= eq_num; ++j) k1[j] = dt*(ODE[j]());
for (int j{ 0 }; j <= eq_num; j++) midvalues[j] = oneloop[j] + k1[j] / 2;
我想实现一个循环:
for (int j{ 0 }; j <= eq_num; j++) ODE[j](midvalues[j]);
这样每个函数ODE [j]都将相应的指针midvalues [j]作为参数。
我猜我需要两个功能指针,
一个不带参数并返回值的,即:ODE[j]()
另一个将指针位置作为参数并返回值。
我一直在努力寻找如何做到这一点,没有运气,将不胜感激任何帮助。 在此先感谢:)
答案 0 :(得分:0)
C ++上的变种:
#include <iostream>
using namespace std;
double voltage(double v) {
return v;
}
double hdot(double h) {
return h * 2;
}
double ndot(double n) {
return n * 3;
}
typedef double(*eqpointer)(double);
int main() {
eqpointer ODE[] = { voltage, hdot, ndot };
int eq_num = sizeof(ODE) / sizeof(*ODE); // quantity elements in ODE[]
double *midvalues = new double[eq_num]; //change to variant (ty M_M)
for (int j = 0; j < eq_num; j++) {
midvalues[j] = (*ODE[j])((j + 1) * 10) / 2; // Value For Example
printf("midvalues[%d]: %f\n", j, midvalues[j]);
}
return 0;
}
stdout
*** midvalues[0]: 5.000000<br>
*** midvalues[1]: 20.000000<br>
*** midvalues[2]: 45.000000<br>
带有std :: vector的变体(ty到@ M.M)
#include <iostream>
#include <vector>
using namespace std;
double voltage(double v) {
return v;
}
double hdot(double h) {
return h * 2;
}
double ndot(double n) {
return n * 3;
}
typedef double(*eqpointer)(double);
int main() {
vector<eqpointer> ODE = { voltage, hdot, ndot };
int eq_num = ODE.size(); // quantity elements in ODE[]
vector<double> midvalues(eq_num);
for (int j = 0; j < eq_num; j++) {
midvalues[j] = (*ODE[j])((j + 1) * 10) / 2; // Value For Example
printf("midvalues[%d]: %f\n", j, midvalues[j]);
}
return 0;
}
C上的变种:
#include <stdio.h>
double voltage(double v) {
return v;
}
double hdot(double h) {
return h * 2;
}
double ndot(double n) {
return n * 3;
}
typedef double(*eqpointer)(double);
int main() {
eqpointer ODE[] = { voltage, hdot, ndot };
int eq_num = sizeof(ODE)/sizeof(*ODE);
// declared only for this examle
double result;
double k1 [eq_num], oneloop[eq_num], midvalues[eq_num];
// or best malloc(eq_num * sizeof(double)); from stdlib
for (int j = 0; j < eq_num; j++) {
k1[j] = (*ODE[j])((j + 1) * 10); // random arguments ODE for example only
oneloop[j] =20 * (*ODE[j])(j + 1);
midvalues[j] = (k1[j] + oneloop[j]) / 2;
result = (*ODE[j])(midvalues[j]);
printf("*** %d) k1[]: %f, oneloop[]: %f, midvalues[]: %f result: %f\n",
j, k1[j], oneloop[j], midvalues[j], result);
}
return 0;
}
#stdout
*** 0) k1[]: 10.000000, oneloop[]: 20.000000, midvalues[]: 15.000000 result: 15.000000
*** 1) k1[]: 40.000000, oneloop[]: 80.000000, midvalues[]: 60.000000 result: 120.000000
*** 2) k1[]: 90.000000, oneloop[]: 180.000000, midvalues[]: 135.000000 result: 405.000000