MATLAB Fundamentals Summary
### MATLAB Fundamentals Summary: Chapters 1 and 2
#### 1. MATLAB Environment and Operations
Command Window and Basic Operations:
The Command Window is the primary user interaction space in MATLAB,
indicated by the >> prompt. Commands are entered here to execute operations.
MATLAB allows basic arithmetic operations such as:
2 + 3 % addition
3 - 2 % subtraction
2 * 3 % multiplication
1 / 2 % division
2 ^ 3 % exponentiation
MATLAB also handles special values like:
- Inf (infinity) for calculations such as 1/0.
- NaN (Not a Number) for undefined operations like 0/0.
Differences between .* ./ .^ and their equivalents without a dot:
- .* ./ .^ are used for element-wise operations on arrays.
- Example:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A .* B; % Element-wise multiplication
Conversely, * corresponds to standard matrix multiplication.
#### 2. Variables and Data Types
Rules for Variable Naming:
- A variable must start with a letter and can contain letters, numbers, and
underscores.
- MATLAB is case-sensitive: balance, Balance, and BALANCE are three different
variables.
Workspace Management:
- clear command: removes variables.
- who and whos commands: display variables in the workspace with details.
Importance of Proper Naming:
- Avoid using names of built-in functions as variables (e.g., pi or sqrt).
Example:
pi = 4; % Redefines pi, causing errors
clear pi; % Resets pi to its default value
#### 3. Vectors and Matrices
Creating and Manipulating Vectors:
- Using the colon operator:
x = 1:5; % Creates a vector from 1 to 5
linspace and logspace functions:
linspace(0, 10, 5); % 5 equally spaced values between 0 and 10
Accessing and Modifying Elements:
Index-based access:
r = [10, 20, 30];
r(2) = 25; % Modifies the second element
Matrices:
- Creation:
A = [1, 2, 3; 4, 5, 6];
- Constructing from column vectors:
x = [1; 2; 3];
y = [4; 5; 6];
M = [x, y];
Solving Linear Systems:
- Using the matrix method:
A = [1, 2; 2, -1];
B = [4; 3];
X = A \ B; % Solution: x = 2, y = 1
Using the symbolic function:
syms x y;
[x, y] = solve(x + 2*y == 4, 2*x - y == 3);
#### 4. Programming Constructs
Flow Control (if, else, elseif):
Example usage:
score = 85;
if score >= 80
grade = 'A';
elseif score >= 65
grade = 'B';
else
grade = 'C';
end
Script for Assigning Grades:
Example:
scores = [90, 75, 60, 50];
grades = cell(size(scores));
for i = 1:length(scores)
if scores(i) >= 80
grades{i} = 'A';
elseif scores(i) >= 65
grades{i} = 'B';
elseif scores(i) >= 50
grades{i} = 'C';
else
grades{i} = 'D';
end
end
disp(grades);
#### 5. Mathematical and Graphical Functions
Built-in Mathematical Functions:
Examples:
sqrt(pi); % Square root of pi
exp(1); % Exponential of 1
sin(pi/2); % Sine of pi/2 (in radians)
Creating Graphs:
- Plotting functions:
x = 0:0.1:10;
y = sin(x);
plot(x, y, 'r', 'LineWidth', 2);
grid on;
xlabel('x'); ylabel('sin(x)');
title('Graph of sin(x)');
Adjusting axes:
axis([0 10 -1 1]);
#### 6. Practical Application
Vertical Displacement under Gravity:
MATLAB script:
g = 9.81; % Acceleration due to gravity (m/s^2)
u = input('Enter initial velocity (m/s): ');
t = 0:0.1:12.3; % Time intervals
s = u .* t - 0.5 * g .* t.^2;
plot(t, s, 'b', 'LineWidth', 2);
grid on;
xlabel('Time (s)'); ylabel('Displacement (m)');
title('Vertical Displacement of an Object');
This summary covers the fundamental concepts presented in the first two
chapters, providing examples to aid understanding.