Minimise the sum of squared errors, with non linear constraints

조회 수: 2 (최근 30일)
Lewis Marshall
Lewis Marshall 2021년 3월 26일
편집: Matt J 2021년 4월 5일
hello i am trying to find the coefficient vlaues that minimises the sum of the squared erorrs between the eqaution ->
sigma_therory=((-(x(1) + x(2)./lamda(i)).*(2./lamda(i) - 2*lamda(i).^2))
and experimental data "sigma_c"
x(1) and x(2) are coefficient values in which im trying to calculate and "lamda" is the input variable to the experiment. x(1) >0 and x(1)+x(2)>0
I have generated this code which i think does what i require, however i know what i have wirtten in the for loop is poor code and ineffifcient as it takes very long to run, any help on how to improve this would be greatly appreicated.
y=0
for i=1:length(lamda)
fun{i}=@(x) y+((-(x(1) + x(2)./lamda(i)).*(2./lamda(i) - 2*lamda(i).^2))-sigma_c(i))^2;
y=fun; %save the output of the function to be added to the next itteration
end
x0=[1 -1];
A=[-1,0];
b=[0];
[xf,fval]=fmincon(fun,x0,A,b,[],[],[],[],@nolin);
function [c,ceq]=nolin(x) %to implement the coefficient x(1)+x(2)>0
c(1)=-x(1)-x(2)
ceq=[]
end

답변 (2개)

Shravan Kumar Vankaramoni
Shravan Kumar Vankaramoni 2021년 4월 4일

Matt J
Matt J 2021년 4월 4일
편집: Matt J 2021년 4월 5일
The unnecessary use of nonlinear constraints can slow things down. Your constraints are in fact linear, and should be expressed like this,
x0=[1 -1];
A=[-1,-1];
b=[0];
lb=[0,-inf];
[xf,fval]=fmincon(fun,x0,A,b,[],[],lb,[]);
Also, the way you have expressed your objective function should be giving you errors. You almost surely want this, instead of what you have above:
lamda=lamda(:); sigma_c=sigma_c(:);
fun=@(x) norm( (-(x(1) + x(2)./lamda).*(2./lamda - 2*lamda.^2))-sigma_c).^2 ;

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by