0

To understand and learn how ode23s method work in R, I used a simple system from a numerical analysis book and implemented. I encountered an error

Error in t + y[1]^2 : non-numeric argument to binary operator

Here is my code

#x'=t+x^2-y
#y'=t^2-x+y^2
#x(0)=3, y(0)=2

model  <- function(t,y) as.matrix(c(t+y[1]^2-y[2], t^2-y[1]+y[2]^2))
sol <- ode23s(model, 0, 20, c(3,2), jac = NULL, hmax = 0)

It looks to me that t is not numeric and I also used as.numeric(t) but did not work either. Could you please explain, how to fix this error?

bell
  • 191
  • 1
  • 1
  • 11
  • Per the documentation examples in https://www.rdocumentation.org/packages/pracma/versions/1.9.9/topics/ode23, it could be that `x0` is also required to be `as.matrix`, that for some reason the list is not automatically converted into a vector. – Lutz Lehmann Jul 04 '18 at 14:56
  • I am following example 3 in documentation, because it is related to ode23s. x0 is used in an example 2 using ode23. – bell Jul 04 '18 at 15:18
  • hmmm, according to documentation y'=f(x,y), so the function f should not depend on time t. Therefore, I am getting error. My original system that I am working with is time dependent and stiff, I don't know how to use ode23s. – bell Jul 04 '18 at 23:47
  • That is for a function `y(x)`. Which can be vector valued. In your case the independent is `t`. Your code looks quite right in that regard. – Lutz Lehmann Jul 05 '18 at 00:02
  • `@Lutz` yes, you are right. Then I don't understand the error. I am passing `t0=0` and `tFinal=20` and both are numeric. – bell Jul 05 '18 at 01:08
  • when I type `t`, it displays: `function (x)` `UseMethod("t")` `` `` can anyone explain this? – bell Jul 05 '18 at 03:09
  • Is there any way to find out the solution? I am stuck. – bell Jul 09 '18 at 21:49

0 Answers0