function called y=x(t,n) that calculates the taylor series in matlab

Here's an implementation of the function y=x(t,n) that calculates the taylor series of e^t up to n terms in MATLAB:

main.m
function y = x(t,n)
% y = x(t,n) calculates the Taylor series of e^t up to n terms.
% Syntax: y = x(t,n)
% Inputs:
%   t = input variable of e^t
%   n = number of terms in the Taylor series expansion
% Output:
%   y = value of the Taylor series at t

y = 1; % start with the first term of the series
for i = 1:n
    % Calculate the next term of the series, which is t^i/i! 
    % Add it to the previous sum to get the total value of the series.
    y = y + t^i/factorial(i);
end
end
483 chars
17 lines

To use this function, simply call it with a value of t and the desired number of terms n, like so:

main.m
y = x(1,5);
12 chars
2 lines

This will calculate the value of the Taylor series of e^1 up to 5 terms. The result will be stored in the variable y.

gistlibby LogSnag