Optimization based line fitting

11 ビュー (過去 30 日間)
Simon Reclapino
Simon Reclapino 2021 年 4 月 26 日
コメント済み: Simon Reclapino 2021 年 6 月 25 日
Hello!
I am using the following optimization script by which I am fitting the following curve between two points and
My question is, how can I solve the same optimization problem by adding the constraints ?
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)]
E = 2×2
1.0000 1.0000 1.0000 0.0000
%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);
Solving problem using lsqlin.
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
Elapsed time is 2.581019 seconds.
  1 件のコメント
Matt J
Matt J 2021 年 4 月 27 日
編集済み: Matt J 2021 年 4 月 27 日
how can I solve the same optimization problem by adding the constraints ...?
The constraint is already satisfied by the solution that you have. What will it accomplish to formalize the constraint in the optimization?

サインインしてコメントする。

採用された回答

Matt J
Matt J 2021 年 4 月 27 日
編集済み: Matt J 2021 年 4 月 27 日
p = optimproblem;
c = optimvar('c',2);
p.ObjectiveSense = 'minimize';
p.Objective = sum((E*c-y).^2);
p.Constraints=[1,exp(-5)]*c>=4;
sol = solve(p);
  1 件のコメント
Simon Reclapino
Simon Reclapino 2021 年 6 月 25 日
Thanks for your support. @Matt J
Could you also please help me in setting a constraint on the derivative. e.g. . However, if you'd like to, you can take a look on the detailed question in the link

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProblem-Based Nonlinear Optimization についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by