In today’s lesson, we will be discussing an introduction to MATLAB. The following topics will be covered:
1. What is MATLAB?
2. Installation
3. Starting and Exiting
4. Basic Calculations
5. Error Messages
What is MATLAB?
How did it become MATLAB?
It is a computational/technical software, and is usually taught in engineering classes. It stands for MATrix LABoratory.
The first version of MATLAB was based on research papers that presented algorithms for solving linear algebra problems, published by many researchers between 1965-70, one of whom became more famously known – J.H. Wilkinson, a British mathematician who became a world leader in linear algebra. In 1971, researchers at Argonne National Laboratory (a science and engineering laboratory in Chicago) released EISPACK (Matrix Eigensystem Package) by translating procedures for eigenvalue problems into the programming language Fortran. Then in 1975, a proposal was brought to the U.S. National Science Foundation for a research project to investigate methods for the development of mathematical software. The product released from this was LINPACK (Linear Equation Package), which was in Fortran.
Later on, the first MATLAB program was written in Fortran. This was because professors needed their students to have easier access to EISPACK and LINPACK. It was just an interactive matrix calculator to begin with. Then a more extended version of MATLAB was written using C, and made its way to the 1984 IEEE Conference on Decision and Control in Las Vegas.
What programming language is it written in?
It is a programming language itself. On top of that, different parts of the software are written in different languages, including C (eg. core framework), C++ (eg. Parallel computing), Java (eg. interfaces) and FORTRAN (eg. indexing starting at 1).
What are the benefits of this over other programming langauges?
MATLAB is modern in the sense that it contains data structures, built-in editing and debugging, can support object oriented programming, and more. It can do much more for technical problems than other softwares.
We will get into these in another chapter, but there are separate parts of the software called “toolboxes,” which can be bought as add-ons. These are useful in many technical aspects, and include signal processing, symbolic computation, control theory, simulation, optimization, and much more.
Note one definition that will be useful to know is the word syntax. Syntax refers to the words or letters you type out in code. This basically is the code. For example, if someone says you have “incorrect syntax,” this means your code is incorrect.
Installation
To install MATLAB, enter your email at this link (you can get a free trial but eventually you’ll have to pay). You will see a screen like this at that link:
Entering your email, it will bring you a page to set up an account. Watch the video above to see the setup process in action!
Starting and Exiting
To open MATLAB, click on the icon in whichever folder you saved it in. The picture below is what MATLAB looks like when I open it. To close it, close the window using the red button at the top left.
Note the “>>” circled in red above. That’s called the “prompt,” and it’s one of the areas where we can type code (there are other things like scripts, so we don’t always type all the code directly into this window above). However, the output of our code always in this window, regardless of where the code is actually typed out.
Here is some terminology of the program, circled in red in each screenshot:
(a) Command Window – MATLAB shows the output of all the code you type in the Command Window. The entire window we see is the Command Window.
(b) Command History (to access this, press the “up” key on your keyboard) – This is a history of all code typed in the past, even if you closed MATLAB in between sessions.
(c) Workspace – This shows where all variables are saved once they’re created.
(d) The Current Folder (also called Current Directory, Present Working Directory, or Current Working Folder) – this shows the files saved in the MATLAB folder. If you have code files saved, they will show here.
(e) Help Browser – This is through Mathworks, which is the website where MATLAB was downloaded from. It is documentation of different commands, and is useful for anything MATLAB related.
This is what pops up when you press the button.
Basic Calculations
In the command window, type in any numerical calculations and MATLAB will act like a calculator. Recall that mathematical formulas follow BEDMAS, so Brackets, Exponents, Division/Multiplication, then Addition/Subtraction is the order from left to right that a math formula is done as.
Notice how before MATLAB shows the answer, it always says “ans=”. This is short for “answer.” If we don’t assign a variable to an equation, like in our case, then MATLAB automatically assigns outputs as the variable “ans.”
But if we did assign a variable to the equation, it would be if we wrote something like x = 2 + 3, and MATLAB will give x = 5. “x” is the variable and it equals 5 in this case. We will discuss more on variables in the next chapter.
The keys for calculations are as follows:
Addition: +
Subtraction: –
Multiplication: *
Division: /
Error Messages
If you write something incorrectly, MATLAB will use an error message in red, to indicate the problem. It will appear in the Command Window where the output would usually be. It also usually tells you the line number that the error is on (if you have a script which does have many lines, which we talk about in future lessons).
You can check out Mathworks.com or google these errors to figure out what they mean and what you need to fix if it isn’t clear from reading the error.
See this example below where I used a dollar sign. This is invalid if I am trying to add 5+4 and throw in some $$$. This error clearly explains that this is “not valid in MATLAB statements or expressions.” When we have many lines of code (in scripts), error messages are useful to understand where we have an issue, because it’s not always this straightforward.
References: Mathworks.com