MATLAB Fundamentals
Introduction to MATLAB
Getting Started with MATLAB
MATLAB is a powerful tool used by engineers and scientists for everything from analyzing data to building control systems for rockets. Think of it as a super-powered calculator that also understands programming. It's designed to work with math, especially a type of math called linear algebra, but you don't need to be a math whiz to get started.
The MATLAB Environment
When you first open MATLAB, you'll see a few different windows. The three most important ones are the Command Window, the Workspace, and the Editor.
- Command Window: This is where you can type commands and see immediate results. It's great for quick calculations and testing ideas.
- Workspace: This window shows you all the variables you have created and their current values. It’s a handy way to keep track of your work.
- Editor: This is a text editor where you write longer programs, called scripts. You can save these scripts and run them again later. For now, we'll focus on the Command Window.
Variables and Arithmetic
Let's start by creating a variable. A variable is just a name that stores a value. In the Command Window, you can create a variable named x and give it a value of 10 like this:
x = 10
When you press Enter, MATLAB will display x = 10, confirming the assignment. You'll also see x appear in your Workspace window. Now you can use this variable in calculations. MATLAB uses standard symbols for arithmetic: + for addition, - for subtraction, * for multiplication, and / for division.
y = x + 5
z = x * 2
After running those commands, your Workspace will now show three variables: x, y, and z, with their corresponding values (10, 15, and 20). If you want to perform a calculation without storing the result in a variable, MATLAB will automatically store it in a temporary variable called ans.
40 / 8
MATLAB will output ans = 5.
Using Built-in Functions
MATLAB comes with a huge library of built-in functions that perform all sorts of tasks. A function takes an input (called an argument), does something with it, and gives you an output. To use a function, you type its name followed by the input in parentheses.
For example, the
sqrt()function calculates the square root of a number.
% Calculate the square root of 81
root_value = sqrt(81)
The variable root_value will now hold the value 9. The line starting with a % is a comment. MATLAB ignores comments, but they are useful for explaining your code to others (and to your future self).
There are functions for all kinds of mathematical operations. Let's try a few more. MATLAB also has built-in constants, like pi.
% Find the absolute value of -25
pos_num = abs(-25)
% Calculate the sine of pi/2 (90 degrees)
sine_val = sin(pi/2)
After running this, pos_num will be 25 and sine_val will be 1.
How do you know what functions are available or how to use them? The help command is your best friend. Just type help followed by the function name.
help cos
This command will display a detailed explanation of the cosine function (cos) right in the Command Window, telling you what it does and how to use it.
What is the primary purpose of the MATLAB Command Window?
If you type 5 + 10 in the Command Window and press Enter without assigning it to a variable, where is the result stored?
Now you know your way around the MATLAB environment and can perform basic calculations. This is the foundation for tackling more complex problems.
