Why my code is not working?
1 次查看(过去 30 天)
显示 更早的评论
I have a small problem, I need resover a problem through the quadratic penalty method, I have done the objective function and constraints of equality and inequality, but now I have a problem in the file you use the fminsearch function, does anyone can help me. I did the following code.
func.m
if true
function [y,rd,ri]=func(x)
A=[5.22 3.11 0.01 1.02 0.00;0.00 2.12 1.03 0.05 1.04;1.12 0.19 3.23 1.05 2.03;2.89 3.76 0.74 4.32 0.00;0.01 1.99 3.88 2.01 5.55];
y=x'*A*x;
%constraints inequality (rd>=0)
rd=[x(1);-x(1)+1;x(2);-x(2)+1;x(3);-x(3)+1;x(4);-x(4)+1;x(5);-x(5)+1];
%constraints (ri=0)
ri=[sum(x)-5];
end
end
funobj_qpm.m
if true
function y=funobj_qpm(funobj,miu,x)
% quadratic penality
[yy,rd,ri]=funobj(x);
y=yy+(1/(2*miu))*sum(ri.^2)+(1/(2*miu))*sum(max(-rd,0).^2);
end
end
penaltyQ1.m
if true
function [xmin,fxmin,it,errorx,rdxmin,rixmin]=penaltyQ1(funobj,x0)
kmax=100; %numero maximo de interacoes
errmax=10^(-7); % erro maximo entre duas solucoes
nx=length(x0);
[y,rd,ri]=funobj(x0)
nrd=length(rd);
nri=length(ri);
c=0.7;
miu=1;
k=0;
err=1;
while (k<kmax)&&(err>errmax) Q=@(x) funobj_qpm(funobj,miu,x); x1=fminsearch(Q,x0) % funcao objectivo passa a ser Q miu=c*miu; err=norm(x1-x0,2); %quadratic norm, (1 norm (1), infinite norm (inf)) x0=x1; k=k+1; [y,rd,ri]=funobj(x1); infxmin(k,1)=k; % linha k, faz 1 e sa? a linha k infxmin(k,2:(nx+1))=x1; %linha k, do 2 ao 3, sa? o xm infxmin(k,nx+2)=y; infxmin(k,(nx+3):(nx+nrd+2))=rd; infxmin(k,(nx+nrd+3):(nx+nrd+nri+2))=ri; infxmin(k,nx+nrd+nri+3)=err; end disp('it x* f(x*) rd(x*) ri(x*) erro') disp(infxmin); % solu??o encontrada [y,rd,ri]=funobj(x1); xmin=x1; %minimizante encontrado fxmin=y;% m?nimo encontrado it=k; errorx=err; rdxmin=rd; % valor na restri??o de desigualdade rixmin=ri; % valor na restri??o de igualdade % Como na fun??o temos 'funobj'.'RI' e 'RD' temos de definir fora. end
% Para executar %[xmin,fxmin,it,errorx,rdxmin,rixmin]=penaltyQ1(@func,[0;0;0;0;0]) %[xmin,fxmin,it,errorx,rdxmin,rixmin]=penaltyQ1(@func,[1;1;1;1;1]) %[xmin,fxmin,it,errorx,rdxmin,rixmin]=penaltyQ1(@func,[-1;-1;-1;-1;-1]) %[xmin,fxmin,it,errorx,rdxmin,rixmin]=penaltyQ1(@func,[0;1;0;1;0]) %[xmin,fxmin,it,errorx,rdxmin,rixmin]=penaltyQ1(@func,[2;0;3;0;0]) %[xmin,fxmin,it,errorx,rdxmin,rixmin]=penaltyQ1(@func,[rand(5,1)]) end
this last file penaltyQ1 is the file that have to execute and does not run
2 个评论
per isakson
2015-1-9
Try to figure out how to use the {}Code button works. In this case it would have been better to attach the files, see the paper-clip-button.
Jan
2015-1-9
Please explain the occurring problems. "Does not run" is not useful. Do you get an error message?
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!