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.

Short Answer

Expert verified
Create a new M-file named "factorial_function.m" and define the function as follows: ```matlab function result = factorial_function(N) if ~isscalar(N) || N < 0 || (mod(N, 1) ~= 0) error('Input N must be a non-negative integer.') end if N == 0 result = 1; return; end result = 1; for i = 1:N result = result * i; end end ``` This MATLAB script checks if the input N is a non-negative integer, handles the special case of 0! (returning 1), and calculates N! using a loop.

Step by step solution

01

Create a new M-file and define the function signature

To start, create a new M-file in MATLAB and name it "factorial_function.m". Then, define the function signature as: ```matlab function result = factorial_function(N) ``` Step 2: Check if N is valid input
02

Check if input N is valid

We need to check if N is a valid input (i.e., non-negative and an integer). We can use the `isinteger` and `isscalar` functions to check this: ```matlab if ~isscalar(N) || N < 0 || (mod(N, 1) ~= 0) error('Input N must be a non-negative integer.') end ``` Step 3: Calculate 0!
03

Handle special case of 0!

Since 0! is equal to 1, we need to check if N is equal to 0 and return 1 in that case: ```matlab if N == 0 result = 1; return; end ``` Step 4: Calculate N!
04

Calculate N! using a loop

Now, we can calculate the factorial function N! using a loop that iterates from 1 to N and multiplies the result by the loop variable in each iteration: ```matlab result = 1; for i = 1:N result = result * i; end ``` Step 5: Save and test the function
05

Save and test the M-file

Save the M-file "factorial_function.m". You can now test the function by running it in the MATLAB Command Window with different values of N to confirm it works correctly: ```matlab >> factorial_function(5) ans = 120 ``` The complete MATLAB script for the M-file "factorial_function.m" should look like this: ```matlab function result = factorial_function(N) if ~isscalar(N) || N < 0 || (mod(N, 1) ~= 0) error('Input N must be a non-negative integer.') end if N == 0 result = 1; return; end result = 1; for i = 1:N result = result * i; end end ```

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

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.

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\).)

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