I’m slightly surprised your code doesn’t produce errors (it doesn’t run for me at all, Julia 1.5.3). What version of Julia and Plots.jl are you using? Some comments:
I would expect m to be local to the while loop, and cause an error in the plot! unless you had previously defined it in an outer scope. Here’s info about local scopes. Did you already define m in REPL?
What do you expect from plot!? Usually you need to feed it array-like collections, not scalars. Perhaps you mean to add a single point at a time, in which case you might have better success with plot!([θ],[m]). Also I am assuming you are using Plots?
How did you initialize your plot? If your plot! works at all, it will just add to an existing plot. You may want to initialize a blank plot with plt = plot() before the loop.
And if you have a plot that doesn’t display, and it’s been initialized as above, you can do display(plt) to make it appear as explained here. But I’m not sure if that’s your problem, since I half expect m not to exist in that scope.
I have no idea why your print doesn’t work, but you may prefer to use println to get a newline between outputs.
I followed the steps above and got something that ran. My println did work, but that inner while only iterated twice, so there was not much output. It appears you expect something to converge iteratively, but two iterations will not look very impressive.
Would help if you could provide an MWE that runs, or at least errors like expected. And maybe some context about what you’re trying to do and what you expect to happen.
Hello @apo383 and @davide! Thank you very much! Your suggestions and corrections made it work.
I’m sorry I forgot to made it clear. Yes, I’m using Plots.jl.
Yes, the inner loop calculate a iteration that initializes from m0=0.1. At the first iteration, for θ almost zero, two iterations are enough (I checked myself).
@davide:
That’s a little embarassing, ha! Obviosly the inner loop would only run once, since I was not reinitializing ϵ = 1.0 at each outter loop.
Now, I present the new code and the plot that I wanted to plot. Just need to figure how to fit a curve instead of a series of points. Once again, thank you very much!
using Plots
θ = 0.01; dθ = 0.01
J = 1.0
μ = 1.0
h=0.0
i=1
while θ < 2
m0 = 0.1
m=0
ϵ = 1.0
while ϵ > 0.001
m = μ*tanh(J/(μ*θ)*m0 + h)
ϵ = abs(m-m0)
m0 = m
end
print("$m ")
if i == 1
display(scatter([θ],[m]))
else
display(scatter!([θ],[m]))
end
θ += dθ
i +=1
end
if you do not need animation, you may collect the values (eg. in a matrix or in 2 vectors) inside the loop, and plot it at once when finished, so to have a unique line, using plot in this case (and not scatter).