-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathwatson.jl
More file actions
55 lines (52 loc) · 1.76 KB
/
watson.jl
File metadata and controls
55 lines (52 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export watson
function watson(; use_nls::Bool = false, kwargs...)
model = use_nls ? :nls : :nlp
return watson(Val(model); kwargs...)
end
function watson(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
n_orig = n
n = min(max(n, 2), 31)
@adjust_nvar_warn("watson", n_orig, n)
function f(x; n = n)
Ti = eltype(x)
return 1 // 2 * sum(
(
sum((j - 1) * x[j] * (Ti(i) / 29)^(j - 2) for j = 2:n) -
sum(x[j] * (Ti(i) / 29)^(j - 1) for j = 1:n)^2 - 1
)^2 for i = 1:29
) +
1 // 2 *
(
sum((j - 1) * x[j] * x[1]^(j - 2) for j = 2:n) -
sum(x[j] * x[1]^(j - 1) for j = 1:n)^2 - 1
)^2 +
1 // 2 *
(
sum((j - 1) * x[j] * (x[2] - x[1]^2 - 1)^(j - 2) for j = 2:n) -
sum(x[j] * (x[2] - x[1]^2 - 1)^(j - 1) for j = 1:n)^2 - 1
)^2
end
x0 = zeros(T, n)
return ADNLPModels.ADNLPModel(f, x0, name = "watson"; kwargs...)
end
function watson(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
n_orig = n
n = min(max(n, 2), 31)
@adjust_nvar_warn("watson", n_orig, n)
function F!(r, x; n = n)
Ti = eltype(x)
for i = 1:29
r[i] =
sum((j - 1) * x[j] * (Ti(i) / 29)^(j - 2) for j = 2:n) -
sum(x[j] * (Ti(i) / 29)^(j - 1) for j = 1:n)^2 - 1
end
r[30] =
sum((j - 1) * x[j] * x[1]^(j - 2) for j = 2:n) - sum(x[j] * x[1]^(j - 1) for j = 1:n)^2 - 1
r[31] =
sum((j - 1) * x[j] * (x[2] - x[1]^2 - 1)^(j - 2) for j = 2:n) -
sum(x[j] * (x[2] - x[1]^2 - 1)^(j - 1) for j = 1:n)^2 - 1
return r
end
x0 = zeros(T, n)
return ADNLPModels.ADNLSModel!(F!, x0, 31, name = "watson-nls"; kwargs...)
end