Skip to content
Back to formatted view

Raw Message

Message-ID: <7o3c9tj298.fsf@foo.nemo-project.org>
Date: 2004-02-02T08:48:19Z
From: Bjørn-Helge Mevik
Subject: MATLAB to R
In-Reply-To: <4368A860.211D5ACA.0B088159@aol.com> (DivineSAAM@aol.com's message of "Fri, 30 Jan 2004 16:04:52 -0500")

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