how can I change the parameter of N in following fmincon? - matlab

I have to solve the following system of nonlinear algebraic equations via fmincon. the problem is that fmincon should try to find the best case when my condition (x(N)-7.6<=Tol) can be satisfied until then fmincon needs to run again. so we need to use some loop to update the number of running fmincon and creating new initial condition either. but it seems there is no change of amount of N. how should I do?
function [x]=runnested(x0,N)
r=ones(4,1);
N=length(r);
Tol=0.001;
for i=1:N
x0=rand(5*N+13,1)
options = optimset('Largescale','off','algorithm','interior-point','Display','iter');
[x(i,:),fval,exitflag,output]=fmincon(#(x) norm(myfoctest(x)),x0,[],[],[],[],[],[],#myfoctest,options)
while x(N)-7.61>Tol
N=N+1;
end
end
function [C,Ceq]=myfoctest(x,N,r)
C=[];
r=ones(4,1);
N=length(r);
Ceq=zeros(5*N+13,1);
for j=1:N-1
Ceq(j)=x(3*N+1+j)-3*N+j)-2*x(4*N+1+j)*Ts*f*sin(x(2*N+1+j))./(pi*sin(i1)*x(j)^2)
Ceq(N+j)=x(4*N+1+j)-x(4*N+j)
Ceq(2*N+1+j)=x(3*N+1+j)*Ts*f*sin(x(2*N+1+j))+2*x(4*N+1+j)*Ts*f*cos(x(2*N+1+j))./(pi*x(j)*sin(i1))
Ceq(3*N+8+j)=x(j)-x(j+1)-Ts*f*cos(x(2*N+1+j))
Ceq(4*N+8+j)=Omeg0-x(j+1)+2*Ts*f*sin(x(2*N+1+j))./(pi*x(j)*sin(i1))
end
Ceq(N)=x(5*N+10)-x(5*N+9)-x(3*N+2)
Ceq(2*N)=x(5*N+12)-x(5*N+11)-x(4*N+2)
Ceq(2*N+1)=x(3*N+1)*Ts*f*sin(x(2*N+1))+2*x(4*N+1)*Ts*f*cos(x(2*N+1))/(pi*V0*sin(i1))
Ceq(3*N+1)=1-x(5*N+9)*b1-x(5*N+10)*b1-x(5*N+11)*b2-x(5*N+12)*b2-x(5*N+8)*N*Ts/100-x(5*N+13)
Ceq(3*N+2)=-2*x(5*N+8)*x(5*N+2)
Ceq(3*N+3)=-2*x(5*N+9)*x(5*N+3)
Ceq(3*N+4)=-2*x(5*N+10)*x(5*N+4)
Ceq(3*N+5)=-2*x(5*N+11)*x(5*N+5)
Ceq(3*N+6)=-2*x(5*N+12)*x(5*N+6)
Ceq(3*N+7)=2*x(5*N+13)*cos(x(5*N+7))*sin(x(5*N+7))
Ceq(3*N+8)=V0-x(1)-Ts*f*cos(x(2*N+1))
Ceq(4*N+8)=Omeg0-x(N+1)+2*Ts*f*sin(x(2*N+1))/(pi*V0*sin(i1))
Ceq(5*N+8)=-x(5*N+2)^2-N*Ts/100-N*Ts*x(3*N+1)/100
Ceq(5*N+9)=-x(5*N+3)^2-x(N)+a1+b1-b1*x(3*N+1)+7.61/100
Ceq(5*N+10)=-x(5*N+4)^2+x(N)+a1+b1-b1*x(3*N+1)-7.61/100
Ceq(5*N+11)=-x(5*N+5)^2-x(2*N)+a2+b2-b2*x(3*N+1)+0.35/100
Ceq(5*N+12)=-x(5*N+6)^2+x(2*N)+a2+b2-b2*x(3*N+1)-0.35/100
Ceq(5*N+13)=-(sin(x(5*N+7)))^2-x(5*N+1)
end
end

Related

How to write a MATLAB code for this kind of Heaviside step function?

To solve one dimensional advection equation denoted by
u_t+u_x = 0, u=u(x,t), and i.c. u(x,0)= 1+H(x+1)+H(x-1)
using Lax Wanderoff method,
I need to write a Heaviside step function H(x) and it needs to be zero when x <= 0, 1 when x>0 . The problem is I also need to use that function writing H(x-t+1), H(x-t-1) as I will compare what I find by the exact solution:
u(x,t) =1 + H(x-t+1) -H(x-t-1)
Here, the "x" and "t" are vectors such that;
x=-5:0.05:5
t=0:0.05:1
I wrote the Heaviside step function as the following; however, I need it without the for loop.
L=length(x)
function H_X= heavisidefunc(x,L)
H_X=zeros(1,L);
for i= 1:L
if x(i)<= 0
H_X(i)=0;
else
H_X(i)=1;
end
end
end
I get "Dimensions must agree." error if I write
H_X3 = heavisidefunc(x-t+1,L);
H_X4 = heavisidefunc(x-t-1,L);
The Heavyside function is really easy to program in Matlab
Heavyside=#(x) x>= 0;
The easiest way to get rid of the dimensions must agree error is to transpose one of the vectors. This will cause Matlab to construct a matrix of length(x1) by length(x2)
Heavyside(x-t'+1);
I came up with a solution. My new Heaviside function is;
function H_X= heavisidefunc(x)
if x<= 0
H_X=0;
else
H_X=1;
end
end
The problem I had was because I was storing the output as a vector and it just complicated things.
Now,writing H(x-t+1), H(x-t-1) is easier. Just put "heavisidefunc(x(i)-t(j)-1)" and loop from 1 to the length of x and l in two loops. Thanks to everyone!

hessian for inequality constraint in fmincon

I am trying to help fmincon to converge faster by supplying gradient vector and Hessian matrix. I am using interior-point algorithm and I realize that in such case, I have to supply Hessian using a call to another function which is assigned to HessFcn of my OPTIOINS. I have only inequality constraint (C). it is in quadratic form.
gbee_r_i,p_in,nel,nhp are known matrices or variables.
I define constraint as below:
function [ c,ceq,DC,DCeq] = cond5( x,gbee_r_i,p_in,nel,nhp)
nn=0;
bb=0;
for i=1:nel
for j=1:nhp
nn=nn+1;
bb(nn,:)=1*x'*(gbee_r_i(:,:,j,i))'*p_in*gbee_r_i(:,:,j,i)*x;
rr(:,nn)=(gbee_r_i(:,:,j,i))'*p_in*gbee_r_i(:,:,j,i)*x;
end
end
DCeq=[];
DC=rr;
ceq=[];
c=bb;
end
Define options like this:
options = optimoptions(#fmincon,...
'GradObj','on','GradConstr','on','Hessian','user-supplied',...
HessFcn',#(x)hessinterior(x,lambda,gbee_r_i,p_in,nel,nhp),'Display',...
'iter','Algorithm','interior-point','maxFunEvals',20000000000000,'MaxIter',5000,'TolFun',1e-3);
`#(x)hessinterior(x,lambda,gbee_r_i,p_in,nel,nhp)
is HessFcn and is defined as below.
function [ h ] = hessinterior(x,lambda,gbee_r_i,p_in,nel,nhp)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
nn=0;
h=0;
for i=1:nel
for j=1:nhp
nn=nn+1;
h=h+lambda.ineqnonlin(nn)*(gbee_r_i(:,:,j,i))'*p_in*gbee_r_i(:,:,j,i);
end
end
end
after running the program this error is shown
Error using #(x)hessinterior(x,lambda,gbee_r_i,p_in,nel,nhp)
Too many input arguments.
Error in C:\Program Files\MATLAB\R2013a\toolbox\optim\optim\private\computeHessian.p>computeHessian
(line 36)
Error in C:\Program Files\MATLAB\R2013a\toolbox\optim\optim\barrier.p>barrier (line 300)
Error in fmincon (line 900)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
You need to make a function that is defined such as
hess = #(x,lambda) cond5( x,lambda,gbee_r_i,p_in,nel,nh)
Then define your options as (You need to define it below the above
options = optimoptions(#fmincon,...
'GradObj','on','GradConstr','on','Hessian','user-supplied',...
HessFcn',hess,'Display','iter','Algorithm','interior-point','maxFunEvals',20000000000000,'MaxIter',5000,'TolFun',1e-3);

changing ObjectiveLimit option in fminunc in matlab

I have a noisy picture that I want to denoise, with specific energy function, my function have 3 free variable which I can change them until the energy function converge to the minimum value,I found values of these 3 variable by testing different value and run the program, but I want to know how can I find them by a good optimization algorithm. as far as I know I can use fminunc function in matlab but it gives me an error:
fminunc stopped because the objective function value is less than or equal to the default value of the objective function limit.
h,beta,v is the 3 variable which initialize by h=0.8,beta=4,v=0.9
here is the enrgy formula
and also my code:
load Input_images.mat;
X=noisyImg();
Z=X;
[n,m]=size(Z);
c=1;
for c=1:5
c
for i=1:n
for j=1:m
[neighbours] = neighbour(i,j,n,m,Z);
sumneighpos=sum(Z(i,j)*neighbours(1,:)');
inputnoisypos=(Z(i,j)*X(i,j));
noisyzpos=h*Z(i,j);
sumpos=noisyzpos-(beta*sumneighpos)-(v*inputnoisypos);
sumneg=-sumpos;
if sumneg<sumpos
Z(i,j)=-Z(i,j);
end
end
end
end
code for the optimization part.
f = #(q,w) h*Z(i,j)-q*sumneighpos-w*inputnoisypos;
fun = #(x) f(x(1),x(2));
x0 = [0.9; 4];
options = optimoptions('fminunc','Algorithm','quasi-newton');
[x, fval, exitflag, output] = fminunc(fun,x0,options);
here I have 2 question how can I fix the error and second one, is there any better algorithm?

Matlab Fmincon "too many output arguments"

I am solving a very simple constrained optimization problem. At this point, I have only entered a constraint that makes the (L-2) vector norm equal 1 and later I hope to add non-negativity constraints.
Fmincon is giving me a "Too many output arguments" on the my constraint. I don't understand why.
Objective function: A simple Quadratic form. Actually a variance covariance Matrix, I am entering as a pre-calculated global variable.
function [y, grady] = quadobj(x)
global Q
y = x*Q*x';
if nargout > 1
grady = 2*Q*x;
end
Equality Constraint: that vector L2 norm should be 1.
function outeq = confuneq2(x)
% Nonlinear equality constraints
outeq = x*x'-1;
end
Fmincon.
x0 = [0.7,0.1, -0.69];
options = optimoptions(#fmincon,'Algorithm','sqp');
[x,fval] = fmincon(#quadobj,x0,[],[],[],[],[],[],...
#confuneq2,options);
But it's not working. I am getting the following error.
Error using confuneq2
Too many output arguments.
Error in fmincon (line 632)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue
Please help!
Confusingly, the problem is that your function has too few output arguments. If you look at the error, it is telling you that MATLAB is trying to call your function with two output arguments but you've programmed it to take only one. Thus it errors because it has called your function with too many output arguments.
All the examples in the docs have two outputs so try create your function this way:
function [out, outeq] = confuneq2(x)
out = x*x'-1;
outeq = [];
end

Lsqr first iteration with no initial guess

I am trying to solve
argmin||W_vector*FT^-1(Ax)-W_vector*B||
using lsqr with a function handle and without any initial guess (W_vector is a weighting vector). I thought that lsqr would have performed the first step computing x by C'b (call C=W*FT^-1(A)) using the vector b=W_vector.*B I passed to the function. This is the code:
b=W_vector.*B;
[x, flag]=lsqr(#(x1,modo)FUNC(x1,W_vector,A,modo),b,tol,maxit);
function [result, modo]=FUNC(x1,W_vector,A,modo)
%Computes y=A*x for modo='notransp'
%Computes y=A'*x for modo='transp'
switch modo
case 'notransp'
res=A*x1;
R1=reshape(res,norient,dim(1)*dim(2)*dim(3));
for co=1:norient
R2(:,:,:,co)=reshape(R1(co,:),dim(1),dim(2),dim(3));
FR(:,:,:,co)=ifftn(R2(:,:,:,co));
aux=FR(:,:,:,co);
R3(co,:)=aux(:).';
end
result=W_vector.*R3(:);
case 'transp'
R1=reshape(x1./(W_vector+eps),norient,dim(1)*dim(2)*dim(3));
for co=1:norient
R2(:,:,:,co)=reshape(R1(co,:),dim(1),dim(2),dim(3));
FR(:,:,:,co)=fftn(R2(:,:,:,co));
aux=FR(:,:,:,co);
R3(co,:)=aux(:).';
end
result=A'*R3(:);
end
end
When I checked the result of R1=reshape(x1./(W_vector+eps),norient,dim(1)*dim(2)*dim(3)); in case 'transp' in the first iteration, I found out that x1 is not equal to b (after reshape, they are images, so I can see that look qualitatively the same but with very different values). I would like to understand how lsqr works because I am having some problems with weightings. Thanks for your help.