So I would like to ultimately solve an equation f(x,a)=0 for x many times as I vary a and plot the solution as a function of a. Before getting to that though, I am first trying to learn to use NLsolve in a very simple case of the function f(x)=x+1.
function f!(F, x)
F=x+1
end
function j!(J,x)
J=1
end
NLsolve(f!, j!, 1)
(I am unsure of what to put in the third argument of NLsolve which I guess is the initial condition but I tried a couple things like 1 and [ 0.1; 1.2] which was in the example).
But now I would like to get a parameter a so that I can solve:
x+a=0 for different a’s.
I try:
for a in range(-1, 1, length=9)
# a = range(-1, 1, length=9)?
# for i in length(a)?
function f!(F, x)
F[1]=x[1]+a
end
function j!(J,x)
J[1]=1
end
nlsolve(f!, j!, 1.0)
end
MethodError: no method matching nlsolve(::var"#f!#13"{Float64}, ::var"#j!#14", ::Float64)
answer=zeros(9)
counter = 1
for a in range(-1, 1, length=9)
function f!(F, x)
F[1]=x[1]+a
end
function j!(J,x)
J[1]=1
end
answer[counter]=nlsolve(f!, j!, [1.0]).zero
counter = counter + 1
end
I get: MethodError: Cannot convert an object of type Array{Float64,1} to an object of type Float64
I guess the type of nlsolve().zero is not compatible with the type of the entries from zeros() function. How can I convert?
That’s interesting but I’m worried https://github.com/rveltz/BifurcationKit.jl will be too complicated for me at this stage of my learning. I am trying to do super basic things.
The same problem happens over and over. Nlsolve deals with vectors (multi-dimensional problems) and you keep treating inputs and outputs as scalars. Here you try to assign the result (a vector) to a scalar (answer[counter])
Try to run each command one by one in the REPL by hand, in order to be able to see what the output looks like, in particular what type of object it is.
Once you can run each line by hand and “run the loop by hand” then write the actual loop.