# MATLAB Programming/Examples of MATLAB Command

## Introduction

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.

### Examples

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 =
585

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

window_area =
3.2500

>> door_area=1.2*3

door_area =
3.6000

>> paint_area=room_area-window_area-door_area

paint_area =
578.1500

>>%amount of paint can needed
paint_area / 120

ans =
4.8179
%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 =
150000000

>> speed = 7

speed =
7

>> time = distance / speed

time =
2.1429e+07

>> 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 =
0.6795
```

### 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 =
9

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

diceroll =
6

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

diceroll =
9

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

diceroll =
11

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

diceroll =
5

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

diceroll =
12
```