Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 4: User-defined functions (19 points) The Fibonacci sequence is characteriz

ID: 3741459 • Letter: P

Question

Part 4: User-defined functions (19 points) The Fibonacci sequence is characterized by the set of numbers beginning with Fi 1, F2 1, and every following number is the sum of the previous two numbers: F F-2+ F-1. Create a user-defined function that calculates a sequence similar to the Fibonacci sequence using any two starting numbers-F-a, F2 = b,R-Fn-2 + Fn-1-and returns the nth number in the sequence. Before you begin writing your function, take some time to answer these questions. All answers should be written in the appropriate space in the table on the last page. What is the name of your function going to be? NOTE: The rules for naming functions are the same as naming scripts and variables in MATLAB Write a short line stating the purpose of your function. What input arguments do you need in order to transfer data into the function? Write out each input argument; its name and description. Think about what data you need from the user in order to perform the appropriate calculations. DO NOT use the input) function in MATLAB for this part. What output arguments do you need in order to transfer data out of the function? Write out each output argument; its name and description. Think about what information/data the user wants from the calculations. Write down the function definition line for your function using your answers in parts 1, 2, and 3 1. 2. 3. 4. 5. 6. Complete your function body by translating the following steps into MATLAB code: if the nth number is 1 if the nth number is 2 anything else return Fi return F2 for the number of desired terms starting at 3 return Fo Fnz+ Fnl

Explanation / Answer

Copy paste the following code in a file named fibonacci.m

function Fn = fibonacci(F1, F2, n)
if n == 1
Fn = F1;
elseif n == 2
Fn = F2;
else
Fn1 = F1;
Fn2 = F2;
for i = 3:n
Fn = Fn1 + Fn2;
Fn1 = Fn2;
Fn2 = Fn;
end
end
end

=======================
Now ,i n command window type fibonacci(3, 5 , 10)
output is
ans = 233

================

Other answers given below
1. Name of the function is fibonacci

2. The function fibonacci() returns the nth fibonacci number given the first 2 terms in the sequence the value of n, to return the nth term in the sequence

3. The input arguments are F1, F2 and n
F1: the first term in the Fibonacci sequence
F2: the second term in the Fibonacci sequence
n: the value of n , to find the nth term in the sequence

4. The output argument is just a single number Fn which the the nth term in the sequence

5. function Fn = fibonacci(F1, F2, n)

6. if n == 1
Fn = F1;
elseif n == 2
Fn = F2;
else
Fn1 = F1;
Fn2 = F2;
for i = 3:n
Fn = Fn1 + Fn2;
Fn1 = Fn2;
Fn2 = Fn;
end
end

7. At the end of function body , typed end

9. In command window type fibonacci(3, 5 , 10)
output is
ans = 233


10. a. The variable names used when calling function need not be same as those used inside function
b. The variables used in the function don't appear in the workspace

****** Please do rate the answer if it helped. thank you. *********