how do i calculate a 3d graph in this case?

1 view (last 30 days)
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)

Accepted Answer

Image Analyst
Image Analyst on 23 Oct 2022
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 Comment
Cristina Anna Rossi
Cristina Anna Rossi on 23 Oct 2022
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!

Sign in to comment.

More Answers (2)

Torsten
Torsten on 23 Oct 2022
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.
  1 Comment
Cristina Anna Rossi
Cristina Anna Rossi on 23 Oct 2022
thanks a lot, I absolutely did not think of that!

Sign in to comment.


Can Atalay
Can Atalay on 23 Oct 2022
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.
  1 Comment
Cristina Anna Rossi
Cristina Anna Rossi on 23 Oct 2022
oh, thank you so much I absolutely did not think of that!

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!