LAB 1
% Laboratory no.1 Complex Numbers and Complex Variables
% Define Complex Numbers
z1 = 3 + 4i; % using direct notation
z2 = complex(2, -5); % using complex function
% Perform Arithmetic Operations
addition = z1 + z2;
subtraction = z1 - z2;
multiplication = z1 * z2;
division = z1 / z2;
% Find Magnitude and Phase
magnitude_z1 = abs(z1);
phase_z1 = angle(z1); % in radians
phase_z1_deg = rad2deg(phase_z1); % Convert to degrees
% Compute the Conjugate
conjugate_z1 = conj(z1);
% Convert Cartesian to Polar Form
r = abs(z1);
theta = angle(z1);
polar_form = r * exp(1i * theta);
% Plot on the Argand Plane
z = [z1, z2];
figure; hold on; grid on;
plot(real(z), imag(z), 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
xlabel('Real Axis'); ylabel('Imaginary Axis');
title('Argand Diagram');
axis equal;
LAB 2
% Laboratory Act. no.2: Laplace and Inverse Laplace Transform
%Laplace transform of symbolic expression
syms x y
f = 1/sqrt(x);
F = laplace(f)
%Independent and transformation variables
syms a t y
f = exp(-a*t);
F = laplace(f)
F = laplace(f,y)
F = laplace(f,a,y)
%Relation between laplace trns. of func. and its derivative
syms f(t) s
Df = diff(f(t),t);
F = laplace(Df,t,s)
%Laplace transform of array inputs
syms a b c d w x y z
M = [exp(x) 1; sin(y) 1i*z];
vars = [w x; y z];
transVars = [a b; c d];
F = laplace(M,vars,transVars)
F = laplace(x,vars,transVars)
%Inverse laplace trns. of symbolic express.
syms s
F = 1/s^2;
f = ilaplace(F)
%Default independent variable and transformation variable
syms a s
F = 1/(s-a)^2;
f = ilaplace(F)
syms x
f = ilaplace(F,x)
f = ilaplace(F,a,x)
%Inverse laplace trns. of array inputs
syms a b c d w x y z
M = [exp(x) 1; sin(y) 1i*z];
vars = [w x; y z];
transVars = [a b; c d];
f = ilaplace(M,vars,transVars)
syms w x y z a b c d
f = ilaplace(x,vars,transVars)
LAB 3
% A. Power Series (Taylor Series Approximation of sin(x))
clc; clear; close all;
% Define range of x values
x = linspace(-pi, pi, 100);
% Number of terms in the Taylor series expansion
N = 6;
approx_sin = zeros(size(x));
% Compute the Taylor series expansion manually
for n = 0:N
approx_sin = approx_sin + ((-1)^n * x.^(2*n+1)) / factorial(2*n+1);
end
% Plot the results
figure;
plot(x, sin(x), 'r', 'LineWidth', 2); % Exact function
hold on;
plot(x, approx_sin, 'b--', 'LineWidth', 2); % Taylor approximation
legend('Exact sin(x)', 'Taylor Approximation');
xlabel('x');
ylabel('Function Value');
title('Taylor Series Approximation of sin(x)');
grid on;
% A. Power Series (Taylor Series Approximation of e^x) HINDI NA KUHA KASI SAME LANG NG CODE SA UNA
clc; clear; close all;
% Define range of x values
x = linspace(-2, 2, 100);
% Number of terms in the Taylor series expansion
N = 5;
approx_exp = zeros(size(x));
% Compute the Taylor Series expansion manually
for n = 0:N-1
approx_exp = approx_exp + (x.^n) / factorial(n);
end
% Plot the result
figure;
plot(x, exp(x), 'r', 'LineWidth', 2); % Exact function
hold on;
plot(x, approx_exp, 'b--', 'LineWidth', 2); % Taylor Approximation
legend('Exact e^x', 'Taylor Approximation');
xlabel('x');
ylabel('Function Value');
title('Taylor Series Approximation of e^x');
% B. Fourier Series Approximation of Square Wave
clc; clear; close all;
% Define x range
x = linspace(0, 2*pi, 1000);
N = 10; % Number of Fourier terms
square_wave_approx = zeros(size(x));
% Compute Fourier series approximation
for n = 1:2:N % Only odd terms contribute to the series
square_wave_approx = square_wave_approx + (4/pi) * (1/n) * sin(n*x);
end
% Generate actual square wave for comparison
actual_square_wave = sign(sin(x));
% Plot results
figure;
plot(x, actual_square_wave, 'r', 'LineWidth', 2); % Exact square wave
hold on;
plot(x, square_wave_approx, 'b--', 'LineWidth', 2); % Fourier approximation
legend('Exact Square Wave', 'Fourier Approximation');
xlabel('x');
ylabel('Function Value');
title('Fourier Series Approximation of Square Wave');
grid on;
% B. Fourier Series Approximation of Sawtooth Wave
clc; clear; close all;
% Define x range
x = linspace(0, 2*pi, 1000);
N = 10; % Number of Fourier terms
sawtooth_wave_approx = zeros(size(x));
% Compute Fourier series approximation
for n = 1:N
sawtooth_wave_approx = sawtooth_wave_approx + (2/pi) * ((-1)^(n+1)/n) * sin(n*x);
end
% Generate actual sawtooth wave for comparison
actual_sawtooth_wave = (x/pi) - 1;
% Plot results
figure;
plot(x, actual_sawtooth_wave, 'r', 'LineWidth', 2); % Exact sawtooth wave
hold on;
plot(x, sawtooth_wave_approx, 'b--', 'LineWidth', 2); % Fourier approximation
legend('Exact Sawtooth Wave', 'Fourier Approximation');
xlabel('x');
ylabel('Function Value');
title('Fourier Series Approximation of Sawtooth Wave');
grid on;
% C. Fourier Transform
clc; clear; close all;
% Signal parameters
fs = 100; % Sampling frequency (Hz)
t = 0:1/fs:1; % Time vector (1 second duration)
f = 5; % Signal frequency (Hz)
signal = sin(2*pi*f*t); % Sinusoidal signal
% Compute Fourier Transform using FFT
N = length(signal);
freq = (-N/2:N/2-1)*(fs/N); % Frequency axis
fft_signal = fftshift(fft(signal)); % Shift zero frequency to center
% Plot the signal
figure;
subplot(2,1,1);
plot(t, signal, 'b', 'LineWidth', 1.5);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Sinusoidal Signal');
grid on;
% Plot the magnitude spectrum
subplot(2,1,2);
plot(freq, abs(fft_signal)/N, 'r', 'LineWidth', 1.5);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Fourier Transform (Magnitude Spectrum)');
grid on;