MATLAB: Geting value as Inf

functions

Dear All
i am solving a equation after first iteration i got value as infinity, actually i want the value of x(1)to x(20), Lembda(1) to Lembda(2), c(1) to c(20), P(1)to p(20) and a(1) to a(20). when i am running code only i am able to get the value of first iteration. after first iteration x(2) value become inf. here is the code.
M=100;
r=0.8;
alpha=0.45;
beta=0.75;
gama=0.1;
d=0.004;
h=0.4;
n=100;
c0=1500;
nn=10;
x = zeros(nn,1);
a = zeros(nn,1);
p = zeros(nn,1);
c = zeros(nn,1);
lembda = zeros(nn,1);
delT=1;
c(1)=1580;
x(1)=2;
a(1)=310000;
p(1)=1635;
lembda(1)=150;
for t=1:nn
q(t)=0.8;
xxx=t+1;
x(xxx)=x(t)+delT*alpha+beta*log10(a(t))+gama*x(t)*(M-x(t))*exp(-d*p(t))*exp(h*q(t));
disp('x(t)');
x(xxx)
gx=exp(-d*p(t))*exp(h*q(t))*(gama*M-alpha-beta*log10(a(t))-2*gama*x(t));
cx=n*q(t)^(x(t)-1)*log10(q(t));
gt=(alpha+beta*log10(a(t))+gama*x(t))*(M-x(t))*exp(-d*p(t))*exp(h*q(t));
lembda(xxx)=lembda(t)+delT*(r*lembda(t)-gx*(p(t)-c(t)+lembda(t))+cx*gt);
disp('lembda(t+1)');
lembda(xxx)
c(t)=n*q(t)^(x(t)-1)+c0;
disp('c(t)');
c(t)
p(t)=c(t)-lembda(t)+1/d;
disp('p(t)');
p(t)
a(t)=(p(t)-c(t)+lembda(t))*beta*(M-x(t))*exp(-d*p(t))*exp(h*q(t));
disp('a(t)');
a(t)
end
please help me to get my desired result.
Thank You

Best Answer

Here's what I think your code should look like in order to run properly:
M=100;
r=0.8;
alpha=0.45;
beta=0.75;
gama=0.1;
d=0.004;
h=0.4;
n=100;
c0=1500;
nn=10;
x = zeros(nn,1);
a = zeros(nn,1);
p = zeros(nn,1);
c = zeros(nn,1);
q = 0.8*ones(nn,1);
lembda = zeros(nn,1);
delT=1;
c(1)=1580;
x(1)=2;
a(1)=310000;
p(1)=1635;
lembda(1)=150;
for t=1:nn-1
xxx=t+1;
x(xxx)=x(t)+delT*alpha+beta*log10(a(t))+gama*x(t)*(M-x(t))*exp(-d*p(t))*exp(h*q(t));
disp('x(t)');
x(xxx)
gx=exp(-d*p(t))*exp(h*q(t))*(gama*M-alpha-beta*log10(a(t))-2*gama*x(t));
cx=n*q(t)^(x(t)-1)*log10(q(t));
gt=(alpha+beta*log10(a(t))+gama*x(t))*(M-x(t))*exp(-d*p(t))*exp(h*q(t));
lembda(xxx)=lembda(t)+delT*(r*lembda(t)-gx*(p(t)-c(t)+lembda(t))+cx*gt);
disp('lembda(t+1)');
lembda(xxx)
c(xxx)=n*q(xxx)^(x(xxx)-1)+c0;
disp('c(t)');
c(xxx)
p(xxx)=c(xxx)-lembda(xxx)+1/d;
disp('p(t)');
p(xxx)
a(xxx)=(p(xxx)-c(xxx)+lembda(xxx))*beta*(M-x(xxx))*exp(-d*p(xxx))*exp(h*q(xxx));
disp('a(t)');
a(xxx)
end
Note that I took q(t) out of the loop. It's really just a constant so you shouldn't even have it as a vector. This still gives me infs after a few iterations, but that's just because you're raising a number to a VERY large number (0.8^-1.33E39). You're also taking the log10 of a negative number in there so you may want to switch all of your log10(a(t)) commands to log10(abs(a(t))).
Related Question