MATLAB Commands for Mathematics Lab (1st Year
Engineering Students)
This document contains commonly used MATLAB commands for Mathematics Lab in the 1st year
of Engineering. Each command is explained with examples for better understanding.
Basic Setup
These commands are used to clear the workspace and command window for fresh execution.
clc; % Clear command window
clear; % Clear all variables
close all; % Close all figures
Arithmetic Operations
MATLAB can perform simple arithmetic operations such as addition, subtraction, multiplication,
division, and power.
a = 10;
b = 5;
sum_val = a + b % Addition
diff_val = a - b % Subtraction
prod_val = a * b % Multiplication
div_val = a / b % Division
power_val = a ^ 2 % Power
Vectors and Matrices
MATLAB is especially powerful in handling vectors and matrices. These commands are used for
defining and performing operations on matrices.
A = [1 2 3; 4 5 6; 7 8 9] % Define a matrix
B = [2 0 1; 1 3 2; 4 1 5]
C = A + B % Matrix addition
D = A * B % Matrix multiplication
E = A' % Transpose
detA = det(A) % Determinant
invB = inv(B) % Inverse
Trigonometric Functions
MATLAB provides built-in functions for trigonometric calculations. All values are in radians.
theta = pi/4; % 45 degrees in radians
sin_val = sin(theta) % Sine
cos_val = cos(theta) % Cosine
tan_val = tan(theta) % Tangent
Polynomials
Polynomials can be represented as vectors in MATLAB. The coefficients are given in descending
order of power.
p = [1 3 2]; % Represents x^2 + 3x + 2
roots(p) % Find roots of polynomial
polyval(p, 2) % Evaluate polynomial at x=2
Differentiation and Integration
Using symbolic math, MATLAB can differentiate and integrate expressions.
syms x
f = x^3 + 2*x^2 + 5*x + 7;
diff_f = diff(f) % Differentiate f wrt x
int_f = int(f) % Integrate f wrt x
Solving Equations
Equations can be solved symbolically using the solve function.
syms x
eqn = x^2 - 5*x + 6 == 0;
sol = solve(eqn, x) % Solve quadratic equation
2D Plotting
MATLAB is widely used for plotting mathematical functions.
x = -10:0.1:10; % Range from -10 to 10
y = x.^2; % y = x^2
plot(x, y, 'r', 'LineWidth', 2) % Plot parabola
xlabel('x-axis')
ylabel('y-axis')
title('y = x^2')
grid on
Multiple Functions Plot
Multiple functions can be plotted in the same graph using the plot command.
x = -10:0.1:10;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, 'b', x, y2, 'r--', 'LineWidth', 2)
legend('sin(x)', 'cos(x)')
xlabel('x')
ylabel('Function values')
title('Sine and Cosine functions')
grid on
3D Plotting
MATLAB can also plot functions in 3D, useful for visualizing surfaces.
[X,Y] = meshgrid(-5:0.5:5, -5:0.5:5);
Z = X.^2 + Y.^2;
surf(X, Y, Z) % 3D surface plot
xlabel('X-axis')
ylabel('Y-axis')
zlabel('Z-axis')
title('3D Surface: Z = X^2 + Y^2')