MATLAB Study Guide
1. Core Syntax & Basics
clc; clear; close all; % reset environment
a = 5; % variable assignment
% comment line
2. Math Operations
Scalars: +, -, *, /, ^
Element-wise: .* , ./ , .^
Functions: sqrt(x), exp(x), abs(x), log(x), log10(x), factorial(x)
Trig: sin(x), cos(x), tan(x) [radians]
sind(x), cosd(x), tand(x) [degrees]
Inverses: asin(x), acos(x), atan(x), asind(x), acosd(x), atand(x)
3. Vectors & Matrices
A = [1 2 3; 4 5 6]; % 2x3 matrix
v = [1 2 3 4]; % row vector
w = [1; 2; 3; 4]; % column vector
Indexing: A(2,1), A(:,2), A(1,:)
size(A) % matrix dimensions
Matrix math: A*B (matrix mult), A.*B (element-wise mult)
4. Input & Output
x = input('Enter a number: ');
name = input('Enter your name: ', 's');
disp('Hello World')
fprintf('The result is %g\n', x)
5. Plotting
x = 0:0.1:2*pi; y = sin(x);
plot(x,y,'r--o') % red dashed line with circles
title('My Plot'), xlabel('x values'), ylabel('y values')
grid on, legend('sin(x)')
subplot(2,1,1); plot(x,y); subplot(2,1,2); plot(x,cos(x));
6. Common Gotchas
Use .*, ./, .^ with vectors
Indexing starts at 1 (not 0)
input needs 's' for strings
plot not Plot (case-sensitive)
Practice Problems
Q1
Compute the natural logarithm of 50 in MATLAB.
Q2
Create a 2x3 matrix A and a 3x2 matrix B. Multiply them using matrix multiplication.
Q3
Write a script that asks the user for their age and prints: 'You are XX years old.'
Q4
Plot y = cos(x) for x = -pi:0.1:pi with a green dotted line, and label the axes.
Q5
What is the difference between A*B and A.*B? Give an example.
Solutions
Q1
log(50)
Q2
A = [1 2 3; 4 5 6]; B = [1 2; 3 4; 5 6]; C = A*B
Q3
age = input('Enter your age: ');
fprintf('You are %g years old.\n', age)
Q4
x = -pi:0.1:pi; y = cos(x);
plot(x,y,'g:')
xlabel('x'); ylabel('cos(x)')
Q5
A*B = matrix multiplication (rows x columns rule).
A.*B = element-wise multiplication (same size matrices).