how do i calculate a 3d graph in this case?
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to draw a graph using the following formula, however i have two vectors instead of a matrices and while the values get calculated, I cannot plot them. I want to plot the graph as pi and pp change.
The code actually calculates the results for all the values, then says there is an error in line 33 without explaining what it is.
Thanks to anyone in advance.
q_wheat=240;
q_iron=12;
q_pig=18;
wheat_totale=450;
q_wheat2=120;
q_iron2=8;
q_pig2=12;
iron_totale=21;
q_wheat3=120;
q_iron3=6;
q_pig3=30;
pig_totale=60;
pw=1;
pi=0.1:0.1:40;
pp=0.1:0.1:40;
for i=1:length(pi)
for j=1:length(pp)
cost_w(i,j)=q_wheat*pw+(q_iron*pi(i,j))+(q_pig*pp(i,j))
cost_i(i,j)=q_wheat2*pw+(q_iron2*pi(i,j))+(q_pig2*pp(i,j))
cost_p(i,j)=q_wheat3*pw+(q_iron3*pi(i,j))+(q_pig3*pp(i,j))
rev_wheat(i,j)=wheat_totale*pw;
rev_iron(i,j)=iron_totale*pi(i,j);
rev_pig(i,j)=pig_totale*pp(i,j)
prof_iron(i,j)=rev_iron(i,j)-cost_i(i,j);
prof_wheat(i,j)=rev_wheat(i,j)-cost_w(i,j);
prof_pig(i,j)=rev_pig(i,j)-cost_p(i,j)
riron(i,j)=(rev_iron(i,j)/cost_i(i,j))-1
rwheat(i,j)=(rev_wheat(i,j)/cost_w(i,j))-1
rpig(i,j)=(rev_pig(i,j)/cost_p(i,j))-1
end
end
figure(1), plot(pi,riron,pi,rwheat,pi,rpig)
댓글 수: 0
채택된 답변
Image Analyst
2022년 10월 23일
Not exactly sure what you want since you removed all your comments in the code, which I'm sure you put it (like all good programmers do), but this is what I got. Perhaps it's what you intended, perhaps not.
q_wheat = 240;
q_iron = 12;
q_pig = 18;
wheat_totale = 450;
q_wheat2 = 120;
q_iron2 = 8;
q_pig2 = 12;
iron_totale = 21;
q_wheat3 = 120;
q_iron3 = 6;
q_pig3 = 30;
pig_totale = 60;
pw = 1;
pk = 0.1:0.1:40;
pp = 0.1:0.1:40;
for i = 1:length(pk)
for j = 1:length(pp)
cost_w(i,j) = q_wheat*pw + (q_iron*pk(i)) + (q_pig*pp(j));
cost_i(i,j) = q_wheat2*pw + (q_iron2*pk(i)) + (q_pig2*pp(j));
cost_p(i,j) = q_wheat3*pw + (q_iron3*pk(i)) + (q_pig3*pp(j));
rev_wheat(i,j) = wheat_totale*pw;
rev_iron(i,j) = iron_totale*pk(i);
rev_pig(i,j) = pig_totale*pp(j);
prof_iron(i,j) = rev_iron(i,j)-cost_i(i,j);
prof_wheat(i,j) = rev_wheat(i,j)-cost_w(i,j);
prof_pig(i,j) = rev_pig(i,j)-cost_p(i,j);
riron(i,j) = (rev_iron(i,j)/cost_i(i,j))-1;
rwheat(i,j) = (rev_wheat(i,j)/cost_w(i,j))-1;
rpig(i,j) = (rev_pig(i,j)/cost_p(i,j))-1;
end
plot(pk,riron(i, :), 'r-') % Draw iron with a red curve for this i
hold on
plot(pk,rwheat(i, :), 'g-') % Draw wheat with a green curve for this i
plot(pk,rpig(i, :), 'b-') % Draw pig with a blue curve for this i
end
xlabel('pk')
ylabel('iron, wheat, or pig')
% Draw line along x axis
yline(0, 'LineWidth',2);
grid on;
추가 답변 (2개)
Torsten
2022년 10월 23일
pi and pp are vectors ; thus trying to access pi(i,j) and pp(i,j) will throw an error.
Further, don't name a variable "pi" in order to avoid conflicts with Ludolph number.
Can Atalay
2022년 10월 23일
Haven't been able to run your code but I think that your
pi
is a row vector but you're accessing
pi(2,1)
eventually, which doesn't exist.
You could simply access the nth element in a row vector by saying
pi(3)
instead of
pi(1,3)
Moreover, using something along the lines of
ii1, ii2, ii3, etc. / jj1, jj2... / my_pi, pi1, pi_1
instead of
i, j, pi
would be a good idea since these might create some conflict between the inbuilt definitions of "pi" and "i" in MATLAB.
참고 항목
카테고리
Help Center 및 File Exchange에서 Directed Graphs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!