I have run the algorithm in a for i in length(X).
The package Distances helps me find the Euclidean distance
between θ and P. I could have used norm(θ-P) but this was
more efficient. Plots is used to illustrate the fit.
Copyright By PowCoder代写 加微信 powcoder
X = [-2 -1.64 -1.33 -0.7 0 0.45 1.2 1.64 2.32 2.9]
Y = [0.699369 0.700462 0.695354 1.03905 1.97389 2.41143 1.91091 0.919576 -0.730975 -1.42001]
using Distances
function NonLinearEstimator(X, Y, p, q, ϵ)
P = [p, q]
θ = [0.0, 0.0]
N = length(X)
Yt = zeros(N)
jac = [zeros(N) zeros(N)]
while ϵ > 0.0001
for i in 1:N
jac[i, 1] = cos(q*X[i])+q*cos(p*X[i])*X[i]
jac[i, 2] = -X[i]*p*sin(q*X[i])+sin(p*X[i])
F = p.*cos(q.*X[i])+q.*sin(p.*X[i])
Jacobian = P’*jac[i, :]
Yt[i] = Y[i] – F + Jacobian[1]
θ = inv(jac’ * jac) * jac’ * Yt
ϵ = Euclidean()(θ, P)
P = [p, q]
Answer = NonLinearEstimator(X, Y, 10, 0.2, 1)
println(“The parameters are “, Answer)
using Plots
function plotfunction(X, Y, θ)
f(X) = θ[1]*cos(θ[2]*X)+θ[2]*sin(θ[1]*X)
plot(f, X[1], X[10]); scatter!(X, Y)
Display = plotfunction(X’, Y’, Answer)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com