Learning MATLAB? Number 1 Best Place to Start

MATLAB is a very popular tool used across industries. MATLAB is an environment for all kinds of technical computing like numerical calculations, data analysis, simulation, visualization, and algorithm development. MATLAB is exclusively used in automotive, medical, aerospace domains, and scientific research for rapid prototyping, software development, and simulations. If you are new to MATLAB and wondering how to start learning MATLAB, then this is the perfect place to start reading. Optionally you can directly jump to the video tutorial at the end of this article.

Learning MATLAB Environment

When we start MATLAB; the default layout contains three windows. The current folder, command window, and workspace. In the command window, we can enter MATLAB commands. Workspace shows variables created by MATLAB command.

MATLAB Environment

If you want to try something out and see the results quickly we can enter commands in the command window.

MATLAB Command

By default, MATLAB assigns all the calculated results in variable name ‘ans’ and you can see that variable in the workspace window in above picture. We can assign results to a specific variable. in the example below, We can find variable m is created in the workspace.

MATLAB Command

If we add a semicolon at the end of the command, the result will not be displayed. The calculation is still executed and you can see variable is created in the workspace.

We can just enter the variable name in the command window to see its value.

MATLAB Command

How to name variables?

We can name them anything we like; as long as the variable name starts with a letter and contains letters, numbers, and underscores. Always name variable something meaningful. like

>> final_height = 0;

>> initial_velocity = 0;

In MATLAB, variable names are case-sensitive. let’s take an example, H is equal to 10 and h is equal to 7. We can see in the workspace, both variables do exist and have different values.

CASE Sensitive Variable Name

Using Live Script to Solve Problems

Let’s see how to solve a problem programmatically.
If we drop an object from a height of 10 meters, how much time it will take to hit the ground?

When we drop any object, the initial velocity is always 0 m/s2.
When the object hits the ground, the final height is also 0 m.
The initial height, in this case, is 10 m.
Acceleration due to gravity g is 9.8 m/s2.

MATLAB Variables

final_height = initial_height + initial_velocity x time + (1/2) x (-g) x time2

Above equation looks similar to below equation
y = at2 + bt + c

a becomes equal to (1/2) x (-g)
b becomes equal to initial_velocity and
c becomes equal to the initial_height

MATLAB gives a handy solution to find the roots.

We found 2 roots. In this case, time cannot be negative, so let’s take the maximum value of these two roots.

Finding roots in MATLAB

So, the object dropped from height of 10m will hit the ground in 1.4286 seconds.

we can organize commands to create a script. To create a new live script hit the live script button on the toolbar. It will open a live editor window. we can add our code here in the live editor. we can also add text and comments for better readability.

We can run this program by hitting the run button. We can see the results on the right pane.

MATLAB Live Editor

we can break down these lines into sections. and run the script section by section. It becomes easier to debug the script. we can share the script with others in PDF format or HTML format.

Save and Load Variables

When we close the MATLAB the workspace variables are automatically deleted. So to save the workspace variables we can use the save command. we can specify in which file we want to save workspace variables.

>> save filename

If we don’t specify a filename, MATLAB creates a file with the default name as matlab.mat.

When we start MATLAB again and we want to load the variables we can use the load command. All the variables are restored in the workspace.

>> load filename

Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *