MATLAB Series Functions and Demos
1. Arithmetic Progression Function (arithmetic_progression.m)
function ap = arithmetic_progression(a, d, n)
%ARITHMETIC_PROGRESSION Generate an arithmetic progression vector.
% AP = ARITHMETIC_PROGRESSION(A, D, N) returns a 1×N row vector:
% AP(k) = A + (k-1)*D, k = 1..N
if ~isscalar(n) || n <= 0 || n ~= fix(n)
error('Number of terms n must be a positive integer.');
end
ap = a + (0:n-1)*d;
end
2. Arithmetic Progression Demo Script (demo_AP.m)
%% Demo: arithmetic progression & plot
a = 2; % first term
d = 3; % common difference
n = 4; % number of terms
ap = arithmetic_progression(a, d, n);
figure;
stem(1:n, ap, 'filled');
title(sprintf('Arithmetic Progression: a=%g, d=%g', a, d));
xlabel('k (term index)'); ylabel('a_k');
grid on;
3. Fibonacci Function (fibonacci_basic.m)
function fib = fibonacci_basic(n)
%FIBONACCI_BASIC Return first n Fibonacci numbers (0-indexed).
% FIB = FIBONACCI_BASIC(N) returns a 1×N row vector.
if ~isscalar(n) || n <= 0 || n ~= fix(n)
error('Input n should be a positive integer.');
end
fib = zeros(1, n); % pre-allocate
fib(1) = 0;
if n >= 2, fib(2) = 1; end
for k = 3:n
fib(k) = fib(k-1) + fib(k-2);
end
end
MATLAB Series Functions and Demos
4. Fibonacci Demo Script (demo_Fib.m)
%% Demo: Fibonacci series & plot
n = 10;
fib = fibonacci_basic(n);
figure;
plot(1:n, fib, 'o-', 'LineWidth', 1.5);
title(sprintf('Fibonacci sequence (n = %d)', n));
xlabel('k'); ylabel('F_k');
grid on;