Solving a nonlinear system of equations in Julia - julia

I can write in function in Matlab in this way:
function res=resid(theta,alpha,beta);
RHS=[];
LHS=[];
RHS= theta-alpha;
LHS= theta*beta;
res = (LHS-RHS);
We set the parameters, call the function:
alpha=0.3;beta=0.95;
a01=[1.0;1.0];
th=fsolve('resid',a01,[],alpha,beta)
This will return [6.0;6.0]. Does the option"[]" signal to fsolve that the input is a vector?
Anyway, how can I implement this in Julia using a NLsolve, Optim or JuMP? The original problem has more than 10 variables, so I would prefer a vector approach.
I can implement the function in Julia:
h! =function (theta)
RHS=[];
LHS=[];
RHS= theta-alpha;
LHS= theta*beta;
res= (LHS-RHS);
return res;
end
But simply using NLsolve:
a01 = [1.0;1.0];
res = nlsolve(h!,a01)
Returns:
MethodError: no method matching (::##17#18)(::Array{Float64,1}, ::Array{Float64,1})
Closest candidates are:
#17(::Any) at In[23]:3
If I alternatively use Optim, I get:
using Optim
optimize(h!, a01)
which returns:
MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Thank you for your suggestions!

Following the suggestion by Chris Rackauckas, the solution would be to keep the definition of h:
h =function (theta)
RHS=[];
LHS=[];
RHS= theta-alpha;
LHS= theta*beta;
res= (LHS-RHS);
return res;
end
and to use not_in_place:
a01 = [1.0;1.0];
solve = nlsolve(not_in_place(h),a01)
Returning a solution:
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
Thank you!

Related

Updating a list of StaticArrays

Suppose I have this function, implemented without StaticArrays
function example_svector_bad(G)
vector_list = [ randn(G) for q in 1:1000]
for i in size(vector_list)
for g in 1:G
vector_list[i][g] = vector_list[i][g] * g
end
end
return vector_list
end
I'm hoping to implement it using StaticArrays for speed gains. However, I don't know how to do it without losing the flexibility of specifying G. For example, I could do
function example_svector()
vector_list = [#SVector randn(3) for q in 1:1000]
for i in size(vector_list)
vector_list[i] = SVector(vector_list[i][1] * 1, vector_list[i][1] * 2,
vector_list[i][1] * 3)
end
return vector_list
end
if I knew that G = 3 and I had to write out SVector(vector_list[i][1] * 1, vector_list[i][1] * 2, vector_list[i][1] * 3).
Is there a way to implement this for any arbitrary number of G?
The size of a static vector or array must be known at the compile time.
At the compile time only types are known (rather than values).
Hence your function could look like this:
function myRandVec(::Val{G}) where G
SVector{G}(rand(G))
end
Note that G is passed as type rather than as value and hence can be used to create a static vector.
This function could be used as:
julia> myRandVec(Val{2}())
2-element SVector{2, Float64} with indices SOneTo(2):
0.7618992223709563
0.5979657793050613
Firstly, there is a mistake in how you are indexing vector_list, where you do
for i in size(vector_list)
Let's see what that does:
julia> x = 1:10;
julia> size(x)
(10,)
The size of x is its length in each dimension, for a vector that is just (10,) since it has only one dimension. Let's try iterating:
julia> for i in size(x)
println(i)
end
10
It just prints out the number 10.
You probably meant
for i in 1:length(vector_list)
but it's better to write
for i in eachindex(vector_list)
since it is more general and safer.
As for your actual question, you can use StaticArrays.SOneTo which provides a static version of [1,2,3]:
function example_svector()
vector_list = [#SVector randn(3) for q in 1:1000]
N = length(eltype(vector_list))
c = SOneTo(N)
for i in eachindex(vector_list)
vector_list[i] = vector_list[i] .* c
end
return vector_list
end

print a function with plots in julia

I'm currently trying to print the following function in my plot and I'm not allowed to use any imports or other libraries:
using Plots
amplitude = 1
frequenz_E = 329.63
frequenz_G_SHARP = 415.30
frequenz_B = 493.88
k(t) = amplitude * sin * (2 * pi * frequenz_E * t)
p = plot(0:0.005:0.001, k , label="E = 329.63 Hz", title="Triade sound wave", xlabel="t (seconds)", ylabel="y(t)")
display(p)
I do want to start at 0 and then increase the value by 0.005 up to 0.01.
Now if I want to print the function, I do get the following error:
include("/home/user/Applied_Mathmatics/Assignment_01/templates/waves.jl")
ERROR: LoadError: MethodError: no method matching *(::Int64, ::typeof(sin))
Closest candidates are:
*(::Any, ::Any, ::Any, ::Any...) at operators.jl:529
*(::ChainRulesCore.NotImplemented, ::Any) at /home/user/.julia/packages/ChainRulesCore/8NXnp/src/tangent_arithmetic.jl:37
*(::ChainRulesCore.ZeroTangent, ::Any) at /home/user/.julia/packages/ChainRulesCore/8NXnp/src/tangent_arithmetic.jl:104
How can I work around the error and just print the function?
The output should look like this (I did not implement all functions yet):
As the error states, you cannot multiply a function with an Integer. You should write
k(t) = amplitude * sin(2 * pi * frequenz_E * t)
And by the way the range 0:0.005:0.001 is just one element, i.e., 0.. You can try that by
julia> collect(0:0.005:0.001)
1-element Vector{Float64}:
0.0
In Julia the middle element is the step and the third element is the end of the range. Thus something like 0:0.001:0.005 makes more sense.

Julia - generate an array of functions programmatically

I want to generate an array of functions programmatically, with a loop so that each successive function depends on the previous.
For example in pseudo-code:
f_array = [f1, f2, f3]
with:
f1(x) = x
f2(x) = 3 * f1(x)
f3(x) = 3 * f2(x)
so that I could call:
f_array[3](x)
and get the result of f3(x)
Here is what I have tried:
# create simple function just to initialize
f(x)=x
# initialize array of functions
N = 3
f_array = fill(f, N)
# now we update each function
for i in 2:N
f_array[i] = (f(x)= 3 * f_array[i-1](x))
end
I get an error:
ERROR: MethodError: Cannot convert an object of type getfield(Main,
Symbol("#f#15")){Int64} to an object of type typeof(f)
I cannot find a solution at the moment. Any help would be appreciated.
When you use fill with f it sets expected type for the elements of f_array to f, in the code below I am switching to abstract type to make it possible to have any function in the array
# create simple function just to initialize
f(x)=x
# initialize array of functions
N = 3
f_array = Array{Function}(undef, N);
f_array[1] = f;
# now we update each function
for i in 2:N
f_array[i] = x -> 3 * f_array[i-1](x)
end
print(f_array[3](2))
which produces a value of 18
In the mean time, I also found a way using metaprogramming. I post this here as it could be useful for others:
f1(x) = x
for i in 2:N
prog = "f$i(x) = 3 * f$(i-1)(x)"
exp = Meta.parse(prog)
eval(exp)
end
f3(2)
# 18
I'd write Yegor's answer as
f_array = Function[identity]
for i in 2:3
push!(f_array, x -> 3f_array[i-1](x))
end
But more importantly, this is a well known pattern: iterated function application. And it is already implemented, not in Base, but for example in IterTools.jl, by which you should be able to write:
f_array(start, N) = collect(Iterators.take(iterated(x -> 3x, start), N))
(I didn't test this, though.)

Using Complex Numbers in ODE Problem returns Inexact Error

I am trying to implement to Swing equation for a n-Machine system using Julia.
When i run the following code I get this Error Message:
LoadError: InexactError: Float64(0.0 + 1.0im)
in expression starting at /home/Documents/first_try.jl:61
Swing_Equation(::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Float64) at complex.jl:37
ODEFunction at diffeqfunction.jl:219 [inlined]
initialize!
The problem is occuring since I am using du[3] = (u[3] * u[2]) * im which can not be a Float64 type. The code is working fine when I remove the im - but then it is not the model I want to implement anymore.
What way is there to work around my problem?
using Plots
using DifferentialEquations
inspectdr()
# Constants
P_m0 = 0.3 # constant Mechanical Power
P_emax = 1
H = 1.01 # Inertia constant of the system
θ_0 = asin(P_m0 / P_emax) # angle of the system
ω_0 = 1.0 # initial angular velocity
M = 2 * H / ω_0
D = 0.9 # Damping constant
u02 = [θ_0;ω_0] # Initial Conditions
tspan = (0.0,100.0) # Time span to solve for
p = [M;P_m0;D]
i = 3
function Swing_Equation(du,u,t,p) # u[1] = angle θ
du[1] = u[2] # u[2] = angular velocity ω
P_e = real(u[3] * conj(i))
du[2] = (1 / M) * ( P_m0 - P_e - D * u[2]) # du[2] = angular acceleration
du[3] = (u[3] * u[2]) * im
end
# solving the differential equations
prob2 = ODEProblem(Swing_Equation,u0,tspan,p)
print(prob2)
sol2 = solve(prob2)
# Visualizing the solutoins
plot(sol2; vars = 1, label = "Θ_kura", line = ("red"))
plot!(sol2; vars = 2, label = "ω_kura", line = ("blue"))
gui()
plot(sol2,vars = (1,2),label="Kurmamoto" ,line = ("purple"))
xlabel!("Θ")
ylabel!("ω")
gui()
The problem is most likely in your input.
prob2 = ODEProblem(Swing_Equation,u0,tspan,p)
I am guessing that in this part you are providing an array of Float64 for u0? Your Swing_Equation then receives u as an Array{Float64} type. I suspect that also means du is the same.
This causes the expression
du[3] = (u[3] * u[2]) * im
to fail because you are trying to assign a Complex{Float64} number to du[3] which is of type Float64. Julia will then try to perform a
convert(Float64, (u[3] * u[2]) * im)
Which will cause the inexact error, because you cannot convert a complex number to a floating point number.
The Solution is to make sure du and u are complex numbers so you avoid this conversion. A quick and dirty way to solve that would be to write:
prob2 = ODEProblem(Swing_Equation, collect(Complex{Float64}, u0),tspan,p)
This will collect all elements in u0 and create a new array where every element is a Complex{Float64}. However this assumes a 1D array. I don't know your case. I don't work with ODE solvers myself.
General Advice to avoid this kind of problem
Add some more type assertions to in your code to make sure you get the kind of inputs you expect. This will help catch these kinds of problem and make you more easily see what is going on.
function Swing_Equation(du::AbstractArray{T}, u::AbstractArray{T}, t,p) where T<:Complex # u[1] = angle θ
du[1] = u[2] :: Complex{Float64}
P_e = real(u[3] * conj(i))
du[2] = (1 / M) * ( P_m0 - P_e - D * u[2]) # du[2] = angular acceleration
du[3] = (u[3] * u[2]) * im
end
Keep in mind Julia is a bit more demanding when it comes to matching up types than other dynamic languages. That is what gives it the performance.
Why is Julia different from Python in this case?
Julia does not upgrade types like Python to whatever fits. Arrays are typed. They cannot contain anything like in Python and other dynamic languages. If you e.g. made an array where each element is an integer, then you cannot assign float values to each element without doing an explicit conversion to floating point first. Otherwise Julia has to warn you that you are getting an inexact error by throwing an exception.
In Python this is not a problem because every element in an array can be a different type. If you want every element in a Julia array to be a different number type then you must create the array as a Array{Number} type but these are very inefficient.
Hope that helps!

Building a recursion function for LU decomposition in Julia

I am very new to Julia and tried troubleshooting some code I wrote for a recursive LU decomposition.
This is all of my code:
`using LinearAlgebra
function recurse_lu(A)
n=size(A)[1]
L=zeros(n,n)
U=zeros(n,n)
k=n/2
convert(Int, k)
A11=A[1:(k),1:(k)]
A12=A[1:(k),(k+1):n]
A21=A[(k+1):n,1:(k)]
A22=A[(k+1):n,(k+1):n]
if n>2
L11,U11=recurse_lu(A11)
L12=zeros(size(A11)[1],size(A11)[1])
U21=zeros(size(A11)[1],size(A11)[1])
U12=inv(L11)*A12
L21=inv(U11)*A21
L22,U22=recurse_lu(A22-L21*U12)
else
L11=1
L21=A21/A11
L22=1
L12=0
U21=0
U12=A12
U22=A22-L21*A12
U11=A11
end
L[1:(k),1:(k)]=L11
L[1:(k),(k+1):n]=L12
L[(k)+1:n,1:(k)]=L21
L[(k)+1:n,(k+1):n]=L22
U[1:(k),1:(k)]=U11
U[1:(k),(k+1):n]=U12
U[(k+1):n,1:(k)]=U21
U[(k+1):n,(k+1):n]=U22
return L,U
end`
This runs into
ArgumentError: invalid index: 1.0 of type Float64 when I try to compute the function for a matrix. I would really appreciate any tips for the future as well as how to resolve this. I am guessing I am working with the wrong variable type as some point, but Julia doesn't let you know where exactly the problem is.
Thanks a lot.
The error is because you try to index an array with a floating point number, example:
julia> x = [1, 2, 3]; x[1.0]
ERROR: ArgumentError: invalid index: 1.0 of type Float64
It looks like it origins from these two lines:
k=n/2
convert(Int, k)
which probably should be
k=n/2
k = convert(Int, k)
Alternatively you can use integer division directly:
k = div(n, 2) # or n ÷ 2
which will return an Int which you can index with.

Resources