error in displaying graph

조회 수: 1 (최근 30일)
summyia qamar
summyia qamar 2018년 1월 6일
답변: Star Strider 2018년 1월 6일
I'm solving the following code but the graph is not showing result of "D":
N=10;
D=zeros(1,N); %demand
Q=zeros(1,N); %supply
P=zeros(1,N); %Price
d=1000; %constant of demand function
q=100; %constant of supply function
a=0.8; % sensitivity of demand to price
b=0.5; %sensitivity of supply to price
for t=2:N
P(t)=(d-q-(b*P(t-1)))/a;
D(t)=d-a*P(t);
Q(t)=q+b*P(t-1);
end
%plot
Xvals=1:N;
plot(Xvals,P,'b',Xvals,D,'r',Xvals,Q,'g')
xlabel('time')
legend('Price','Demand','Quantity')
the output is shown below:
however, when i change the *P* function only, the graph appears:
N=10;
D=zeros(1,N); %demand
Q=zeros(1,N); %supply
P=zeros(1,N); %Price
d=1000; %constant of demand function
q=100; %constant of supply function
a=0.8; % sensitivity of demand to price
b=0.5; %sensitivity of supply to price
for t=2:N
P(t)=(P(t-1)-((d-q)/(a+b)))*((-b/a)^t)+((d-q)/(a+b));
D(t)=d-a*P(t);
Q(t)=q+b*P(t-1);
end
%plot
Xvals=1:N;
plot(Xvals,P,'b',Xvals,D,'r',Xvals,Q,'g')
xlabel('time')
legend('Price','Demand','Quantity')
what can be the error? both results show some values of D

채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 6일
D and Q are identical and so draw on top of each other.

추가 답변 (1개)

Star Strider
Star Strider 2018년 1월 6일
In the first code example:
for t=2:N
P(t)=(d-q-(b*P(t-1)))/a;
D(t)=d-a*P(t);
Q(t)=q+b*P(t-1);
end
both ‘D’ and ‘Q’ have exactly the same values, so ‘Q’ overplots ‘D’.
In the second code example:
for t=2:N
P(t)=(P(t-1)-((d-q)/(a+b)))*((-b/a)^t)+((d-q)/(a+b));
D(t)=d-a*P(t);
Q(t)=q+b*P(t-1);
end
they are not the same, so you see all three.
I leave it to you to determine any error that may be causing that.

카테고리

Help CenterFile Exchange에서 Price and Analyze Financial Instruments에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by