求解c ++上的微分方程

时间:2017-09-18 20:02:47

标签: c++ c++11

main 函数运行一个循环,将向量值提供给在另一个函数 gate_probabilities 中找到的依次相互依赖的微分方程组,该函数将计算反馈给main和循环重复。

将方程的解按序列连续地输入到其相应的矢量中,这在两个函数中都发生。中间方程 m_new; h_new; n_new ,最终根据时间绘制的主要向量是 V 。 (不在代码中绘图)

向量是在函数体外部定义的(它们被认为是全局的,但我想问题是它们没有被正确地声明为这样)

构建不会产生任何错误,但我在执行时遇到调试错误:“Abort()已被调用”。

非常感谢任何帮助:)

#include "C:\Users\jenbe\Documents\Visual Studio 2015\std_lib_facilities.h"
#include <math.h>
#include <iostream>
#include <cmath>
#include <tuple>
#include <vector>

using namespace std;

vector<double> mvector;
vector<double> hvector;
vector<double> nvector;
vector<double> V; // potential vector
vector<double> timevector;

//global constants

double gL = 0.1; // mS/cm^2
double gK = 9;
double gNa = 35;
double EL = -65; // mV
double EK = -90;
double ENa = 55;
double phi = 5; // Coefficient increasing reaction speed of the alpha and beta constants which it multiplies.
double Iapp = 5; // microAmpere values (as in Wang-Buzsaki)
double runtime = 3000;// 3 Seconds
double dt = 0.001; // Timestep = 1ms
double V_init = -60;


std::tuple<double> gate_probabilities(double v, double m, double h, double n) { 
    double am, bm, ah, bh, an, bn, dh, dn;

    //first stage -> gate probabilities

    am = -0.1*(v + 35) / (exp(-0.1*(v + 35)) - 1); //the probability of a closed gate to open
    bm = 4 * exp(-(v + 60) / 18); //the probability of an open gate to be closed.
    ah = 0.07*exp(-(v + 58) / 20);
    bh = 1 / (exp(-0.1*(v + 28)) + 1);
    an = -0.01*(v + 34) / (exp(-0.1*(v + 34)) - 1);
    bn = 0.125 * exp(-(v + 44) / 80);

    //second stage -> gate states

    dh = phi * ( ah*(1 - h) - bh*h); // inactivation variable h
    dn = phi * ( an*(1 - n) - bn*n); // inward recitifier

    double m_new = am / (am + bm);   //activation variable 
    double h_new = dh*dt + h;
    double n_new = dn*dt + n;

    mvector.push_back(m_new);
    hvector.push_back(h_new); 
    nvector.push_back(n_new);

    // third stage -> currents

    double IL = gL*(v - EL);
    double INa = gNa * pow(m, 3) * h * (v - ENa);
    double IK = gK * pow(n, 4) * (v - EK); // delayed recitifier
    double currents = -INa - IK - IL + Iapp;

    return std::make_tuple(currents);
}


int main() {

             //initialize vectors - later modify with function within function
    double ami = -0.1*(V_init + 35) / (exp(-0.1*(V_init + 35)) - 1);
    double bmi = 4 * exp(-(V_init + 60) / 18);
    double ahi = 0.07*exp(-(V_init + 58) / 20); // 
    double bhi = 1 / (exp(-0.1*(V_init + 28)) + 1);
    double ani = -0.01*(V_init + 34) / (exp(-0.1*(V_init + 34)) - 1);
    double bni = 0.125 * exp(-(V_init + 44) / 80);
    V.push_back(V_init); //V_init
    double m_init = (ami / (ami + bmi));
    double h_init = (ahi / (ahi + bhi)); 
    double n_init = (ani / (ani + bni));

    mvector.push_back(m_init); 
    hvector.push_back(h_init);
    nvector.push_back(n_init);

    for (int i{ 1 }; i <= runtime; ++i) { // 1ms iterations over 3000ms range

        auto ret = gate_probabilities(V[i - 1], mvector[i - 1], hvector[i - 1], nvector[i - 1]);
        double currents;
        currents = std::get<0>(ret);

        double potential = currents*dt + V[i - 1]; //calculate new V.
        V.push_back(potential); //incorporates new V into a vector.
        timevector.push_back( timevector[i - 1] + dt );
    }
}

1 个答案:

答案 0 :(得分:0)

你的行

timevector.push_back( timevector[i - 1] + dt );

在第一次循环运行时不起作用,因为timevector为空。