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)

采纳的回答

Image Analyst
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;
  1 个评论
Cristina Anna Rossi
Cristina Anna Rossi 2022-10-23
Oh thank you! Yes this is what I needed, thank you so much!
I did remove all of my comments because they felt silly, but I will remember to keep them for future questions! I tend to panic a bit using matlab, this has helped me a lot, though, so thank you!

请先登录,再进行评论。

更多回答(2 个)

Torsten
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
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 CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by