How can i create one plot inside a for loop of different values?

i hav created these fourier series but want to plot each iteration in one plot but i do not know how?any help?

dx = 0.001
L = 2
x = seq(0,L+dx,dx)
n = length(x);n 
y = floor(n/4);y

f = rep(0,length(x));f

a = 0
b = y;b
c = y*2;c
d = y*3;d
e = n;e

f[a:b] = 0
f[b:c] = 1
f[c:d] = 1
f[d:e] = 0
f
A0 =  sum(f * rep(1,n)) *dx
fFS = (A0/2) 

n = 1000
Am = numeric(n)
Bm = numeric(n)
for(i in 1:n){
  Am[i] = sum(f * cos((2*pi*(i)*x)/L)) * dx
  Bm[i] = sum(f * sin((2*pi*(i)*x)/L)) * dx
  fFS = fFS  +  Am[i]*cos((2*(i)*pi*x)/L) + Bm[i]*sin((2*(i)*pi*x)/L)
  plot(x,fFS,type="l","col"=i+1)
}

I think the key is to use par() to set the columns and rows that the device should be split to when plots are sent.

par(mfcol=c(2,3))

for(i in 1:6){
irsam<-dplyr::sample_n(iris,size=5*i,replace=TRUE)
plot(irsam$Petal.Length,
     irsam$Petal.Width
)
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.