I'm working on least squares problem and trying to fit given n points to 2 curves, with using JuMP and Gurobi.
This Code works for creating one curve
using JuMP, Gurobi
m = Model(solver=GurobiSolver(OutputFlag=0))
@variable( m, x[1:size(A,2)] )
@objective( m, Min, sum((A*x-b).^2) )
solve(m)
taken from this slide https://laurentlessard.com/teaching/cs524/slides/8%20-%20least%20squares.pdf
My attempt is here,
# define (x,y) coordinates of the points
x = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 2 ]
y = [ 1, 3, 0, 1, 2, 4, 6, 7, 5, 4 ]
x1 = x[1:5]
x2 = x[5:9]
y1 = y[1:5]
y2 = y[5:9]
using PyPlot
figure(figsize=(8,4))
plot(x,y,"r.", markersize=10)
axis([0,10,-2,8])
grid("on")
println(x1)
println(x2)
println(y1)
println(y2)
# order of polynomial to use
k = 3
# fit using a function of the form f(x) = u1 x^k + u2 x^(k-1) + ... + uk x + u{k+1}
n = length(x1)
A = zeros(n,k+1)
for i = 1:n
for j = 1:k+1
A[i,j] = x1[i]^(k+1-j)
end
end
A1 = zeros(n,k+1)
for i = 1:n
for j = 1:k+1
A1[i,j] = x2[i]^(k+1-j)
end
end
println(A)
print(A1)
# NOTE: must have either Gurobi or Mosek installed!
using JuMP, Gurobi
#m = Model(solver=MosekSolver(LOG=0))
m = Model(with_optimizer(Gurobi.Optimizer))
#m = Model(solver=GurobiSolver(OutputFlag=1,NumericFocus=2)) # extra option to do extra numerical conditioning
#m = Model(solver=GurobiSolver(OutputFlag=1,BarHomogeneous=1)) # extra option to use alternative algorithms
@variable(m, u[1:k+1])
@variable(m, v[1:k+1])
@variable(m, 0 <= s[1:k+1] <= 1,integer=true)
@expression(m, su[i in 1:k+1] , u'.*A[i,:])
@expression(m, sv[i in 1:k+1] , v'.*A[i,:])
#@expression(model, q_expr[k=1:2], sum([sum([x[i]*x[j]*c[k] for i in 1:3]) for j in 1:3]))
@objective(m, Min, sum( (1-s[i])*(y[i]-su[i]).^2 + s[i]*(y[i]-sv[i]).^2 for i in 1:k+1 ) )
optimize!(m)
uopt = value.(u)
println(uopt)
But model gives following error
Collection of expressions with @expression must be linear. For quadratic expressions, use your own array.
This didn't help https://discourse.julialang.org/t/collections-of-quadratic-expressions-causes-expression-to-throw-error/32359