0% found this document useful (0 votes)
3 views

Practice Questions

The document outlines various tasks related to programming in MATLAB, including generating the Fibonacci sequence, counting even Fibonacci numbers, analyzing soccer game results, processing a list of integers for positive and negative values, creating a matrix and calculating diagonal sums, finding the next binary palindrome year, plotting mathematical functions with specific formatting, and user input validation for patient registration. It also includes a brief explanation of the difference between 'disp' and 'fprintf' in MATLAB. Additionally, it explains the format specifier %4.7f and provides an example of its use.

Uploaded by

Papa
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)
3 views

Practice Questions

The document outlines various tasks related to programming in MATLAB, including generating the Fibonacci sequence, counting even Fibonacci numbers, analyzing soccer game results, processing a list of integers for positive and negative values, creating a matrix and calculating diagonal sums, finding the next binary palindrome year, plotting mathematical functions with specific formatting, and user input validation for patient registration. It also includes a brief explanation of the difference between 'disp' and 'fprintf' in MATLAB. Additionally, it explains the format specifier %4.7f and provides an example of its use.

Uploaded by

Papa
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
You are on page 1/ 3

1.

The Fibonacci sequence is a set of integers (the Fibonacci numbers) that


starts with a 0 followed by a 1, then by another 1, and then by a series of
steadily increasing numbers. The sequence follows the rule that each
number is equal to the sum of the preceding numbers.
Eg. 1st 14 numbers in the Fibonacci sequence:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...

Write a MATLAB program that takes an integer n from a user. The


program should then generate the Fibonacci sequence to the nth
number. Print out your result. (You can use a function if you prefer)

2. Find how many even Fibonacci numbers are available in the first n
numbers.
Consider the following first n = 14 numbers:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...

4 of them are even.

3. You are given a cell array containing information about a number of


soccer games. Each cell contains one of the following:
• 'H', meaning the home team won
• 'A', meaning the away team won
• 'D', meaning a draw, or a tie
So if,

games = {'D','D','A','H','D','H'}

Find how many draws there are in the cell array.

4. You have a phenomenon that produces strictly positive or negative


results.
delta = [1 -3 4 2 -1 6 -2 -7];

Marching through this list from beginning to end, mark a value with -1 if it
is the greatest magnitude negative yet seen (strictly greater, not equal).
Mark it with a one (1) if it has the greatest magnitude in the positive
direction (strictly greater, not equal). Just use a 0 if neither of these
conditions have been met.
The result for the above example would be:

result = [1 -1 1 0 0 1 0 -1]

5. Create a 40 by 40 matrix. Find the sum of the elements in the diagonal


that starts at the top-right corner and ends at the bottom-left corner.

6. The number 2015 is a palindrome in binary (11111011111 to be exact)


Given a year (in base 10 notation) calculate how many more years it will
be until the next year that is a binary palindrome. For example, if you are
given the year 1881 (palindrome in base 10! :-), the function should
output 30, as the next year that is a binary palindrome is 1911. You can
assume all years are positive integers.

7. Write a script to do the following: On a single figure, plot the functions


1
sinh(x), xcosh(x), tanh(x), and 𝑒 𝑥 for −1 ≤ x ≤ 1, with point spacing 𝛥𝑥 = .
10
Make sinh a red line, cosh a black dotted line, tanh a blue line with circles
at each point, and 𝑒 𝑥 just green ×’s with no line. Make a legend. Label your
axes and give the figure a title. Use linspace to create a vector of x values
and call each MATLAB mathematical function with vector arguments to
create the corresponding vector of “y” values.

8. When would you use disp instead of fprintf? When would you use fprintf
instead of disp?
9. Write a function called isapatient that first prompts the user to enter a
first name, last name, age and gender and then compares with existing
names, ages and genders in a file (called Registered Patients that will be
sent to you). If the inputs entered cannot be found in the file or if any of
the inputs do not correspond (for instance, the first name exists, but the
last name does not), then an error message should be printed (Please
enter a registered patient) and the input prompt should reappear for the
process to start again.

10. Explain what %4.7f means? Give an example of its use in MATLAB (use
MATLAB code to demonstrate this)

You might also like