MATLAB Programming/Examples of MATLAB Command



MATLAB is interesting in that it is dynamically compiled. In other words, when you're using it, you won't run all your code through a compiler, generate an executable, and then run the executable file to obtain a result. Instead, MATLAB simply goes line by line and performs the calculations without the need for an executable.

Partly because of this, it is possible to do calculations one line at a time at the command line using the same syntax as would be used in a file. It's even possible to write loops and branches at the command line if you want to. Of course this would often lead to a lot of wasted efforts, so doing anything beyond very simple calculations, testing to see if a certain function, syntax, etc. works, or calling a function you put into an .m file should be done within an .m file.



MATLAB can perform the functions of a simple calculator from the command line. We go thru some of the common Maths problem in the real life.

Here are few of mathematics problem that are demonstrated to be solved in MATLAB:

Painting Coverages


A house painter usually uses 10L of white paint to cover 120 square meter on average for single layer of coating.

Calculate how many 10L paint cans that house painter needed to buy to paint a room with size of 13m X 9m with height 5m from floor to ceiling.

Also the room have 2 windows with size of 1.5m*0.75m and 2m*1.25m and a door with size of 1.2m*3m

>> room_area=13*9*5 %calculating the overall area of wall for the room

room_area =
>> window_area=(1.*0.75)+(2*1.25)

window_area =

>> door_area=1.2*3

door_area =
>> paint_area=room_area-window_area-door_area

paint_area =

>>%amount of paint can needed 
paint_area / 120

ans =
%house painter needed equaivalent of 5 tin cans to paint the room

Earth to Sun Distance


Distances from sun to earth is equivalent to 150 million km (150,000,000 km)

If human launched a advance rocket capable of achieving constant speed of 7km/s (ignore all of the air frictions, gravitational pull) , how many year(s) does the rocket to reach the sun from earth ?

>> distance = 150000000

distance =

>> speed = 7

speed =
>> time = distance / speed

time =

>> time_to_reach_sun=time/(3600*24*365) % 3600= 1 hour =3600secs, 1 day = 24hour, 1 year=365days(discounting leap years)

time_to_reach_sun =

Dice Roll for board games


You invite some friends come over to play a board game but somehow the dice that should be comes with board game is missing. Create a MATLAB programs to throw two dices (with 6 faces)

You need to use randi command to generate random numbers. More command can be learnt here: MATLAB Programming/Fundamentals of MATLAB/MATLAB operator

>> diceroll = randi(6) + randi(6) % press up button to recall last command and press Enter

diceroll =

>> diceroll = randi(6) + randi(6)

diceroll =

>> diceroll = randi(6) + randi(6)

diceroll =

>> diceroll = randi(6) + randi(6)

diceroll =

>> diceroll = randi(6) + randi(6)

diceroll =

>> diceroll = randi(6) + randi(6)

diceroll =

External Resources