[R] vector math problem
Bill Simpson
wsimpson at gcal.ac.uk
Tue Jun 22 12:18:49 CEST 1999
I am trying to get a persp plot of a surface that is a function of v0 and
v1.
The surface height at (v0,v1) is
sqrt(sum((s1[i][j]-s0[i][j])^2))
where
s1[i][j] = a*sin(2*pi*freq*x[i]+v1*y[j])
s0[i][j] = a*sin(2*pi*freq*x[i]+v0*y[j])
I am having a hard time with the vector multiplication. This is what I
have so far:
v0 <- seq(-7,7,1)*.1
v1 <- v0
x <- seq(0,2*pi,.1)
y <- x
f <- function(v0,v1)
{
a<-2
freq<-1
s1<-a*sin(2*pi*freq*x+v1*y) ##wrong
s0<-a*sin(2*pi*freq*x+v0*y) ##wrong
sqrt(sum((s1-s0)^2))
}
z <- outer(v0,v1,f)
z[is.na(z)] <- 1
persp(v0, v1, z, theta=30, phi=30)
The lines marked ##wrong are wrong because the dimensions of x[] and y[]
are not necessarily the same. In C one would do
for(v0=v0min;v0<v0max;v0+=v0inc)
for(v1=v1min;v1<v1max;v1+=v1inc)
{
sumsq=0;
for(i=0;i<nx;i++)
for(j=0;j<ny;j++)
{
s1[i][j]=a*sin(2*pi*freq*x[i]+v1*y[j]);
s0[i][j]=a*sin(2*pi*freq*x[i]+v0*y[j]);
sumsq+=(s1[i][j]-s0[i][j])*(s1[i][j]-s0[i][j]);
}
m++; n++;
output[m][n]=sqrt(sumsq);
}
How to do this in R?
Thanks very much for any help.
Bill Simpson
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list