geting value as Inf
1 回表示 (過去 30 日間)
古いコメントを表示
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
1 件のコメント
Daniel Shub
2012 年 5 月 9 日
While we all appreciate the formatting of the code, when you deleted your previous question, you deleted my answer. This new "question" isn't actually a question, but rather a command. Further, the concept of your desired result, is confusing ...
採用された回答
Kevin Holst
2012 年 5 月 9 日
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))).
3 件のコメント
その他の回答 (1 件)
Kevin Holst
2012 年 5 月 9 日
I actually answered your problem in your previous problem. The error is in your calculations for c, p, and specifically a. These values get calculated and then are placed in the 't' index rather than the 'xxx' ('t+1') index. As a result, when your loop goes through for the second time, you take log10(a(t)) which is log10(0), resulting in -inf. That -inf then cascades into your other calculations.
2 件のコメント
Kevin Holst
2012 年 5 月 9 日
Since I don't actually know what these equations mean or what they're supposed to be doing, I can't give you an exact solution to your problem. However, my answer points out where your error lies.
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!