Untitled
unknown
plain_text
a month ago
795 B
4
Indexable
% Power Series Approximation of e^x taylor_exp = @(x, n) sum(arrayfun(@(k) x.^k ./ factorial(k), 0:n)); x = -2:0.1:2; % Define range n_terms = [2, 5, 10, 20]; % Number of terms to compare figure; hold on; for n = n_terms y_approx = arrayfun(@(x) taylor_exp(x, n), x); plot(x, y_approx, 'DisplayName', sprintf('%d terms', n)); end plot(x, exp(x), 'k', 'LineWidth', 2, 'DisplayName', 'e^x'); legend; title('Power Series Approximation of e^x'); xlabel('x'); ylabel('f(x)'); grid on; hold off; % Compute and Plot Error figure; hold on; for n = n_terms y_approx = arrayfun(@(x) taylor_exp(x, n), x); error = abs(exp(x) - y_approx); plot(x, error, 'DisplayName', sprintf('%d terms', n)); end title('Error in Power Series Approximation of e^x'); xlabel('x'); ylabel('Error'); legend; grid on; hold off;
Editor is loading...
Leave a Comment