[R] MATLAB to R
Bjørn-Helge Mevik
bhx2 at mevik.net
Mon Feb 2 09:48:19 CET 2004
DivineSAAM at aol.com writes:
> In MATLAB, I can write:
>
> for J=1:M
> Y(J+1)=Y(J)+ h * feval(f,T(J),Y(J));
> ...
>
> In R, I can write above as:
>
> for (J in 2:M)
> {
> y = y + h * f(t,y)
> ...
> }
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.)
> for J=1:M
> k1 = feval(f,T(J),Y(J));
> k2 = feval(f,T(J+1),Y(J)+ h * k1
I assume you mean k1(J) = ... and k2(J) = ...
> How do I write k2 in R?
> k1 = f(t,y)
> k2 = ?
## 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])
}
--
Hth,
Bjørn-Helge Mevik
More information about the R-help
mailing list