Skip to content

MATLAB to R

1 message · Bjørn-Helge Mevik

#
DivineSAAM at aol.com writes:
Are you sure this gives the same result?  If Y and T in Matlab are
vectors, I believe

for (J in 1:M)
{
  y[J+1] <- y[J] + h * f(tt[J], y[J])
  ...
}

is what you want.  (Don't use `t' as a variable; t() is the function
to transpose a matrix.)
I assume you mean k1(J) = ... and k2(J) = ...
## If f can take vector arguments:
k1 <- f(tt[-M],y)
k2 <- f(tt[-1], y+h*k1)
## Otherwise:
for (J in 1:M) {
  k1[J] <- f(tt[J], y[J])
  k2[J] <- f(tt[J+1], y[J] + h*k1[J])
}