error in displaying graph

1 view (last 30 days)
summyia qamar
summyia qamar on 6 Jan 2018
Answered: Star Strider on 6 Jan 2018
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

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jan 2018
D and Q are identical and so draw on top of each other.

More Answers (1)

Star Strider
Star Strider on 6 Jan 2018
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.

Categories

Find more on Price and Analyze Financial Instruments in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!