DEV Community

Dumebi Okolo
Dumebi Okolo

Posted on

4 2 2 2 2

11 Common MATLAB Keywords and Syntax for Engineers

I am writing a guide that highlights the most commonly used MATLAB keywords and their associated syntax, with practical examples, for my classmates to help with tasks like working with polynomials, Laplace transforms, plotting, solving equations, basic circuit analysis like mesh analysis, as well as differentiation, integration, and partial fractions.


1. Basic MATLAB Syntax and Keywords

These are foundational commands that manage the environment and display results.

Example: You’re starting a new script and want to clean the workspace.

clc        % Clears the command window
clear      % Clears all variables
close all  % Closes all figure windows
Enter fullscreen mode Exit fullscreen mode

Use these at the start of every script to avoid clutter and ensure a clean slate.


2. Variables and Operators In MATLAB

Example: You want to compute the value of a quadratic expression ( y = x^2 + 2x + 1 ) for ( x = 3 ).

x = 3;
y = x^2 + 2*x + 1;  % y = 16
Enter fullscreen mode Exit fullscreen mode

You can also create a vector and apply the same formula element-wise:

x = 0:1:5;
y = x.^2 + 2.*x + 1;  % Use element-wise operations for vectors
Enter fullscreen mode Exit fullscreen mode

3. Control Structures In MATLAB

Example: You want to classify a number based on its sign:

x = -5;
if x > 0
    disp('Positive')
elif x < 0
    disp('Negative')
else
    disp('Zero')
end
Enter fullscreen mode Exit fullscreen mode

Exam Tip: Expect questions that ask you to loop through values or make decisions based on conditions.


4. Working with Polynomials In MATLAB

Polynomials are stored as vectors of coefficients starting from the highest degree.

Example 1: Represent ( x^3 - 6x^2 + 11x - 6 ):

p = [1 -6 11 -6];
Enter fullscreen mode Exit fullscreen mode

Find roots:

r = roots(p);  % Returns [3; 2; 1]
Enter fullscreen mode Exit fullscreen mode

Evaluate polynomial at x = 2:

y = polyval(p, 2);  % Returns 0
Enter fullscreen mode Exit fullscreen mode

Multiply polynomials ( (x+2)(x+3) ):

conv([1 2], [1 3])  % Returns [1 5 6]
Enter fullscreen mode Exit fullscreen mode

Divide polynomials ( (x^2 + 5x + 6) / (x + 2) ):

[q, r] = deconv([1 5 6], [1 2]);  % Quotient and remainder
Enter fullscreen mode Exit fullscreen mode

5. Laplace and Inverse Laplace Transforms In MATLAB

Used to convert time-domain functions into the s-domain.

Example: Find Laplace of ( e^{-2t} ):

syms t s
f = exp(-2*t);
F = laplace(f)  % Returns 1/(s + 2)
Enter fullscreen mode Exit fullscreen mode

Inverse Laplace:

ilaplace(1/(s + 2))  % Returns exp(-2*t)
Enter fullscreen mode Exit fullscreen mode

Exam Tip: You may be asked to transform simple time-domain signals to the s-domain.


6. Solving Equations In MATLAB

Example 1: Solve quadratic equation ( x^2 - 5x + 6 = 0 ):

syms x
solve(x^2 - 5*x + 6 == 0, x)  % Returns 2 and 3
Enter fullscreen mode Exit fullscreen mode

Example 2: Solve differential equation ( dy/dt = -2y ), with ( y(0) = 1 ):

syms y(t)
dsolve(diff(y,t) == -2*y, y(0) == 1)  % Returns exp(-2*t)
Enter fullscreen mode Exit fullscreen mode

7. Differentiation and Integration In MATLAB

Differentiate: ( f(x) = x^3 + 2x^2 + x )

syms x
f = x^3 + 2*x^2 + x;
df = diff(f)  % Returns 3*x^2 + 4*x + 1
Enter fullscreen mode Exit fullscreen mode

Integrate: ( f(x) = x^2 )

int(x^2)  % Returns x^3/3
int(x^2, x, 0, 2)  % Returns 8/3 (definite integral)
Enter fullscreen mode Exit fullscreen mode

Exam Tip: Know how to perform both definite and indefinite integrals symbolically.


8. Partial Fraction Decomposition In MATLAB

Partial fractions are used to simplify rational expressions—especially for Laplace transforms.

Example 1:

syms s
expr = (s + 5)/(s^2 + 3*s + 2);
partfrac(expr)
Enter fullscreen mode Exit fullscreen mode

Explanation: MATLAB factors the denominator as (s + 1)(s + 2) and returns:

3/(s + 1) + 2/(s + 2)
Enter fullscreen mode Exit fullscreen mode

Example 2: More complex case:

expr2 = (s^2 + 5*s + 6) / ((s + 1)*(s + 2)*(s + 3));
partfrac(expr2)
Enter fullscreen mode Exit fullscreen mode

9. Plotting and Visualization In MATLAB

Example: Plot sine and cosine functions:

x = 0:0.1:2*pi;
plot(x, sin(x))
hold on
plot(x, cos(x))
title('Sine and Cosine')
xlabel('x')
ylabel('Function value')
legend('sin(x)', 'cos(x)')
Enter fullscreen mode Exit fullscreen mode

Exam Tip: You may be asked to visualize signals or plot mathematical functions.


10. Matrix Operations In MATLAB

Example: Solve system of equations using matrices:
[
\begin{cases}
x + 2y = 5 \
3x + 4y = 6
\end{cases}
]

A = [1 2; 3 4];
B = [5; 6];
X = A\B;  % Solves AX = B
Enter fullscreen mode Exit fullscreen mode

Other operations:

det(A)     % Determinant
inv(A)     % Inverse
rank(A)    % Rank of matrix
eig(A)     % Eigenvalues and eigenvectors
Enter fullscreen mode Exit fullscreen mode

11. Solving Circuit Problems In MATLAB: Mesh Analysis Example

Example: Solve mesh currents in a 2-mesh circuit:

{Mesh 1: } 10 = 2I1 + 4(I1 - I2)
{Mesh 2: } 0 = 3I2 + 4(I2 - I1)

A = [6 -4; -4 7];
B = [10; 0];
I = A\B;
I1 = I(1);
I2 = I(2);
disp(['I1 = ', num2str(I1), ' A'])
disp(['I2 = ', num2str(I2), ' A'])
Enter fullscreen mode Exit fullscreen mode

Explanation: Use matrix equations for circuit analysis. This example solves for currents using mesh analysis.

Top comments (0)