Adv Math Lab 3&4 Code
Adv Math Lab 3&4 Code
% 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;
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');