Error at finding minimum
Afficher commentaires plus anciens
Hi, I try to find the minimum of a sum. It does work for a fixed parameter for 'test', but I want to show it for various values of 'test'. I always get the error saying: "Index exceeds array bounds" in the line where I define 'c_hat = single(c_hat(1))', what is wrong in my code? (In the end I want to plot the results for each value of test(j), I deleted this in the code shared to make it more simple.) I would be very thankful if you can help me!
lambda = 0.95; %close to 1
n = 100; %number of samples
theta = 0.03;
alph = 2.5; %divergence level
%Generate Pareto distributed r.v.:
x_m = 1; %scale parameter for Pareto
alpha = 3; %shape parameter for Pareto
pd = makedist('uniform');
u = random(pd,[1,n]);
X = zeros(1,n);
for i=1:n
X(i) = x_m / (1-u(i))^(1/alpha); %Pareto distributed r.v.
end
a_1 = x_m / (1-lambda).^(1/alpha)
V = zeros(n,1);
a=a_1;
for i=1:n
V(i) = 1/(1-lambda) .* max(X(i)-a,0) + a;
end
k=20;
test=linspace(0,10,k);
E=zeros(k,1);
rel_entropy=zeros(k,1);
alph_div = zeros(k,1);
a_hat = zeros(k,1);
for j=1:k
syms c
summe_5=0;
for i=1:n
summe_5 = summe_5 + (test(j)*(alph -1)*V(i) + c)^(1/(alph-1));
end
cond = (1/n)* summe_5 == 1;
c_hat = zeros(3,1);
for q = 1:3
c_hat = vpasolve(cond,c, 'random', true) ;
end
c_hat = single(c_hat(1))
%syms b
size(c_hat)
M_b = @(b) ((alph-1)/alph) * sum((test(j).*(alph-1) .* (1/(1-lambda) .* max(X-b,0)+b) + c_hat).^(1/(alph-1)).*(1/(1-lambda) .* max(X-b,0)+b)) + (c_hat/(test(j) .* alph * (1-alph)));
a_hat(j) = fminsearch(M_b,a_1);
V_a = zeros(n,1);
for i=1:n
V_a(i) = 1/(1-lambda) .* max(X(i)-a_hat(j),0) + a_hat(j);
end
m_hat = zeros(n,1);
for i=1:n
m_hat(i) = (test(j) * (alph-1) * V_a(i)+ c_hat) .^(1/(alph-1));
end
E(j)= (1/n) * sum(V.*m_hat);
%plotting results
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!