# Ode\_bdf solver of a vector function

**URL:** <https://discourse.mc-stan.org/t/ode-bdf-solver-of-a-vector-function/26802>\
**Category:** Modeling\
**Created:** [March 15, 2022, 2:18pm UTC](https://discourse.mc-stan.org/t/ode-bdf-solver-of-a-vector-function/26802 "2022-03-15T14:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![irenegfsr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/irenegfsr/32/15802_2.png) [@irenegfsr](https://discourse.mc-stan.org/u/irenegfsr)\
**Post date:** [March 15, 2022, 2:18pm UTC](https://discourse.mc-stan.org/t/ode-bdf-solver-of-a-vector-function/26802/1 "2022-03-15T14:18:22Z")

</div>

Hi all,

I am trying to solve this code. When I run it I get the error message written below. However, the only way to solve the function is by adding vector[] model3 and not vector model3 as the program is suggesting to do, also it says that I should add an int value where I did not define prior any int, and all my values are real type, any idea why this?  
I have also tried to change my parameters of the function as reals and change the vector type equations to reals as well but I still get the same error message.  
Semantic error in ‘string’, line 40, column 25 to column 72:

Ill-typed arguments supplied to function ‘ode\_bdf\_tol’. Expected arguments:  
(real, vector) =\> vector, vector, real, real[], real, real, int  
Instead supplied arguments of incompatible type:  
(real, vector, vector, vector, vector) =\> vector[], vector, real, real[], real, real, real

```nohighlight
functions {
  vector[] model3(real t, vector y, vector pAB, 
  vector uAB, vector uASC) {
    
    vector[rows(y)] dAB_dt = pAB*y[2]-uAB*y[1];
    vector[rows(y)] dASC_dt = -uASC*y[2];
    vector[rows(y)] out[2];
    out = {dAB_dt, dASC_dt};
    
   return (out);
  }
}
data {
  int <lower=1> nobs;
  real t0;
  vector[2] y0;
  array[nobs] real ts;
  int <lower=1> indivs;
  array[nobs] real antib;
  real <lower=1, upper=indivs> subj[nobs];
}
parameters {
  //real <lower=0> AB0;
  row_vector[1] pAB0;
  row_vector[1] uAB0;
  row_vector[1] uASC0;
  //real <lower=0> sigmaAB0;
  real <lower=0> sigma;
  vector[indivs] rpAB;
  vector[indivs] ruAB;
  vector[indivs] ruASC;
}
transformed parameters {
  real <lower=0> pAB;
  pAB = pAB0*rpAB;
  real <lower=0> uAB;
  uAB = uAB0*ruAB;
  real <lower=0> uASC;
  uASC = uASC0*ruASC;
  vector[nobs] yhat[2] = ode_bdf_tol(model3, y0, t0, ts, pAB, uAB, uASC);
}
model {
  rpAB ~ lognormal(-2, sqrt(4));
  ruAB ~ lognormal(-1, sqrt(2));
  ruASC ~ lognormal(-0.1, 0.44);
  //AB0 ~ lognormal(-2, 4);
  //sigmaAB0 ~ gamma(0.001, 0.001);
  sigma ~ normal(0, 1);
  //for (i in 1:nobs) {
    //antib[i] ~ lognormal(yhat[i,1], sigma); 
  //}
  antib ~ lognormal(log(yhat[: , 2]), sigma);
}
generated quantities {
  array[nobs] real z_pred;
  for (j in 1:nobs) {
    z_pred[j] = lognormal_rng(log(yhat[j,2]), sigma); 
  }
}

```

.

---

<div class="post-metadata">

**Author:** ![arya](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/arya/32/1626_2.png) [@arya](https://discourse.mc-stan.org/u/arya)\
**Post date:** [March 15, 2022, 5:50pm UTC](https://discourse.mc-stan.org/t/ode-bdf-solver-of-a-vector-function/26802/2 "2022-03-15T17:50:22Z")

</div>

`model3` should only be returning a vector but `out` is an array of vectors. The ODE is supposed to be 2D right? Why are the parameters declared as vectors? If I’m understanding correctly, it might make more sense to write the function like the [guideline here](https://mc-stan.org/docs/2_29/stan-users-guide/measurement-error-models.html):

```nohighlight
vector[] model3(real t, vector y, real pAB, 
  real uAB, real uASC) {
    
    real dAB_dt = pAB*y[2]-uAB*y[1];
    real dASC_dt = -uASC*y[2];
    vector[rows(y)] out = [dAB_dt, dASC_dt]';

   return (out);
  }

```

---

<div class="post-metadata">

**Author:** ![irenegfsr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/irenegfsr/32/15802_2.png) [@irenegfsr](https://discourse.mc-stan.org/u/irenegfsr)\
**Post date:** [March 16, 2022, 8:28am UTC](https://discourse.mc-stan.org/t/ode-bdf-solver-of-a-vector-function/26802/3 "2022-03-16T08:28:34Z")

</div>

Thanks for the reply. I will try this code. However, the parameters were defined as vectors since later on I needed to transform them (see in transformed parameters). Hence, if I would define this in the function as reals it would not allow me to transform them later and call the transformed parameters into the ode solver?

---

<div class="post-metadata">

**Author:** ![irenegfsr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/irenegfsr/32/15802_2.png) [@irenegfsr](https://discourse.mc-stan.org/u/irenegfsr)\
**Post date:** [March 16, 2022, 8:41am UTC](https://discourse.mc-stan.org/t/ode-bdf-solver-of-a-vector-function/26802/4 "2022-03-16T08:41:54Z")

</div>

Unfortunately this did not work; There is an error statement indicating that line 2 (vector [] model3), should be the same type as the return statement, indicating that out and model3 are not the same type.

Semantic error in ‘string’, line 2, column 2 to line 9, column 3:

Function bodies must contain a return statement of correct type in every branch.

---

<div class="post-metadata">

**Author:** ![irenegfsr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/irenegfsr/32/15802_2.png) [@irenegfsr](https://discourse.mc-stan.org/u/irenegfsr)\
**Post date:** [March 16, 2022, 8:51am UTC](https://discourse.mc-stan.org/t/ode-bdf-solver-of-a-vector-function/26802/5 "2022-03-16T08:51:04Z")

</div>

And otherwise, if I keep the parameters as reals but I do change the response of the ODEs as vectors, I would get the following error:

Semantic error in ‘string’, line 5, column 4 to column 47:

Ill-typed arguments supplied to assignment operator =: lhs has type vector and rhs has type real
