Advanced Lab Code 1-3
Advanced Lab Code 1-3
LAB 2
% Laboratory Act. no.2: Laplace and Inverse Laplace Transform
F = laplace(f,y)
F = laplace(f,a,y)
F = laplace(x,vars,transVars)
syms x
f = ilaplace(F,x)
f = ilaplace(F,a,x)
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');
% 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;