@LStoleriu wrote:
Hi everyone,
I’m a C&Maple physicist trying to learn the “true” Julia way of solving problems.
As a small project I’m trying to solve a nonlinear equation doing something like that:using NLsolve Nsteps = 101 Hk = 1.0 FieldMax = 3.0 * Hk FieldMin = -FieldMax H = FieldMax theta_H = 45.0 * π / 180.0 theta_0 = 15.0 * π / 180.0 function f!(F, x) F[1] = Hk * cos(x[1]) * sin(x[1]) + H * sin(x[1] - theta_H) end function j!(J, x) J[1, 1] = Hk * (cos(x[1])^2 - sin(x[1])^2) + H * cos(x[1] - theta_H) end theta = nlsolve(f!, j!, [ 0.5 ]) println(theta.zero) println(converged(theta))
This works great, but now I’d like to solve the same equation for Nsteps different values of H and store the results in an array called M.
I was able to do that using a C-like approach, by continuing the program like that:M = zeros(Nsteps) H_range = collect(range(FieldMax, length=Nsteps, stop=FieldMin)) function SWsolve(H_range, M) i = 0 for H_loco in H_range global H = H_loco theta = nlsolve(f!, j!, [ 0.5 ]) i = i+1 M[i] = cos(theta_H - theta.zero[1]) end end SWsolve(H_range, M) hyst = plot(H_range, M) display(hyst)
I tried a lot to simplify this using Julia-like constructions but I failed (for example, I tried to remove the for loop from the SWsolve() function while using the dot(".") operator to pass the H_range elements) but I failed.
Can you suggest a more efficient way of doing this?
Posts: 3
Participants: 3