Subscript indices must either be real positive integers or logicals

1 vue (au cours des 30 derniers jours)
Javier
Javier le 14 Juil 2013
I am trying to make a while-loop for an fsolve function. I am getting the error message Subscript indices must either be real positive integers or logicals
But this is strange because i can solve the first two values of the iteration, and the problem arises when matlab is trying to input c=3 I can see in the matlab screen that when c starts changing it is first set as c=1 c=2
But then for the third value it says c=3.0000
I don't know why matlab is storing this number n a different way
This is the code
c=1
psi_var=0.1
theta_starb=zeros(10,1)
while c<11
aux1 = @(x)(beta1-1) * exp(psi_var/2) * (x / (r-alpha_prime)) * (1 - exp(-tau*(r-alpha_prime)));
aux2 = @(x)C2 * x^beta1 * exp(beta1*alpha*tau+1/2*beta1^2*(psi_var + tau*sigma^2));
aux3 = 1/sqrt(psi_var + tau*sigma^2);
aux4 = @(x)normpdf(-beta1*sqrt(psi_var + tau*sigma^2)+(-log(x)-alpha*tau)/sqrt(psi_var + tau*sigma^2));
cdf4 = @(x)normcdf(-beta1*sqrt(psi_var + tau*sigma^2)+(-log(x)-alpha*tau)/sqrt(psi_var + tau*sigma^2)); %Not neccesary here but part of omega
aux6 = @(x) C1 * x^beta2 * exp(beta2*alpha*tau+1/2*beta2^2*(psi_var + tau*sigma^2));
aux7 = @(x)normpdf(beta2*sqrt(psi_var + tau*sigma^2)-(-log(x)-alpha*tau)/sqrt(psi_var + tau*sigma^2)); %I deleted the first -
cdf7 = @(x)normcdf(beta2*sqrt(psi_var + tau*sigma^2)-(-log(x)-alpha*tau)/sqrt(psi_var + tau*sigma^2));%added
aux9 = @(x)x/(r-alpha_prime) * exp(alpha*tau+1/2*(psi_var + tau*sigma^2)); %I added r-alpha
aux10 = @(x)normpdf(sqrt(psi_var + tau*sigma^2)-(-log(x)-alpha*tau)/sqrt(psi_var + tau*sigma^2)); %I deleted the first -
cdf10 = @(x) normcdf(sqrt(psi_var + tau*sigma^2)-(-log(x)-alpha*tau)/sqrt(psi_var + tau*sigma^2));%added
aux12 = @(x)normpdf(-(-log(x)-alpha*tau)/sqrt(psi_var + tau*sigma^2));
cdf12 = @(x)normcdf(-(-log(x)-alpha*tau)/sqrt(psi_var + tau*sigma^2));%added
y = @(x) aux1(x) + exp(-r*tau) * (aux2(x)*aux3*aux4(x) + aux6(x)*((beta1-beta2)*cdf7(x)-aux3*aux7(x)) + aux9(x)*((beta1-1)*cdf10(x)-aux3*aux10(x)) + -1/r * (beta1*cdf12(x) - aux3*aux12(x))) - beta1*S/F; % it is the same as before
theta_starb(c) = fsolve(@(x) y(x), F+S);
psi_var=psi_var+0.1
c=10*psi_var
end

Réponse acceptée

the cyclist
the cyclist le 14 Juil 2013
Javier,
Your question is very closely relate to this one:
and illustrates why one needs to be very careful using floating point numbers as loop variables.
Notice what happens:
>> 10*(0.1) - 1
>> 10*(0.1+0.1) - 2
>> 10*(0.1+0.1+0.1) - 3
[See that prior thread for what is going on there.]
In your case, I think the simple solution is to increment c directly:
c = c+1
rather than relying on multiplying psi_var by 10.
  1 commentaire
Javier
Javier le 14 Juil 2013
Thank you very much!
i´ve just solved it with the command c=round(c)
But this made me understand the problem.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by