Message Boards Message Boards

0
|
4352 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Using table to produce an output of 3 variables?

Posted 8 years ago

My homework question states: " My car is about out of gas, and I'm trying to decide which gas station to go to. Some gas is cheaper, but the distance to drive to it is farther. I've researched 5 gas stations. The gas prices per gallon are listed in price. The distance in miles to each station, respectively, is in dist. I want you to make me a table that lists triples of (price, distance, the total cost of buying 13 gallons of fuel). The total cost includes the cost of filling my tank plus the cost of gas required to get me to the station."

I am given price and dist, listed below..Feel free to open the attachment in mathematica and run my code to actually see what I'm trying to do. I posted the code in here also..

price = {3.95, 3.93, 3.89, 3.79 , 3.87};
dist = {1.0, .8, 3.3, 9.6, 7.9};

This is my function I created, I know this is right.

total[price_, dist_] := (price*13) + (price*dist/40); 

I know my function must include a price * 13 because we are interested in the total cost of 13 gallons plus the cost of gas required to get to the station. I must know how much mileage per gallon our car gets so I chose 40 because I'm driving an eco friendly vehicle. ;)

Next is my bit of code to actually run and produce this table I need..

clear[Num5];
Num5 = Table[{dist, price, total[price, dist]}, {price, dist}];
Num5 // TableForm

I know there is something wrong with my above code, I want to produce a table that shows price, dist and total price, but I seem to be getting something else..I've tried altering my code multiple times. I'm pretty stumped. If anyone can lead me ina different direction that would be awesome. I feel like for the most part I am on the right track.

Attachments:
POSTED BY: Brandon Davis
2 Replies

Since this is homework, I'll just give a couple of pointers:

  1. You can do the entire thing within the Table function, similar to what you have done for your Num5 variable (btw, you really shouldn't start a variable name with a capital letter).
  2. I would use Part notation (e.g., price[[i]]) to extract each part of each of your lists for use in the Table function.
  3. The last argument of the Table function should give the range of the counter variable (the i in price[[i]]). I don't think the way that you have it now is doing what you think it is.
POSTED BY: Tim Mayes
Posted 8 years ago

Hey Tim,

I appreciate the input. You're right, the built in commands in Mathematica are capitalized so I can see how that might affect me down the road..I see I am missing some information in my Table function, I will work on that part. Thank you! Its hard to post a question about homework without assuming the person is just looking for an answer, especially in something like Mathematica because their is a certain process to follow and if I am just given the answer then I will never learn or understand why it is the way it is. Again, thanks for the pointers.

Brandon

POSTED BY: Brandon Davis
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract