Introduction to MATLAB
## Learning Objectives
- Understand MATLAB history and applications
- Set up MATLAB development environment
- Write and run your first MATLAB program
- Understand MATLAB syntax basics
## What is MATLAB?
MATLAB (Matrix Laboratory) is a high-level programming language and interactive environment developed by MathWorks for numerical computing, algorithm development, and data visualization.
```matlab
% Your first MATLAB program
disp('Hello, World!');
```
## MATLAB Platform
### Key Features
- **Matrix-Based** - Built-in support for matrix and array operations
- **Interactive** - Execute commands line-by-line or run scripts
- **Visualization** - Extensive plotting and graphics capabilities
- **Toolboxes** - Specialized function libraries for different domains
- **Numerical Computing** - Designed for mathematical and engineering computations
### Components
| Component | Description |
|-----------|-------------|
| MATLAB | Interactive development environment |
| Editor | Write and debug code |
| Command Window | Execute commands directly |
| Workspace | Store variables in memory |
| Path | Search order for functions |
## Installing MATLAB
### Download
Get MATLAB from (requires license)
### Online Alternative
MATLAB Online: (browser-based)
### Verify Installation
```matlab
ver
```
## Your First Program
### Using Command Window
```matlab
>> disp('Hello, World!')
Hello, World!
```
### Using Script
Create a new script file named `hello.m`:
```matlab
% hello.m
% My first MATLAB program
name = 'World';
fprintf('Hello, %s!\n', name);
```
### Run the Script
```matlab
hello
```
## MATLAB Interface
### Main Window Areas
- **Current Folder** - Browse files and folders
- **Command Window** - Enter commands directly
- **Workspace** - View variables in memory
- **Editor** - Write and edit scripts
- **Command History** - View previous commands
## Comments
```matlab
% Single-line comment
%{
Multi-line comment
Can span multiple lines
%}
x = 5; % Inline comment
```
## Variables
### Creating Variables
```matlab
a = 10; % Numeric variable
name = 'MATLAB'; % String variable
isActive = true; % Logical variable
```
### Displaying Variables
```matlab
x = 42;
disp(x); % Display value
fprintf('x = %d\n', x); % Formatted output
```
### Variable Naming Rules
- Start with a letter
- Can contain letters, numbers, and underscores
- Case-sensitive
- Cannot use reserved words
```matlab
validName = 1; % Valid
my_variable = 2; % Valid
2ndPlace = 3; % Invalid (starts with number)
end = 5; % Invalid (reserved word)
```
## Built-in Constants
```matlab
pi % 3.1416...
exp(1) % e (Euler's number)
Inf % Infinity
NaN % Not-a-Number
eps % Machine epsilon
realmin % Smallest positive number
realmax % Largest positive number
```
## Basic Math Operations
```matlab
a = 10;
b = 3;
sum = a + b % Addition: 13
diff = a - b % Subtraction: 7
prod = a * b % Multiplication: 30
quot = a / b % Division: 3.3333
power = a ^ b % Power: 1000
modulus = mod(a,b) % Modulo: 1
```
## Command Window Tips
```matlab
clc % Clear command window
clear % Clear all variables
clear x % Clear specific variable
who % List variables in workspace
whos % List variables with details
```
## MATLAB Versions
```matlab
version % Get MATLAB version
ver % List all installed products
```
| Version | Year | Key Features |
|---------|------|--------------|
| R2023a | 2023 | Live Editor improvements |
| R2022b | 2022 | New graphing functions |
| R2022a | 2022 | Array formation |
| R2021b | 2021 | Enhanced debugging |
## Summary
- MATLAB is designed for numerical computing and matrix operations
- Interactive environment with command window and workspace
- Scripts are saved as `.m` files
- Comments start with `%`
- Use `disp()` or `fprintf()` for output
- Variables are created automatically when assigned
- Use `clear` to remove variables and `clc` to clear the screen
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →