Fibonacei Numbers. The nth Fibonacci number is defined by the following recursive equations: $$ \begin{aligned} &f(1)=1 \\ &f(2)=2 \\ &f(\mathrm{n})=f(\mathrm{n}-1)+f(\mathrm{n}-2) \end{aligned} $$ Therefore, \(f(3)=f(2)+f(1)=2+1=3\), and so forth for higher numbers, Write an M-file to calculate and write out the nth Fibonacci number for \(n>2\), where \(n\) is input by the user. Use a while loop to perform the calculation.

Short Answer

Expert verified
To calculate the nth Fibonacci number for $n>2$ using a while loop, create an M-file with the following MATLAB script: ``` MATLAB % Input the value of n n = input('Enter the value of n (>2): '); % Initialize variables f1 = 1; f2 = 2; count = 2; % Compute nth Fibonacci number using a while loop while count < n fn = f1 + f2; f1 = f2; f2 = fn; count = count + 1; end % Display the result disp(['The ' num2str(n) 'th Fibonacci number is: ' num2str(fn)]); ``` This script takes input n from the user, initializes variables, then uses a while loop to iteratively compute the nth Fibonacci number using the recursive equation. Once the loop is completed, the script displays the nth Fibonacci number to the user.

Step by step solution

01

Understanding the Fibonacci sequence

The Fibonacci sequence is a series of numbers where each number is the sum of the two numbers preceding it. It starts as follows: 1, 2, 3, 5, 8, 13, 21, ... The recursive equations for the Fibonacci sequence are: \(f(1) = 1\) \(f(2) = 2\) \(f(n) = f(n - 1) + f(n - 2)\) We will calculate the nth Fibonacci number while using a while loop to perform the calculation.
02

Initialize variables

We will initialize the variables inside the MATLAB script. Let 'n' be the input value by the user. We set f1 = 1 and f2 = 2 which represent the first two numbers in the sequence. ``` MATLAB n = input('Enter the value of n (>2): '); f1 = 1; f2 = 2; count = 2; ```
03

Implement the while loop

Next, we will implement a while loop that will iterate until the count reaches the input value 'n'. Inside the while loop, we will calculate the next Fibonacci number using the recursive equation and update the values of f1 and f2 in each iteration. ``` MATLAB while count < n fn = f1 + f2; f1 = f2; f2 = fn; count = count + 1; end ```
04

Display the output

After the while loop is done executing, the nth Fibonacci number will be stored in the variable 'fn'. We will display the output (resulting Fibonacci number) to the user. ``` MATLAB disp(['The ' num2str(n) 'th Fibonacci number is: ' num2str(fn)]); ```
05

Complete MATLAB script

Finally, here is the complete MATLAB script: ``` MATLAB % Input the value of n n = input('Enter the value of n (>2): '); % Initialize variables f1 = 1; f2 = 2; count = 2; % Compute nth Fibonacci number using a while loop while count < n fn = f1 + f2; f1 = f2; f2 = fn; count = count + 1; end % Display the result disp(['The ' num2str(n) 'th Fibonacci number is: ' num2str(fn)]); ```

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Program doy in Example \(4.3\) calculates the day of year associated with any given month, day, and year. As written, this program docs not check to sce if the data entered by the user is valid. It will accept nonsense values for months and days and do calculations with them to produce meaningless results. Modify the program so that it checks the input values for validity before using them. If the inputs are invalid, the program should tell the user what is wrong and quit. The year should be a number greater than zero, the month should be a number between 1 and 12 , and the day should be a number between 1 and a maximum that depends on the month. Use a switch construct to implement the bounds checking performed on the day.

Bacterial Growth. Suppose that a biologist performs an experiment in which he or she measures the rate at which a specific type of bacterium reproduces asexually in different culture media. The experiment shows that in Medium A the bacteria reproduce once every 60 minutes, and in Medium B the bacteria reproduce once every 90 minutes. Assume that a single bacterium is placed on each culture medium at the beginning of the experiment. Write a program that calculates and plots the number of bacteria present in cach culture at intervals of 3 hours from the beginning of the experiment antil 24 hours have elapsed. Make two plots, one a linear xy plot and the other a linear-log (semi logy) plot. How do the numbers of bacteria compare on the two media after 24 hours?

Modify program 1sqfit from Example \(4.7\) to read its input values from an ASCII file named input.1. dat. The data in the file will be organized in rows, with one pair of \((x, y)\) values on each row, as shown below: $$ \begin{array}{ll} 1.1 & 2.2 \\ 2.2 & 3.3 \end{array} $$ Test your program using the same two data sets that were used in Example 4.7. (Hint: Use the load command to read the data into an array named input1, and then store the first column of inpat 1 into array \(x\) and the second column of input 1 into array \(y\).)

Declbels. Engineers often measure the ratio of two power measurements in decibels, or dB. The equation for the ratio of two power measurements in decibels is $$ d B=10 \log _{10} \frac{P_{2}}{P_{1}} $$ where \(P_{2}\) is the power level being measured, and \(P_{1}\) is some reference power level. Assume that the reference power level \(P_{1}\) is 1 watt, and write a program that calculates the decibel level corresponding to power levels between 1 and 20 watts, in \(0.5 \mathrm{~W}\) steps. Plot the dB-versus- power curve on a log-linear scale.

Geometric Mean. The geometric mean of a set of numbers \(x_{1}\) through \(x_{n}\) is defined as the \(n\)th root of the product of the numbers $$ \text { geometric mean }=\sqrt[4]{x_{1} x_{2} x_{3} \ldots x_{n}} $$ Write a MATLAB program that will accept an arbitrary number of positive input values and calculate both the arithmetic mean (i.e., the average) and the geometric mean of the numbers. Use a while loop to get the input values and terminate the inputs when a user enters a negative number. Test your program by calculating the average and geometric mean of the four numbers \(10,5,2\), and 5 .

See all solutions

Recommended explanations on Psychology Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free