Created
August 14, 2020 12:11
-
-
Save RohitRathore1/e08e62afcf090b3c9e367e30078d5e94 to your computer and use it in GitHub Desktop.
Gists of surrogates package on which I am working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Plots # hide | |
default(c=:matter, legend=false, xlabel="x", ylabel="y") # hide | |
using Surrogates # hide | |
function leon(x) | |
x1 = x[1] | |
x2 = x[2] | |
term1 = 100*(x2 - x1^3)^2 | |
term2 = (1 - x1)^2 | |
y = term1 + term2 | |
end | |
n_samples = 80 | |
lower_bound = [0, 0] | |
upper_bound = [10, 10] | |
xys = sample(n_samples, lower_bound, upper_bound, SobolSample()) | |
y1 = leon.(xys); | |
x, y = 0:10, 0:10 # hide | |
p1 = surface(x, y, (x1,x2) -> leon((x1,x2))) # hide | |
xs = [xy[1] for xy in xys] # hide | |
ys = [xy[2] for xy in xys] # hide | |
scatter!(xs, ys, y1) # hide | |
p2 = contour(x, y, (x1,x2) -> leon((x1,x2))) # hide | |
scatter!(xs, ys) # hide | |
plot(p1, p2, title="True function") # hide | |
grad_x1 = x -> 2*(300*(x1)^5 - 300*(x1)^2*x2 + x1 -1) | |
grad_x2 = x -> 200*(x2 - (x1)^3) | |
d = 2 | |
function create_grads(n, d, grad_x1, grad_x2, y) | |
c = 0 | |
y2 = zeros(eltype(y[1], n*d)) | |
for i in 1:n | |
y2[i + c] = grad_x1([x[i]]) | |
y2[i + c + 1] = grad_x2(x[i]) | |
c = c + 1 | |
end | |
return y2 | |
end | |
y2 = create_grads(n, d, grad_x1, grad_x2, y) | |
@ludoro Were you talking about x1 and x2 of grad_x1 which I have put with grad. If it is the case then grad_x1 is the differentiation wrt x1 of function f and same for x2 but anyway yesterday I had improved it but I was facing this problem
MethodError: no method matching eltype(::Int64, ::Int64)
Well go look at the line of the error and see where's the problem, I cannot help you by just looking at the error
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
there is an error in the definition of grad_x1 and grad_x2, what are x1 and x2? You should have x[1] and x[2]