Problem 3 Bowman sequence is an integer sequence calculated
Problem 3
Bowman sequence is an integer sequence calculated by adding the previous 3 numbers together to calculate the next value. This is represented mathematically by saying that F_n = F_n - 1 + F_n - 2 + F_n - 3 (where F_n is the n^th value in the sequence, F). For example, a Bowman sequence of length 8 will be: Note, this sequence always starts with the underlined values (0, 1, 2). Write a MATLAB program that will: Request the user for the length of the desired Bowman sequence to generate. Display an error message and terminate if the input is invalid (for example, -8). Return an array containing the Bowman sequence of the input length if the input is valid.Solution
% matlab code bowman sequence
function sequence = bowman_sequence()
length = input(\"Enter the length of desired bowman sequence: \");
if length <= 0
disp(\"Invalid Input\ \")
exit
else
sequence(1) = 0;
sequence(2) = 1;
sequence(3) = 2;
for i=4:length
sequence(i) = sequence(i-3)+sequence(i-2)+sequence(i-1);
end
end
end
disp(\"Bowman Sequence: \")
sequence = bowman_sequence()
disp(sequence)
%{
output:
Bowman Sequence:
Enter the length of desired bowman sequence: -8
Invalid Input
Bowman Sequence:
Enter the length of desired bowman sequence: 10
sequence = 0 1 2 3 6 11 20 37 68 125
%}
