% m-file contains an example of useful functions using
% Compute 5!
n = 10;
prod(1:n);
% compute sum of ten first numbers
sum(1:n)
% compute the vector containing subsequent products of its elements
cumprod(1:n)
% compute the vector containing subsequrent sums of its elements
cumsum(1:n)
% compute value of sinus(k) (from its definition!)
% x - x^3/3! + x^5/5! + ... + (-1)^n x^(2n + 1)/ (2n + 1)!
% a bit of math: what kind of power series is it?
% odd powers so we need construct a vactor of odd values
% n from the definition
n = 10;
n1 = 2*[0:n] + 1;
% compute vector of (2n + 1)!
t = cumprod(1:(2*n + 1));
% compute vector of 1/(2n+1)!
r = 1./t(1:2:end);
% point of computation
k = 2;
% k to different powers defined by p
p = k.^n1
% how about signs?
s = (-1).^[2:(n+2)]
% and together
output = cumsum(s.*p.*r)
% exact value
output2 = sin(k)
% to plot
% How do you describe the convergence of the "output" series?
plot([0:n], output, 'ro', [0:n], output2*ones(1, n+1), 'b-');
title(['Sinus for k = ', num2str(k)]);
xlabel('n');
ylabel(['sin(', num2str(k), ')']);
% as it is seen from the example above the prod, sum, cumsum and cumprod functions can be successfully applied to power expressions