Archive for November, 2008

plotting with R

November 24, 2008

R is a statistical plotting language, similar to Matlab. It’s not very user-friendly though, especially when you try to use it for the first time. In my class, I needed to plot two functions in the same graph, for Voltage and Current through an RL circuit. Here is what I used in order to accomplish that:

> t = seq(-2*pi,2*pi, length=1000)

> f = 4*cos(2*t)

> g = 0.398*cos(2*t-1.466)

> plot(c(t,t), c(f,g), type=”n”, xlab=”t”, ylab=”y”)

> lines(t,g, col=”red”)

> lines(t,f, col=”blue”,lty=2)

> legend(-2.636377,3.731795,”y=0.398cos2t-84″,lty=1,col=”red”)

> legend(-2.636377,4.264303, “y=4cos2t”,lty=2,col=”blue”)

The result I got was

plotting in R

plotting in R