Hi ckneale, thanks a lot for your help & sample code.
Iāve been implementing it, and unfortunately the optimizerās results are a bit poor & inconsistent.
For example, where y = y(x)
is a simple function of x
, and trying to minimize the distance z
where z^2 = x^2 + y^2
, I use cubic interpolation of z
to find where z
is smallest. x
and y
are (artificially) sampled as discrete data points.
It finds xMin, and zMin(xMin). Then using the distance formula, y = sqrt(z^2 - x^2)
, at xMin, I get y = 1.173. BUT, doing a cubic interpolation of the y data points themselves (and using the exact y(x)
), yields the value 1.159 at xMin. Thatās a pretty big difference!
Also, using Mathematica to find the exact minimum, gets xMin = 0.547 instead of Juliaās xMin = 0.554.
I understand some inexactness, but the inconsistency in y(xMin) is alarming. Is it supposed to be this off, or am I doing something wrong? Iāve included a copy of my sample code belowā¦ Thanks again for any feedback!
using Plots, Interpolations, Optim
k = 0.47
xSample = -12.0:1.0:12.0
y = Array{Float64,1}(undef, 25); z = Array{Float64,1}(undef, 25)
for i in 1:25
y[i] = (1.42-(k*xSample[i]))
z[i] = sqrt(xSample[i]^2+y[i]^2)
end
y_Interp_cubic = CubicSplineInterpolation(tSample, y)
z_Interp_cubic = CubicSplineInterpolation(tSample, z)
opt = optimize(Stuff -> z_Interp_cubic(Stuff), -12.0, 12.0)
xFORzMin = Optim.minimizer(opt)
yATmin = y_Interp_cubic(xFORzMin)
zMin = z_Interp_cubic(xFORzMin)
println("xFORzMin = ",xFORzMin," yATmin = ",yATmin," zMin = ",zMin)
InterpDisagreement = zMin-sqrt((xFORzMin^2)+(yATmin^2))
println("InterpDisagreement = ",InterpDisagreement)
yMinValDisagreement = yATmin - sqrt((zMin^2)-(xFORzMin^2))
println("yMinValDisagreement = ",yMinValDisagreement)
#
plot(xSample, abs.(xSample), xlim=(-2,3), ylim=(0,2.5)); plot!(xSample, abs.(y)); plot!(xSample, z)
scatter!(xSample, abs.(xSample)); scatter!(xSample, abs.(y)); scatter!(xSample, z)
xFine = -12.0:0.01:12.0
plot!(xFine,z_Interp_cubic(tFine), linewidth=3); scatter!([xFORzMin], [z_Interp_cubic(xFORzMin)], markersize=7)
scatter(xSample, y); plot!(xFine,y_Interp_cubic(tFine))