60 MA 008 – TRANSFORMS AND LINEAR
ALGEBRA
HANDS ON PRACTICE IN
MATLAB
1) Generate the Fourier series of f(x) in (−π,π)
Sample Function
Let’s use the function f(x)=xf(x) = xf(x)=x as our example function, which is
defined in the interval (−π,π)(-π, π)(−π,π).
MATLAB CODE
f = @(x) x;
L = pi; % Half-length of the interval
N = 10;
a0 = (1/(2*L)) * integral(f, -L, L);
a_n = zeros(1, N);
b_n = zeros(1, N);
for n = 1:N
a_n(n) = (1/L) * integral(@(x) f(x) .* cos(n * x), -L, L);
b_n(n) = (1/L) * integral(@(x) f(x) .* sin(n * x), -L, L);
end
syms x
fourier_series = a0;
for n = 1:N
fourier_series = fourier_series + a_n(n) * cos(n * x) + b_n(n) * sin(n * x);
end
disp('Fourier Series:');
disp(fourier_series);
x_vals = linspace(-pi, pi, 400);
f_vals = f(x_vals);
fs_vals = double(subs(fourier_series, x, x_vals));
figure;
plot(x_vals, f_vals, 'b', 'LineWidth', 2); hold on;
plot(x_vals, fs_vals, 'r--', 'LineWidth', 2);
xlabel('x');
ylabel('f(x)');
title('Fourier Series Approximation of f(x) = x');
legend('Original Function', 'Fourier Series');
grid on;
axis([-pi pi -pi pi]);
hold off;
OUTPUT:
Fourier Series:
x/2 + (1/pi)*sum((-1)^n/n*sin(n*x), n=1..10)
2) Generate the Fourier series of f(x) in (-l,l)
To generate the Fourier series of a function f(x)f(x)f(x) in the interval (−l,l)(-l, l)(−l,l), you
can modify the MATLAB code accordingly. Below is an example using the function
f(x)=x2f(x) = x^2f(x)=x2 for the interval (−l,l)(-l, l)(−l,l). We will let l=2 l = 2l=2 for
this example.
MATLAB CODE:
% Fourier Series for f(x) = x^2 in the interval (-l, l)
% Define the function and the interval
f = @(x) x.^2; % Change this to your desired function
l = 2; % Length of the interval (l)
L = l / 2; % Half-length of the interval
% Number of terms in the Fourier series
N = 10; % You can change this value to improve the approximation
% Calculate Fourier coefficients
a0 = (1/(2*L)) * integral(f, -L, L);
a_n = zeros(1, N);
b_n = zeros(1, N);
for n = 1:N
a_n(n) = (1/L) * integral(@(x) f(x) .* cos(n * (pi * x / L)), -L, L);
b_n(n) = (1/L) * integral(@(x) f(x) .* sin(n * (pi * x / L)), -L, L);
end
% Construct the Fourier series
syms x
fourier_series = a0;
for n = 1:N
fourier_series = fourier_series + a_n(n) * cos(n * (pi * x / L)) + b_n(n) * sin(n * (pi * x /
L));
end
% Display the Fourier series
disp('Fourier Series:');
disp(fourier_series);
% Plot the original function and the Fourier series
x_vals = linspace(-l, l, 400);
f_vals = f(x_vals);
fs_vals = double(subs(fourier_series, x, x_vals));
figure;
plot(x_vals, f_vals, 'b', 'LineWidth', 2); hold on;
plot(x_vals, fs_vals, 'r--', 'LineWidth', 2);
xlabel('x');
ylabel('f(x)');
title('Fourier Series Approximation of f(x) = x^2');
legend('Original Function', 'Fourier Series');
grid on;
axis([-l l 0 l^2]);
hold off;
OUTPUT: Fourier Series:
...
3) Compute the Fourier sine transform of f(x), plot and visualize.
To compute the Fourier sine transform of a function f(x)f(x)f(x), we can follow these steps in
MATLAB. Let's take the function f(x)=xf(x) = xf(x)=x for this example, which is defined on
the interval [0,∞)[0, \infty)[0,∞).
Fourier Sine Transform Definition
The Fourier sine transform of a function f(x)f(x)f(x) is defined as:
Fs(k)=∫0∞f(x)sin(kx) dxF_s(k) = \int_0^\infty f(x) \sin(kx) \, dxFs (k)=∫0∞
f(x)sin(kx)dx
MATLAB CODE:
% Define the function f(x)
f = @(x) x; % Change this to your desired function
% Define the Fourier sine transform function
F_s = @(k) integral(@(x) f(x) .* sin(k .* x), 0, Inf);
% Create a range of k values for plotting
k_vals = linspace(0, 10, 100); % Adjust the range as needed
Fs_vals = arrayfun(F_s, k_vals);
% Plotting the Fourier sine transform
figure;
plot(k_vals, Fs_vals, 'b', 'LineWidth', 2);
xlabel('k');
ylabel('F_s(k)');
title('Fourier Sine Transform of f(x) = x');
grid on;
OUTPUT:
f = @(x) exp(-x); % Example of a different function
4) Compute the Fourier Cosine transform of f(x), plot and visualize.
To compute the Fourier cosine transform of a function f(x)f(x)f(x), we can follow these steps in
MATLAB. Let’s take the function f(x)=e−xf(x) = e^{-x}f(x)=e−x for this example, which
is defined on the interval [0,∞)[0, \infty)[0,∞).
Fourier Cosine Transform Definition
The Fourier cosine transform of a function f(x)f(x)f(x) is defined as:
Fc(k)=∫0∞f(x)cos(kx) dxF_c(k) = \int_0^\infty f(x) \cos(kx) \, dxFc (k)=∫0∞
f(x)cos(kx)dx
MATLAB CODE:
% Define the function f(x)
f = @(x) exp(-x); % Change this to your desired function
% Define the Fourier cosine transform function
F_c = @(k) integral(@(x) f(x) .* cos(k .* x), 0, Inf);
% Create a range of k values for plotting
k_vals = linspace(0, 10, 100); % Adjust the range as needed
Fc_vals = arrayfun(F_c, k_vals);
% Plotting the Fourier cosine transform
figure;
plot(k_vals, Fc_vals, 'b', 'LineWidth', 2);
xlabel('k');
ylabel('F_c(k)');
title('Fourier Cosine Transform of f(x) = e^{-x}');
grid on;
OUTPUT:
f = @(x) x.^2;
5) Solving system of linear equations.
To solve a system of linear equations in MATLAB, you can use various methods, such as matrix
manipulation, built-in functions, or symbolic computation. Below, I'll provide a general example
of solving a system of linear equations using MATLAB.
Example System of Linear Equations
Consider the following system of equations:
2x+3y+z=14x+y+2z=23x+2y+3z=3\begin{align*} 2x + 3y + z &= 1 \\ 4x +
y + 2z &= 2 \\ 3x + 2y + 3z &= 3 \end{align*}2x+3y+z4x+y+2z3x+2y+3z =1=2=3
MATLAB CODE:
% Define the coefficient matrix A and the right-hand side vector b
A = [2 3 1;
4 1 2;
3 2 3];
b = [1; 2; 3];
% Solve the system of equations
x = A \ b; % This is the backslash operator for solving Ax = b
% Display the solution
disp('The solution is:');
disp(x);
OUTPUT:
The solution is:
1.0000
-1.0000
-1.0000
6) Find a basis and dimension for the vector subspace.
To find a basis and dimension for a vector subspace, we typically need to work with the subspace
defined by a set of vectors or by certain conditions (like linear equations). Below, I will outline
the steps to find the basis and dimension for a vector subspace, using a specific example to
illustrate the process.
Example: Subspace in R3\mathbb{R}^3R3
Let’s say we have the following vectors in R3\mathbb{R}^3R3:
MATLAB CODE:
% Define the vectors
v1 = [1; 2; 3];
v2 = [2; 4; 6];
v3 = [1; 0; -1];
% Construct the matrix A
A = [v1, v2, v3];
% Perform row reduction to find the reduced row echelon form
rref_A = rref(A);
% Display the RREF
disp('Reduced Row Echelon Form of A:');
disp(rref_A);
% Identify the pivot columns
pivot_columns = find(any(rref_A, 1)); % Indices of pivot columns
% Get the basis vectors
basis = A(:, pivot_columns);
% Display the basis
disp('Basis for the vector subspace:');
disp(basis);
% Dimension of the vector subspace
dimension = length(pivot_columns);
disp('Dimension of the vector subspace:');
disp(dimension);
OUTPUT:
Reduced Row Echelon Form of A:
1 2 0
0 0 1
0 0 0
Basis for the vector subspace:
-1
Dimension of the vector subspace:
7) Null space of a matrix.
To find the null space of a matrix, we will identify all vectors x\mathbf{x}x such that:
Ax=0A \mathbf{x} = \mathbf{0}Ax=0
where AAA is the matrix in question. The null space consists of all solutions to this
homogeneous equation.
MATLAB CODE:
% Define the matrix A
A = [1 2 1;
2 4 2;
3 6 3];
% Calculate the null space using the null function
null_space = null(A);
% Display the null space
disp('Null space of the matrix A:');
disp(null_space);
% If you want to see the basis vectors of the null space in parametric form
% and how they relate to the free variables
num_cols = size(A, 2); % Number of columns
num_free_vars = num_cols - size(null_space, 2); % Number of free variables
disp('Number of free variables:');
disp(num_free_vars);
OUTPUT:
Null space of the matrix A:
-1
Number of free variables:
1
8) Matrix representation of a linear transformation
A linear transformation can be represented as a matrix when you define the transformation with
respect to a basis. Below, I will explain how to find the matrix representation of a linear
transformation with a specific example.
Linear Transformation
Consider a linear transformation T:Rn→RmT: \mathbb{R}^n \to \
mathbb{R}^mT:Rn→Rm defined as:
T(x)=AxT(\mathbf{x}) = A \mathbf{x}T(x)=Ax
MATLAB CODE:
% Define the matrix A representing the linear transformation
A = [2 3;
4 -1];
% Define a vector x in R^2
x = [1; 0]; % e1
y = [0; 1]; % e2
% Apply the linear transformation
Tx = A * x; % Transformation of e1
Ty = A * y; % Transformation of e2
% Display the results
disp('T(e1) = A * e1:');
disp(Tx);
disp('T(e2) = A * e2:');
disp(Ty);
OUTPUT:
T(e1) = A * e1:
T(e2) = A * e2:
-1
Course Instructor HOD/MATHEMATICS
Dr.N.uthirasamy Dr.K.Senthilvadivu