I think you may be using automatic save and restore of your workspace. this is not recommended for reproducible work.
again, in the section
for (i in 1:5) {
a[i] = 0.5*(1+sin(2*pi*m*i*dx/N));
}
a[i] is accessed, but a has not been defined as anything appropriate and would be expected to get the following error
Error: object 'a' not found
Therefore the code you shared will not work, unless by accident the user (including yourself) has an appropriate a object that can be indexed etc.
There is an easy fix, it is to define a in advance of accessing its parts a[i] i already gave examples so I wont repeat here.
Glad you are happy with your script..., but you are in danger of being 'caught out' if down the road when your r session is in another state (lacking an a for example), at which time your previously working code will fail)