0% found this document useful (0 votes)
18 views2 pages

MATLAB Series Functions and Demos

The document contains MATLAB functions and demo scripts for generating arithmetic progressions and Fibonacci sequences. It includes an 'arithmetic_progression' function to create an arithmetic sequence and a corresponding demo script to visualize it. Additionally, it features a 'fibonacci_basic' function to generate Fibonacci numbers and a demo script for plotting the Fibonacci sequence.

Uploaded by

bloodvjp68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

MATLAB Series Functions and Demos

The document contains MATLAB functions and demo scripts for generating arithmetic progressions and Fibonacci sequences. It includes an 'arithmetic_progression' function to create an arithmetic sequence and a corresponding demo script to visualize it. Additionally, it features a 'fibonacci_basic' function to generate Fibonacci numbers and a demo script for plotting the Fibonacci sequence.

Uploaded by

bloodvjp68
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;

You might also like