A couple things to point out
- Semi colons are not necessary within functions
- In julia, when a function ends in an explamation point
!such ash!, that should signify that it mutates (changes) one of its inputs ( I say should since this is not enforced in the language but is a naming convention) - When showing code on this forum, you should put your code in “backticks” (the key under tilda) for readability:
this is enclosed in backticks
Now for the code, the error message you get from nlsolve has to do with point #2. NLsolve expects your function to take the residual vector and simply mutate it as opposed to returning a new vector. Here’s an example that works.
julia> using NLsolve
julia> function h!(theta, resvec, alpha, beta)
resvec[1] = theta[1]*beta - (theta[1] - alpha)
resvec[2] = theta[2]*beta - (theta[2] - alpha)
end
julia> function f!(theta, resvec)
h!(theta,resvec, 0.3, 0.95)
end
julia> a01 = [1.0,1.0];
julia> nlsolve(f!,a01)
Results of Nonlinear Solver Algorithm
* Algorithm: Trust-region with dogleg and autoscaling
* Starting Point: [1.0,1.0]
* Zero: [6.0,6.0]
* Inf-norm of residuals: 0.000000
* Iterations: 3
* Convergence: true
* |x - x'| < 0.0e+00: false
* |f(x)| < 1.0e-08: true
* Function Calls (f): 4
* Jacobian Calls (df/dx): 4
I’m sure there are other ways to handle the alpha and beta parameters besides making another function.
Good reading materials:
https://docs.julialang.org/en/release-0.4/manual/noteworthy-differences/
ETA: And to clarify for your other example, Optim is used to optimize a function, this is different than solving it, the error you get is because you are returning a vector, while optimization is performed on a scalar (cost) quantity). Hope this helps!