At the moment I have some trouble with Julia and the scopes.
It is a short example from a book: Ferreira Introduction to Computational Physics with examples in Julia, 2016 (page 46, The Euler method).
The code:
using PyPlot
w = 2pi; # frequency, period for small oscillations T = 2pi/w
g(x,t) = -(w^2)*sin(x); # r.h.s. of Newton’s 2nd law
f(x,v,t) = [g(x,t); v]; # r.h.s. of linearized ODE
x0 = 0.5pi; # initial angular displacement
v0 = 0.0; # inicial angular velocity
y0 = [v0; x0]; # initial vector for linearized ODE
tau = 1e-4; # time step
tspan = 0:tau:5; # time range
yt = y0; # we will store the data for each t in yt
y = y0; # y at current t for the calculation
for t=tspan[1:end-1]
y = y + tau*f(y[2], y[1], t); # Euler iteration
yt = [ yt y ]; # store solution for each time step
end
# data stored in lines, transpose to use with PyPlot
v = transpose(yt[1,:]); # first line is v(t)
x = transpose(yt[2,:]); # second line is x(t)
small = x0*cos(w*tspan); # exact solution for small oscillations
# plot numerical and exact solutions for comparison
clf();
plot(tspan, x; label="numerical");
plot(tspan, small; label="small oscillations");
legend();
REPL:
┌ Warning: Assignment to `y` in soft scope is ambiguous because a global variable by the same name exists: `y` will be treated as a new local. Disambiguate by using `local y` to suppress this warning or `global y` to assign to the existing global variable.
└ @ Untitled-1:13
┌ Warning: Assignment to `yt` in soft scope is ambiguous because a global variable by the same name exists: `yt` will be treated as a new local. Disambiguate by using `local yt` to suppress this warning or `global yt` to assign to the existing global variable.
└ @ Untitled-1:14
ERROR: UndefVarError: `y` not defined in local scope
Suggestion: check for an assignment to a local variable that shadows a global of the same name.
Stacktrace:
[1] top-level scope
@ .Untitled-1:13
I have the same problems with my own big code. I haven`t any ideas, to handle this issues.
Best regards!
Julia documentation, Julia books