I have a little problem, fseminf used to work just fine, but now as I reached to it, it happend to produce an error, I have no idea what happend. Here is the code with an example which does not work:
clc;
clear;
close;
% Zdefiniuj funkcje
global g f S
f = #(x) (x(1)-2).^2 + (x(2)-0.2).^2;
g = #(x,s) 5.*x(1).^2.*sin(pi.*sqrt(s))./(1+s.^2) - x(2);
lb = [-1,0];
ub = [1 ,0.2];
S = [0,1];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [0,0]';
[x,fval,exitflag,output,lambda] = fseminf(f,x0,1,#seminfcon2,A,b,Aeq,beq,lb,ub);
disp('x= ')
x
And seminfcon is defined:
function [c, ceq, K1, s] = seminfcon2(x,s)
global S g
% Dodatkowe parametry
c = [];
ceq = [];
if isnan(s(1,1))
s(1,1)=0.001;
s(1,2)=0;
end
t = S(1):s(1):S(2);
K1 = g(x,t);
The error is when calculating x, meaning in fseminf
Related
I wrote the following formula in order to solve a minimization problem. But when I run the code, I always get the following error:
Error using barrier
Objective function is undefined at initial point. Fmincon cannot continue.
function out = Objectivefunction(x,r,q,K,T,S0,type,marketprice)
%loss function to find optimal heston parameters
%rho = x(5);
%kappa = x(2);
%theta = x(4);
%eta = x(3);
%vol0 = x(1);
%sigma0 = sqrt(vol0);
integration_rule = 1;
Price = zeros(length(T),1);
diffr = zeros(length(T),1);
%FFTCarrMadanHeston(kappa, eta, theta, rho, sigma0, K, T, S0, r, q, type, integrationrule)
%price using FFT
for t = 1:length(T)
Price(t,:) = FFTCarrMadanHeston(x(2), x(3), x(4), x(5), sqrt(x(1)), K(t), T(t), S0, r(t), q, type(t), integration_rule);
diffr(t,:) = marketprice(t) - Price(t);
%diffr(t,:) = (abs(marketprice(t) - Price(t)))/Price(t);
end
out = sqrt(sum((diffr).^2)/length(marketprice));
end
% calibration
v0 = 0.0654; %
kappa0 = 0.6067;
eta0 = 0.0707;
theta0 = 0.2928;
rho0 = -0.7571;
x0 = [v0,kappa0,eta0,theta0,rho0];
x0;
% computation of the interest rate for each maturity
r = spline(interest_rate(:,1), interest_rate(:,2), T);
T = T/360; % maturity in years
%calibration parameters
A = [];
b = [];
Aeq = [];
beq = [];
e = 1e-5;
lb = [e e e e -.999]; % Lower bound on the estimates
ub = [100 10 10 10 .999]; % Upper bound on the estimates
optimpar = fmincon(#(x)Objectivefunction(x,r,q,K,T,S0,type,marketprice), x0, A,b, Aeq, beq, lb, ub);
I do get an outcome when I calculate the value of the objective function in the intitial point. However, once I do it in the fmincon function , it doesn't work anymore
I would like to use MATLAB function fmincon using a gradient vector only (without scalar function). But I have trouble with it. For instance, I tried the following, but it doesn't work. Any help please? Thanks!
fun = #rosenbrockwithgrad;
x0 = [-1,2];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [-2,-2];
ub = [2,2];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
function [grad] = rosenbrockwithgrad(x)
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
You need to include both the function and its gradient. See below:
options = optimoptions('fmincon','SpecifyObjectiveGradient',true);
fun = #rosenbrockwithgrad;
x0 = [-1,2];
A = [];
b = [];
Aeq = [];
beq = [];
lb = [-2,-2];
ub = [2,2];
nonlcon = [];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
function [f,g] = rosenbrockwithgrad(x)
% Calculate objective f
f = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
if nargout > 1 % gradient required
g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
end
This code should run properly.
I am trying to run fseminf function
f = #(x) (x(1)-2).^2 + (x(2)-0.2).^2;
g = #(x,s) 5*x(1).^2*sin(pi*sqrt(s))/(1+s.^2) - x(2);
lb = [-1,0];
ub = [1 ,0.2];
S = [0,1];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [0,0];
x = fseminf(f,x0,1,#seminfcon1,A,b,Aeq,beq,lb,ub);
Where:
function [c, ceq, K1, s] = seminfcon1(x,s)
global S g
% No finite nonlinear inequality and equality constraints
c = [];
ceq = [];
% Sample set
if isnan(s)
% Initial sampling interval
s = [0.0001 0];
end
t = S(1):s(1):S(2);
% Evaluate the semi-infinite constraint
K1 = g(x,t);
This is the code I use for creating a finite set from S, which is an interval. I do not understand how it works, why do we use isnan function instead of just declaring s=[0.0001] (which by the way does not work). It is used for fseminf alghoritm, I took the code from an example.
I have a MINLP objective function and I want to fix some variables value into constant as an example described below:
A = [1 1 1];
b = 30;
x1 = zeros(1,3);
y=1;
x = fmincon(#(x)objfun(x,y),x1,A,b);
function f = objfun(x,y)
x(y) = 1;
f = x(1)^2 + x(2)^2 + x(3)^2;
end
However, the result of variable x is all zeros. It seems that x(1) cannot be forced to be 1. How to fix this problem?
You should use a different syntax of fmincon:
fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Then, if you wish to only limit one of the values, you can use these bounds:
lb = [1 -Inf -Inf];
ub = [1 Inf Inf];
Since you will also need to specify the inputs Aeq and beq, don't forget you can use [] for any inputs you don't want/need to specify, as shown in this example in the documentation:
fun = #(x)1+x(1)./(1+x(2)) - 3*x(1).*x(2) + x(2).*(1+x(1));
lb = [0,0];
ub = [1,2];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [0.5,1];
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
My question this time concerns the obtention of the degree distribution of a LDPC matrix through linear programming, under the following statement:
My code is the following:
function [v] = LP_Irr_LDPC(k,Ebn0)
options = optimoptions('fmincon','Display','iter','Algorithm','interior-point','MaxIter', 4000, 'MaxFunEvals', 70000);
fun = #(v) -sum(v(1:k)./(1:k));
A = [];
b = [];
Aeq = [0, ones(1,k-1)];
beq = 1;
lb = zeros(1,k);
ub = [0, ones(1,k-1)];
nonlcon = #(v)DensEv_SP(v,Ebn0);
l0 = [0 rand(1,k-1)];
l0 = l0./sum(l0);
v = fmincon(fun,l0,A,b,Aeq,beq,lb,ub,nonlcon,options)
end
Definition of nonlinear constraints:
function [c, ceq] = DensEv_SP(v,Ebn0)
% It is also needed to modify this function, as you cannot pass parameters from others to it.
h = [0 rand(1,19)];
h = h./sum(h); % This is where h comes from
syms x;
X = x.^(0:(length(h)-1));
R = h*transpose(X);
ebn0 = 10^(Ebn0/10);
Rm = 1;
LLR = (-50:50);
p03 = 0.3;
LLR03 = log((1-p03)/p03);
r03 = 1 - p03;
noise03 = (2*r03*Rm*ebn0)^-1;
pf03 = normpdf(LLR, LLR03, noise03);
sumpf03 = sum(pf03(1:length(pf03)/2));
divisions = 100;
Aj = zeros(1, divisions);
rho = zeros(1, divisions);
xj = zeros(1, divisions);
k = 10; % Length(v) -> Same value as in 'Complete.m'
for j=1:1:divisions
xj(j) = sumpf03*j/divisions;
rho(j) = subs(R,x,1-xj(j));
Aj(j) = 1 - rho(j);
end
c = zeros(1, length(xj));
lambda = zeros(1, length(Aj));
for j = 1:1:length(xj)
lambda(j) = sum(v(2:k).*(Aj(j).^(1:(k-1))));
c(j) = sumpf03*lambda(j) - xj(j);
end
save Almacen
ceq = [];
%ceq = sum(v)-1;
end
This question is linked to the one posted here. My problem is that I need that each element from vectors v and h resulting from this optimization problem is a fraction of x/N and x/(N(1-r) respectively.
How could I ensure that condition without losing convergence capability?
I came up with one possible solution, at least for vector h, within the function DensEv_SP:
function [c, ceq] = DensEv_SP(v,Ebn0)
% It is also needed to modify this function, as you cannot pass parameters from others to it.
k = 10; % Same as in Complete.m, desired sum of h
M = 19; % Number of integers
h = [0 diff([0,sort(randperm(k+M-1,M-1)),k+M])-ones(1,M)];
h = h./sum(h);
syms x;
X = x.^(0:(length(h)-1));
R = h*transpose(X);
ebn0 = 10^(Ebn0/10);
Rm = 1;
LLR = (-50:50);
p03 = 0.3;
LLR03 = log((1-p03)/p03);
r03 = 1 - p03;
noise03 = (2*r03*Rm*ebn0)^-1;
pf03 = normpdf(LLR, LLR03, noise03);
sumpf03 = sum(pf03(1:length(pf03)/2));
divisions = 100;
Aj = zeros(1, divisions);
rho = zeros(1, divisions);
xj = zeros(1, divisions);
N = 20; % Length(v) -> Same value as in 'Complete.m'
for j=1:1:divisions
xj(j) = sumpf03*j/divisions;
rho(j) = subs(R,x,1-xj(j));
Aj(j) = 1 - rho(j);
end
c = zeros(1, length(xj));
lambda = zeros(1, length(Aj));
for j = 1:1:length(xj)
lambda(j) = sum(v(2:k).*(Aj(j).^(1:(k-1))));
c(j) = sumpf03*lambda(j) - xj(j);
end
save Almacen
ceq = (N*v)-floor(N*v);
%ceq = sum(v)-1;
end
As above stated, there is no longer any problem with vector h; nevertheless the way I defined ceq value seemed to be insufficient to make the optimization work out (the problems with v have not diminished at all). Does anybody know how to find the solution?