1

I am using the following optimization script by which I am fitting the following curve y(t)=c_1+c_2 e^{-t} between two points y_1(t_1=0)=1 and y_2(t_2=10)=7

My question is, how can I solve the same optimization problem by adding the constraints y(t=5)>4?

clc;
clear;

tic

%The Data: Time and response data
t = [0 10]';
y = [1 7]';
%Look at the Data
subplot(2,1,1)
plot(t,y,'*','MarkerSize',10)
grid on
xlabel('Time')
ylabel('Response')
hold on
%Curve to Fit
E = [ones(size(t)) exp(-t)]
%Solving constrained linear least squares problem
% cNew = lsqlin(E,y,[],[],[1 1],y(1),[],[],[],opt) % Solver-based approach
p = optimproblem;
c = optimvar('c',2);
p.ObjectiveSense = 'minimize';
p.Objective = sum((E*c-y).^2);
% constraint example: p.Constraints.intercept = c(1) + c(2) == 0.82
sol = solve(p);
cNew = sol.c;
tf = (0:0.1:10)';
Ef = [ones(size(tf)) exp(-tf)];
yhatc = Ef*cNew;
%plot the curve\
subplot(2,1,2)
plot(t,y,'*','MarkerSize',10)
grid on
xlabel('Time')
ylabel('Response')
hold on
plot(tf,yhatc)
title('y(t)=c_1 + c_2e^{-t}')

toc
Iko-SOF
  • 77
  • 1
  • 7
  • This is confusing. You provide two(2) data points and a parametric curve in two(2) parameters. This is a direct fit problem, not an OLS regression then. Did you mean that the two points bound the range of interest (and presumably you have data that falls between the two)? – J.A. Ferrand Apr 27 '21 at 02:54
  • 1
    How about modifying the objective function as follows. `p.Objective = sum((E*c-y).^2)+ 1e2*(sum(c)-0.82).^2;` . Read about Lagrange multiplier for constrained optimizations. – Praveen Apr 27 '21 at 11:59
  • @J.A.Ferrand Sorry there was a missed part in the question I just modified. What I'm aiming to do is to fit(optimize) a curve between two points at T0 and Tf such that in between T0 and Tf , certain constraints are composed (e.g. T0=0, Tf=10 and constraint is at T=4 y(t)~=3) – Iko-SOF Apr 27 '21 at 12:42
  • @Praveen Could you please take a look at the comment I just added above – Iko-SOF Apr 27 '21 at 12:43

1 Answers1

0

From MATLAB's documentation, you can add the constraint using

p.Constraints.cons1 = c(1) + c(2) * exp(-5) >= 4;

Also, note that the strict inequality > is not supported by the optimizer for numerical reasons. So if you still want the strict inequality, then add a tolerance such as

tol = 1e-10;
p.Constraints.cons1 = c(1) + c(2) * exp(-5) >= 4 + tol;

Then proceed by solving the optimization as you did in your code.

Amro
  • 130
  • 7
  • Thanks for your support. It works! I have a question, which is also related to this question. Kindly, I want to ask if you can help! Let's assume that after solving the optimization problem I provided, and thereby a y_i(t) curve got composed, I want to solve the optimization problem one more time for different two points in the space (other two points to be added and a new j curve to be fit). In this second time, how can I solve the opt problem by composing a constraint that yj[t]~=yi[t]. Meaning, at each time step t, there's no conflict between y of the first curve and y of the second @Amro – Iko-SOF Apr 29 '21 at 03:44
  • please let me know if you need more clarification – Iko-SOF Apr 29 '21 at 03:45
  • The fitted function `y_i(t_i)` is valid on the range `t_i`. So if you have `t_j` (e.g., [1, 2]) within the range `t_i` (e.g., [0, 4]), then `y_j[t_i] = y_j[t_j]` without any computation. However, if `t_j` is outside the range of `t_i`, then you solve the optimization problem by adding equality constraints `y_i(t_k)=y_j(t_k)` for every `t_k` in `t_j`. The `t_k` are discretizations of `t_j`. This optimization problem may be difficult to solve numerically though. – Amro Apr 29 '21 at 11:54
  • Thanks for your reply, let me clarify the constraint a bit more. As you mentioned let's assume y_i(t_i) is valid on the range t_i (e.g., [0, 10]), also, similarly to the code above start point y_i(0)=1, and goal point y_i(10)=7. Now, I want to compose other curve y_j(t_i) which is valid on the same time range t_j=t_i. The idea is that I want y_j(t_i) curve to start at the point y_j(0)= 2 and ends at y_j(5)=5, with No conflict in the position at any time step between the two curves (for all t yj[t] is not equal to yi[t]) @Amro – Iko-SOF Apr 29 '21 at 15:09
  • I'd also add three hints what might simplify the problem: 1- I am okay with using different objective function (another function can be taken rather than `y(t)=c(1) + c(2) * exp(-t)`). 2- turning the problem to be in 2D or 3D to avoid the conflict between two curves at certain time step is also ok. 3- the speed is not fixed, in other words, it's ok that the second curve starts at `y_j(0)= 2` and ends at `y_j(15)=5`, with benefiting from the negative `y` values @Amro – Iko-SOF Apr 29 '21 at 15:19