0
$\begingroup$

I'm trying to fit a polynomial of the third degree through a number of points. This could be a very simple problem when not constraining the derivative. I found some promising solutions using CVXPY to combine least squares with constraints but so far I'm not even able to solve the least-squares problem with CVXPY. The optimisation problem is the following.

\begin{align} &\min_{a_i}\, (f(x) - y)^2\, ,\\ &\text{subject to } f'(x_0) = y_0\, ,\\ &\text{where } f(x) = a_0 + a_1 x + a_2 x^2 + a_3 x^3 \end{align}

The code is below.

import cvxpy as cp
import numpy as np

# Problem data
x = np.array([100, 200, 300, 400, 500])
y = 2160 - 1.678571429 * x + 0.006785714 * x * x + 5.52692E-20 * x * x * x
print(x)
print(y)

# Constructing the problem
a = cp.Variable(1)
b = cp.Variable(1)
c = cp.Variable(1)
d = cp.Variable(1)
objective = cp.Minimize(cp.sum_squares(a + b * x + c * x * x + d * x * x * x - y))
prob = cp.Problem(objective)

# The optimal objective value is returned by `prob.solve()`.
result = prob.solve(solver=cp.SCS, eps=1e-5)
# The optimal value for x is stored in `x.value`.
print("a: ", a.value)
print("b: ", b.value)
print("c: ", c.value)
print("d: ", d.value)
print(a.value + b.value * x + c.value * x * x + d.value * x * x * x)

The result of all this is a = 831.67009518; b = 1.17905623; c = 0.00155167 and d = 2.2071452e-06. This gives the mediocre result of y_est = [967.29960654 1147.20547453 1384.63057033 1692.81776513 2085.00993011]. This solution is not really satisfying to me.

Once this works, I should be able to add the constraints as follows.

# add constraint for derivative = 0 in x = 500
constraints = [b + 2 * c * 500 + 3 * d * 500 * 500 == 0]
prob = cp.Problem(objective, constraints)

I'm not bound to CVXPY, so alternative solutions are also welcome.

$\endgroup$
  • 3
    $\begingroup$ Pay attention to your objective definition. It should be: objective = cp.Minimize(cp.sum_squares(a + b * x + c * x**2 + d * x**3 - y)). With this correct definition, cvxpy perfectly retrieves the polynomial coefficients (without any constraint). When you add the derivative constraint the result seems fine as well. $\endgroup$ – Stelios Aug 29 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.