0% found this document useful (0 votes)
10 views6 pages

Adv Math Lab 3&4 Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Adv Math Lab 3&4 Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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');
grid on;

% 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;

LAB 4
% Power Series Solution of y'' + y = 0
syms x
N = 10; % Number of terms in the series expansion
a = zeros(1, N); % Initialize coefficient array
a(1) = 1; % Initial condition: a_0 = 1
a(2) = 0; % Assume a_1 = 0 for simplicity
% Compute coefficients using the recurrence relation
for n = 0:N-3
a(n+3) = -a(n+1) / (n+2) / (n+1);
end
% Construct the power series solution
y_series = sum(a .* x.^(0:N-1));
% Plot the solution
fplot(y_series, [-2, 2]);
grid on;
title('Power Series Solution of y'''' + y = 0');
xlabel('x'); ylabel('y');

% Power Series Solution of y'' - 2y' + y = 0


syms x
N = 10; % Number of terms in the series expansion
a = zeros(1, N); % Initialize coefficient array
a(1) = 1; % Initial condition: a_0 = 1
a(2) = 1; % Assume a_1 = 1 for simplicity
% Compute coefficients using the recurrence relation
for n = 0:N-3
a(n+3) = (2 * (n + 1) * a(n + 2) - a(n + 1)) / (n + 2) / (n + 1);
end
% Construct the power series solution
y_series = sum(a .* x.^(0:N-1));
% Plot the solution.
fplot(y_series, [-2, 2]);
grid on;
title('Power Series Solution of y'''' - 2y'' + y = 0');
xlabel('x'); ylabel('y');

% Power Series Solution of y'' + xy = 0


syms x
N = 10; % Number of terms in the series expansion
a = zeros(1, N); % Initialize coefficient array
a(1) = 1; % Initial condition: a_0 = 1
a(2) = 0; % Assume a_1 = 0 for simplicity
% Compute coefficients using the recurrence relation
for n = 0:N-3
a(n + 3) = -a(n + 1) / (n + 2) / (n + 1);
end
% Construct the power series solution
y_series = sum(a .* x.^(0:N-1));
% Plot the solution
fplot(y_series, [-2, 2]);
grid on;
title('Power Series Solution of y'''' + xy = 0');
xlabel('x'); ylabel('y');

LAB 4 YUNG KILA ALDREI

% Power Series Solution of y'' + y = 0


syms x
N = 10; % Number of terms in the series expansion
% Initialize symbolic coefficient array
a = sym(zeros(1, N));
a(1) = 1; % Initial condition: a_0 = 1
a(2) = 0; % Assume a_1 = 0 for simplicity
% Compute coefficients using the recurrence relation
for n = 0:N-3
a(n+3) = -a(n+1) / ((n+2) * (n+1));
end
% Construct the power series solution
y_series = sum(a .* x.^(0:N-1));
% Plot the solution
fplot(y_series, [-2, 2]);
grid on;
title('Power Series Solution of y'''' + y = 0');
xlabel('x'); ylabel('y');

% Power Series Solution of y'' - 2y' + y = 0


syms x
N = 10;
% Initialize symbolic coefficient array
a = sym(zeros(1, N));
a(1) = 1; % Initial condition: a_0 = 1
a(2) = 1; % Assume a_1 = 0 for simplicity
% Compute coefficients using recurrence relation
for n = 0:N-3
a(n+3) = - (2 / (n+2)) * a(n+1);
end
% Construct power series solution
y_series = sum(a .* x.^(0:N-1));
% Plot the solution
fplot(y_series, [-2, 2]);
grid on;
title('Power Series Solution of y'''' + 2y'' = 0');
xlabel('x'); ylabel('y');

% Power Series Solution of y'' - 2y' + y = 0


syms x
N = 10;
% Initialize symbolic coefficient array
a = sym(zeros(1, N));
a(1) = 1; % Initial condition: a_0 = 1
a(2) = 0; % Assume a_1 = 0 for simplicity
% Compute coefficients using recurrence relation
for n = 0:N-3
a(n+3) = - (2*(n+1)*a(n+1) + a(n+1)) / ((n+2)*(n+1));
end
% Construct power series solution
y_series = sum(a .* x.^(0:N-1));
% Plot the solution
fplot(y_series, [-2, 2]);
grid on;
title('Power Series Solution of y'''' + 2y'' + y = 0');
xlabel('x'); ylabel('y');

You might also like