# Machelp with matlab beginner

#### twoodcc

##### macrumors P6
Original poster
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

#### notjustjay

##### macrumors 603
Well, Matlab is a very powerful tool, but at heart it's basically a calculator

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.

#### twoodcc

##### macrumors P6
Original poster
notjustjay said:
Well, Matlab is a very powerful tool, but at heart it's basically a calculator

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.

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

and were the commands correct so far?

#### Mav451

##### macrumors 68000
twoodcc said:
the spacecraft also consumes 0.078 liters/hour
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

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)

#### mwpeters8182

##### macrumors 6502
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.

#### balamw

##### Moderator
Staff member
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.

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

B

#### mkrishnan

##### Moderator emeritus
LOL You're just lucky the spaceship doesn't travel at relativistic speeds!

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!

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

#### mduser63

##### macrumors 68040
balamw said:
(Using MATLAB for this kind of problem seems like overkill. MATLAB truly shines when matrices are involved.)

B
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.

#### balamw

##### Moderator
Staff member
mduser63 said:
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.
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

#### mkrishnan

##### Moderator emeritus
balamw said:
I know, but the problem seems so artificial and heavily constrained that it doesn't seem very useful. JMHO.
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.

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 )

#### notjustjay

##### macrumors 603
twoodcc said:

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

and were the commands correct so far?
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.

#### twoodcc

##### macrumors P6
Original poster
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

#### balamw

##### Moderator
Staff member
twoodcc said:
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.
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.

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

#### mkrishnan

##### Moderator emeritus
balamw said:
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.
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!

#### twoodcc

##### macrumors P6
Original poster
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

#### deepy

##### macrumors regular
twoodcc said:
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
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!

#### twoodcc

##### macrumors P6
Original poster
deepy said:
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!
well i'm glad i wasn't in that class

#### twoodcc

##### macrumors P6
Original poster
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

#### balamw

##### Moderator
Staff member
twoodcc said:
ok, so tell me if my figures are correct:
They seem fine. As was pointed out I left out the 3600 in my half speed estimate and it's consistent w/yours.

B

#### savar

##### macrumors 68000
twoodcc said:
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));
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.

#### twoodcc

##### macrumors P6
Original poster
balamw said:
They seem fine. As was pointed out I left out the 3600 in my half speed estimate and it's consistent w/yours.

B
thanks, that's good to know.

now i just gotta put this into matlab

#### twoodcc

##### macrumors P6
Original poster
savar said:
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.
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