help with matlab beginner

Discussion in 'Mac Programming' started by twoodcc, Sep 5, 2006.

  1. macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    i'm currently taking a matlab course. i've never used matlab before, so it's very new to me.

    we had an in-class assignment today, that we didn't have time to finish, but i am stuck.

    we have information that Pluto is 4.34 billion km from earth. we have a spacecraft that travels at 5.2 km.sec

    the spacecraft also consumes 0.078 liters/hour

    will it have enough fuel to reach pluto?

    that is the assignment basically. here is what i have done so far:

    distance = input('How far away is Pluto in km? ');
    fuel = input('How much fuel in liters does the spacecraft have? ');
    speed = input('How fast does it go? ');

    disp(sprintf('Pluto is %5.2f km away from the Earth.', distance));
    disp(sprintf('The spacrecraft has %5.2f liters of fuel,' fuel));


    am i doing this right? any help will be greatly appreciated

    thanks in advance
     
  2. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #2
    Well, Matlab is a very powerful tool, but at heart it's basically a calculator :D

    What you should do first is get a handle on how to solve the actual math problem first. You can maybe write it down on paper, use variable names you're familiar with. With this problem, for example, you're going to need to convert some units around (km/sec into km/hour) and do some intermediate steps (how many hours will it take to fly to Pluto?).

    Once you figure out the steps, writing them into a Matlab .m file is almost as simple as writing them down on paper, step by step.
     
  3. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    thank you for your reply.

    so do i have to tell matlab that it's in km and in km/hour?

    and were the commands correct so far?
     
  4. macrumors 68000

    Mav451

    Joined:
    Jul 1, 2003
    Location:
    Maryland
    #4
    Hehe, good thing they give that to you. If they didn't...you probably need to do some dW/dt action in regards to the effects of fuel efficiency as your spacecraft got lighter :D

    In terms of the units--I think that is inherent in the constants you put in.
    E.g. Calculate everything in km/sec. And then at the end, put a few lines that will convert it back to km/hour...basically your "km/sec" value * 3600 (hr/min * min/sec)
     
  5. macrumors 6502

    Joined:
    Apr 16, 2003
    Location:
    Boston, MA
    #5
    Since you're using matlab, the best way (if they've taught this yet) is to use a function. That way you don't have to worry about the prompt every time you run the program.

    You could run it as pluto(distance, amount of fuel), and it could return yes/no.

    Otherwise, I agree with the rest of the folks in here. Run the calculations out by hand, and then code them.
     
  6. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    All I can say is d*mn that's a lot of fuel!

    If the fuel is as dense as water it would weigh 18 metric tons and fit in a 3x3x3 meter cube. :eek:

    (Using MATLAB for this kind of problem seems like overkill. MATLAB truly shines when matrices are involved.)

    B
     
  7. Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #7
    LOL You're just lucky the spaceship doesn't travel at relativistic speeds! :D

    But psssh...why is it burning fuel all the way to Pluto? Shouldn't it be doing an initial burn and holding onto its fuel for positioning when it gets to the planet? There's not exactly a lot of wind resistence out there! :eek: ;) :D

    The 3x3x3 doesn't seem quite right. It seems too big by a factor of two, unless *I'm* miscalculating.
     
  8. macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #8
    Agreed, but the OP said he's in a class to learn MATLAB. I'm sure this is a start-small assignment to get used to syntax, etc.
     
  9. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #9
    I know, but the problem seems so artificial and heavily constrained that it doesn't seem very useful. JMHO.

    mkrishnan, I got 18.1E3 liters, which at 1E-3m^3/l works out to 18.1 m^3, the cube root of 18.1 is 2.62, so I rounded up a bit to 3. Did I screw it up somewhere! (You're right, that the volume increase from my rouning almost doubles it, but I suspect you'd need some wall thickness to contain the fuel. ;) )

    B
     
  10. Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #10
    I came up with 18 cubic meters when I knew there were 18k liters, since the liter was already a volumetric unit. But I was wrong about the 2x ... I just knew that 3x3x2=18 and 3x3x3=27. So I think I meant 1.5x. :eek: :eek: :D

    Hehehehehehe

    But yeah, this is a weird problem to use Matlab for because it's so clearly suited to a simple calculator.

    (EDIT: 18k liters natch :eek: )
     
  11. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #11
    I didn't actually check your commands closely but at first glance they look like they should work. There might be some minor tweaking but I think you'll get the idea.

    As for telling matlab that it's in km and km/hour -- not exactly. You -- the programmer -- need to keep track of your units.

    (I don't mean to sound condescending or anything, so I apologize if I do, I really have no idea what level of math this is (high school? college?), so I have to appeal to the lowest common denominator :) I also don't want to give away the answer directly, so excuse me for being a bit obtuse at times too.)

    Forget all about Matlab for the moment, and suppose you are trying to solve this problem with a paper and pencil. You need to work through the exact steps first. THEN you can worry about whether or not the code is correctly performing these steps. In fact, this is how you would "unit test" your code once you get it done - you calculate your answer by doing it by hand, and then run your code. If your code produces the same answer, then you know you coded it correctly.

    So, given:
    fuel_consumption = 0.078 liters/hour
    distance = 4.34 billion km
    speed = 5.2 km/sec
    fuel_provided = (user provided) liters

    In order to answer "will it have enough fuel", you first need to ask "how much fuel will it need?" Ultimately you want to compare: is fuel_provided >= fuel_needed?

    So how do you calculate fuel_needed? You need to know the fuel consumption rate (which is given) and how long the spacecraft will be burning fuel (time).

    OK, so now you need to calculate how much time the spacecraft will be travelling for. How do you do that? You'll need to know the distance (given) and speed (given).

    But your distance is in km's per hour and your speed is in km's per second. How do you convert km's per second into km's per hour?

    ... and so it goes. Break it down into little steps. Your steps on paper should look like this:

    1. Convert speed from km/sec to km/hour
    2. Time = ???
    3. Fuel_needed = ???
    4. Is fuel_provided > fuel_needed?

    Then you can write Matlab code to get the data from the user, do the calculation, and print the answer.
     
  12. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #12
    thanks for the replies and help. this is a college course, and i'm actually taking visual basic as well (about to go to class soon)

    anyways, i forgot a few details in the problem, if you would like to know.

    you have 50 liters of fuel in the tank to start off with, and the second part of the problem is:

    the spacecraft cosumes 1/4 of fuel when going 1/2 the speed.

    sorry i can't say more, but i gotta eat then get to class.

    thanks
     
  13. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #13
    Ah, a bit more interesting, but it seems like you have still left out some details, since at 1/2 speed, you'd still only be able to go to 6666 km on 50 l of fuel. Not quite all the way to Pluto. :confused: :p

    Perhaps if the fuel consumption continues to scale like the square of the speed you might be able to get there at super slow speed.

    B
     
  14. Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #14
    I think it was liters per hour but km per second, so there was a factor of 3600 involved...

    Okay, okay, I just miss being an engineer once in a while. sosumi. :)

    But I think there are pieces of data missing. I don't see how this is going to happen on 50l of fuel! :D
     
  15. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #15
    the teacher told me that he basically just made this up. it might not make it to Pluto.

    i'm pretty sure i gave all the information
     
  16. macrumors regular

    Joined:
    Jun 28, 2006
    #16
    lol. wish we got given problems like this to teach us Matlab. We started with numerical linear algebra and tryin to make matrix calculations more efficient!
     
  17. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #17
    well i'm glad i wasn't in that class ;)
     
  18. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #18
    ok, so tell me if my figures are correct:

    5.2 * 3600 = 18720 km/hr

    50/.078 = 641.025641 hrs

    18720 * 641.025641 = 12,000,000 km - didn't make it to Pluto

    18720 / 2 = 9360

    .078 / 4 = .0195

    50 / .0195 = 2564.102564 hours

    9360 * 2564.102564 = 24,000,000 - didn't make it to pluto
     
  19. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #19
    They seem fine. As was pointed out I left out the 3600 in my half speed estimate and it's consistent w/yours.

    B
     
  20. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #20
    Hmm.. I never did any user input with matlab, but at least for the output I think you're going overkill. Matlab will display the result of any operation if that operation does not have a semicolon after it. I'm not using matlab right now, but this is kind of what it looks like:

    > a = [1 2; 3 4]
    a = [1 2
    3 4]
    > b = [1 2; 3 4];
    >

    Without semicolon, Matlab shows you the result of the operation (in this case, the operation is merely assignment). With semicolon, matlab does not show the result.

    Matlab is amazingly powerful but so different from any other environment that I've ever used. I suggest you attend as many extra hours sessions as possible (office hours, labs, whatever it is they offer) if you're interested in Matlab. I learned far more watching somebody else use it than I ever could have with the on-line help.
     
  21. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #21
    thanks, that's good to know.

    now i just gotta put this into matlab
     
  22. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #22
    thanks. i just thought i'd post this here for the first assignment so i could get a little extra help outside of class.

    hopefully i'll get the hang of it soon
     

Share This Page