Commit 6c44fba1 authored by Fredrik Bagge Carlson's avatar Fredrik Bagge Carlson
Browse files

Modified types Polynom

parent d019ff4d
......@@ -24,8 +24,9 @@ abstract Model
abstract LinearModel <: Model
abstract NonLinearModel <: Model
abstract Network <: NonLinearModel
typealias Polynom{T<:Real} Union(T, Array{T,1})
typealias PolynomMatrix{T<:Real} Union(Array{Polynom,1},Polynom)
typealias Polynom{T<:Real} Array{T,1}
typealias PolynomMatrix Array{Polynom,1}
"""
......
......@@ -5,7 +5,7 @@ function ar(y::Vector{Float64}, na; λ = 0, bias=false)
if λ == 0
w = A\y_train
else
w = (A'A + λeye(na+1))\A'y_train
w = (A'A + λeye(size(A,2)))\A'y_train
end
prediction = A*w
error = y_train - prediction
......@@ -23,11 +23,20 @@ function arx(y::Vector{Float64}, u::VecOrMat{Float64}, na, nb; λ = 0, doplot=fa
if λ == 0
w = A\y_train
else
w = (A'A + λeye(na+1))\A'y_train
w = (A'A + λeye(size(A,2)))\A'y_train
end
prediction = A*w
error = y_train - prediction
model = AR(w,na,bias,λ)
si = na+1
b = Polynom[Polynom(w[si:si+nb[1]-1])]
si += nb[1]
for i = 2:length(nb)
push!(b,Polynom(w[si:si+nb[i]-1]))
si += nb[i]
end
model = ARX(w,na,nb,bias,λ)
na = bias ? na+1:na
result = FitResult(y_train,prediction,na, λ>0?(:LS_reg) :(:LS))
return model, result
......@@ -49,20 +58,20 @@ getARregressor(iddata::IdData, na, bias=false) = getARXregressor(iddata.y, na, b
function getARXregressor(y::Vector{Float64},u::VecOrMat{Float64}, na, nb; bias=false)
assert(length(nb) == size(u,2))
@show m = max(na+1,maximum(nb))
@show n = length(y) - m+1
@show offs = m-na-1
@show A = toeplitz(y[offs+na+1:n+na+offs],y[offs+na+1:-1:1])
@show y = copy(A[:,1])
@show A = A[:,2:end]
m = max(na+1,maximum(nb))
n = length(y) - m+1
offs = m-na-1
A = toeplitz(y[offs+na+1:n+na+offs],y[offs+na+1:-1:1])
y = copy(A[:,1])
A = A[:,2:end]
for i = 1:length(nb)
@show offs = m-nb[i]
offs = m-nb[i]
A = [A toeplitz(u[nb[i]+offs:n+nb[i]+offs-1,i],u[nb[i]+offs:-1:1+offs,i])]
end
if bias
@show A = [A ones(n)]
A = [A ones(n)]
end
return y,A
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment