• Home
  • Textbooks
  • MATLAB Programming for Engineers
  • Input/Output Functions

MATLAB Programming for Engineers

Stephen J.Chapman

Chapter 8

Input/Output Functions - all with Video Answers

Educators


Chapter Questions

02:41

Problem 1

What is the difference between binary and formatted $1 / O$ ? Which MATLAB functions perform each type of L/O?

Anatole Borisov
Anatole Borisov
Numerade Educator
03:41

Problem 2

Write a MATLAB program to generate a table of the base-10 logarithms between 1 and 10 in steps of $0.1$. The table should start on a new page, and it should include a title describing the table and row and column headings. This table should be organized as shown below.

Willis James
Willis James
Numerade Educator
05:12

Problem 3

Write a MATLAB program that reads in a time in seconds since the start of the day (this value will be somewhere between $0.0$ and $86400.0$ ), and prints a character string containing time in the form HH:MM: SS using the 24-hour clock convention. Use the proper format converter to ensure that leading zeros are preserved in the MM and ss fields. Also, be sure to check the input number of seconds for validity, and write an appropriate error message if an invalid number is entered.

Willis James
Willis James
Numerade Educator
08:33

Problem 4

The acceleration due to the Earth's gravity at any height $h$ above the surface of the Earth is given by the equation
$$
g=-G \frac{M}{(R+h)^{2}}
$$
where $G$ is the gravitational constant $\left(6.672 \times 10^{-11} \mathrm{~N} \mathrm{~m}^{2} / \mathrm{kg}^{2}\right), M$ is the mass of the Earth $\left(5.98 \times 10^{24} \mathrm{~kg}\right), R$ is the mean radius of the Earth $(6371 \mathrm{~km})$, and $h$ is the height above the Earth's surface. If $M$ is measured in $\mathrm{kg}$ and $R$ and $h$ in meters, then the resulting acceleration will be in units of meters per second squared. Write a program to calculate the acceleration due to the Earth's gravity in $500-\mathrm{km}$ increments at heights from $0 \mathrm{~km}$ to $40,000 \mathrm{~km}$ above the surface of the Earth. Print out the results in a table of height versus acceleration with appropriate labels, including the units of the output values. Plot the data as well.

Kaitlin Coad
Kaitlin Coad
Numerade Educator
01:37

Problem 5

The program in Example $8.5$ illustrated the use of formatted $1 / O$ commands to read $(x, y)$ pairs of data from disk. This could also be done with the 1 oad -asci i function. Rewrite this program to use load instead of the formatted I/O functions. Test your rewritten program to confirm that it gives the same answers as Example 8.5.

AG
Ankit Gupta
Numerade Educator
01:40

Problem 6

Rewrite the program in Example $8.5$ to use the textread function instead of the formatted $1 / O$ functions. How difficult was it to use textread, compared to using load -ascif or the formatted l/O functions?

Mayank Tripathi
Mayank Tripathi
Numerade Educator
10:06

Problem 7

Write a program that reads an arbitrary number of real values from a user-specified input data file, rounds the values to the nearest integer, and writes the integers out to a user-specified output file. Make sure that the input file exists, and if not, tell the user and ask for another input file. If the output file exists, ask the user whether or not to delete it. If not, prompt for a different output file name.

Darren Wilson
Darren Wilson
Numerade Educator
00:38

Problem 8

Table of Sines and Cosines. Write a program to generate a table containing the sine and cosine of $\theta$ for $\theta$ between $0^{\circ}$ and $90^{\circ}$, in $1^{\circ}$ increments. The program should properly label each of the columns in the table.

AG
Ankit Gupta
Numerade Educator
08:07

Problem 9

Suppose that you have a sum of money $P$ in an interest-bearing account at a local bank ( $P$ stands for present value). If the bank pays you interest on the money at a rate of $i$ percent per year and compounds the interest monthly, the amount of money that you will have in the bank after $n$ months is given by the equation
$$
F=P\left(1+\frac{i}{1200}\right)^{n}
$$
where $F$ is the future value of the account and $\frac{i}{12}$ is the monthly percentage interest rate (the extra factor of 100 in the denominator converts the interest rate from percentages to fractional amounts). Write a MATLAB program that will read an initial amount of money $P$ and an annual interest rate $i$, and will calculate and wriv out a table showing the future value of the account every month for the next 5 years. The table should be written to an output file called "interest': Be sure to properly label the columns of your table.

Ernest Castorena
Ernest Castorena
Numerade Educator
01:13

Problem 10

Write a program to read a set of integers from an input data file, and locate the largest and smallest values within the data file. Print out the largest and smallest values, together with the lines on which they were found. Assume that you do not know the number of values in the file before the file is read.

SS
Sarvesh Somasundaram
Numerade Educator
03:43

Problem 11

We wrote a MATLAB program that calculated the arithmetic mean (average), rms average, geometric mean, and harmonic mean for a set of numbers. Modify that program to read an arbitrary number of values from an input data file, and calculate the means of those numbers. To test the program, place the following values into an input data file and run the program on that file: $1.0,2.0,5.0,4.0,3.0$, $2.1,4.7,3.0$.

Kajal Gautam
Kajal Gautam
Numerade Educator
00:57

Problem 12

Angles are often measured in degrees $\left({ }^{\circ}\right)$, minutes $\left({ }^{*}\right)$, and seconds $\left({ }^{*}\right)$, with 360 degrees in a circle, 60 minutes in a degree, and 60 seconds in a minute. Write a program that reads angles in radians from an input disk file, and converts them into degrees, minutes, and seconds. Test your program by placing the following four angles expressed in radians into an input file, and reading that file into the program: $0.0,1.0,3.141593,6.0$.

Erika Bustos
Erika Bustos
Numerade Educator
00:58

Problem 13

Create a data set in some other program on your computer, such as Microsoft Word, Microsoft Excel, a text editor, etc. Copy the data set to the clipboard using the Windows or UNIX copy function, and then use function uimport to load the data set into MATLAB.

Ashley Volpe
Ashley Volpe
Numerade Educator