slp method to minimize function

3 views (last 30 days)
Ous Chkiri
Ous Chkiri on 8 Mar 2021
Commented: Rena Berman on 6 May 2021
Error:
Error using SLP>f_SLPclass (line 26)
x should be 5 elements long, but instead it is length 2
Error in SLP (line 6)
[f,g]=f_SLPclass(x);
clear;
clc;
x=[0;10];
Tolx=0.01;
TolCon=0.01;
[f,g]=f_SLPclass(x);
[gradf,gradg]=g_SLPclass(x);
coverged=false;
iter=0;
while converged
c=gradf';
a=gradg';
b=-g;
dx=linprog(c,a,b,[],[]);
x=x+dx;
[f,g]=f_SLPclass(x);
[gradf,gradg]=g_SLPclass(x);
iter=iter+1;
converged=max(abs(dx)<Tolx)&& max(g)<TolCon;
end
fprintf('\n number of iteration %f\n',iter);
fprintf('\n Optimum Point: (%f,%f,%f,%f,%f)\n',x1,x2,x3,x4,x5);
function [f,g]=f_SLPclass(x) %return outputs
assert(length(x)==5, "x should be 5 elements long, but instead it is length " + length(x));
xcell=num2cell(x);
[x1,x2,x3,x4,x5]= deal(xcell{:});
f=0.0624*(x1+x2+x3+x4+x5);
g=-1+61/x1^3 +37/x2^3 +19/x3^3 +7/x4^3 +1/x5^3;
end
function [gradf,gradg]=g_SLPclass(x)
assert(length(x)==5, "x should be 5 elements long, but it is only length " + length(x));
xcell=num2cell(x);
[x1,x2,x3,x4,x5]= deal(xcell{:});
gradf=0.312;
gradg=-183/x1^4 - 111/x2^4 -57/x3^4 -21/x4^4 -3/x5^4;
end
  1 Comment
Rena Berman
Rena Berman on 6 May 2021
(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

Catalytic
Catalytic on 8 Mar 2021
Edited: Catalytic on 8 Mar 2021
The error message was generated by your own code, in this line
assert(length(x)==5, "x should be 5 elements long, but instead it is length " + length(x));
Surely you put it there for a reason. Why aren't you passing a 5-element vector when your function clearly requires it?
  1 Comment
Ous Chkiri
Ous Chkiri on 8 Mar 2021
Because I have x1,x2.x3,x4 and x5

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by