Chapter 4: Problem 5
Write an M-file to calculate the factorial function N!, as defined in Example \(4.2\). Be sure to handle the special case of \(0 !\) Also, be sure to report an error if \(\mathrm{N}\) is negative or not an integer.
Chapter 4: Problem 5
Write an M-file to calculate the factorial function N!, as defined in Example \(4.2\). Be sure to handle the special case of \(0 !\) Also, be sure to report an error if \(\mathrm{N}\) is negative or not an integer.
All the tools & learning materials you need for study success - in one app.
Get started for freeFibonacei 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.
Write the MATLAB statements required to calculate and print out the squares of all the even integers between 0 and 50 . Create a table consisting of each integer and its square, with appropriate labels over each column.
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.
Tension on a Cable. A 200-pound object is to be hung from the cnd of a rigid 8 -foot horizontal pole of negligible weight, as shown in Figure 4.5. The pole is attached to a wall by a pivor and is supported by an 8 -foot cable that is attached to the wall at a higher point. The tension os this cable is given by the equation $$ T=\frac{W+k c \cdot l p}{d \sqrt{l p^{2}-d^{2}}} $$ where \(T\) is the tension on the cable, \(W\) is the weight of the object. Ic is the length of the cable, Ip is the length of the pole, and \(d\) is the distance along the pole at which the cable is attached. Write a program to determine the distance \(d\) at which to attach the cable to the pole to minimize the tension on the cable. To do this, the program should calculate the tension on the cable at regular l-foot intervals from \(d=1\) foot to \(d=7\) fect. and should locate the position \(d\) that produces the minimum tens on. Also, the program should plot the tension on the cable as a function of \(d\), with appropriate tities and axis labels.
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\).)
What do you think about this solution?
We value your feedback to improve our textbook solutions.