Chapter 1

Starting with MATLAB

This chapter begins by describing the characteristics and purpose of the different windows in MATLAB. Next, the Command Window is introduced in detail. The chapter shows how to use MATLAB for arithmetic operations with scalars in much to the way that a calculator is used. This includes the use of elementary math functions with scalars. The chapter then shows how to define scalar variables (the assignment operator) and how to use these variables in arithmetic calculations. The last section in the chapter introduces script files. It shows how to write, save, and execute simple MATLAB programs.

1.1 STARTING MATLAB, MATLAB WINDOWS

It is assumed that the software is installed on the computer, and that the user can start the program. Once the program starts, the MATLAB desktop window opens with the default layout, Figure 1-1. The layout has a Toolstrip at the top, the Current Folder Toolbar below it, and four windows underneath. At the top of the Toolstrip there are three tabs: HOME, PLOTS, and APPS. Clicking on the tabs changes the icons in the Toolstrip. Commonly, MATLAB is used with the HOME tab selected. The associated icons are used for executing various commands, as explained later in this chapter. The PLOTS tab can be used to create plots, as explained in Chapter 5 (Section 5.12), and the APPS tab can be used for opening additional applications and Toolboxes of MATLAB.

The default layout

The default layout (Figure 1-1) consists of the following four windows that are displayed under the Toolstrip: the Command Window (larger window at the center), the Current Folder Window (on the left) and the Workspace and Command History windows (on the right). A list of several MATLAB windows and their purposes is given in Table 1-1.

Four of the windows—the Command Window, the Figure Window, the Editor Window, and the Help Window—are used extensively throughout the book and are briefly described on the following pages. More detailed descriptions are included in the chapters where they are used. The Command History Window, Current Folder Window, and the Workspace Window are described in Sections 1.2, 1.8.4, and 4.1, respectively.

images

Figure 1-1: The default view of MATLAB desktop.

Command Window: The Command Window is MATLAB’s main window and opens when MATLAB is started. It is convenient to have the Command Window as the only visible window. This can be done either by closing all the other windows, or by selecting Command Window Only in the menu that opens when the Layout icon on the Toolstrip is selected. To close a window, click on the pull-down menu at the top right-hand side of the window and then select Close. Working in the Command Window is described in detail in Section 1.2.

Table 1-1: MATLAB windows

Window Purpose
Command Window Main window, enters variables, runs programs.
Figure Window Contains output from graphic commands.
Editor Window Creates and debugs script and function files.
Help Window Provides help information.
Command History Window Logs commands entered in the Command Window.
Workspace Window Provides information about the variables that are stored.
Current Folder Window Shows the files in the current folder.

Figure Window: The Figure Window opens automatically when graphics commands are executed, and contains graphs created by these commands. An example of a Figure Window is shown in Figure 1-2. A more detailed description of this window is given in Chapter 5.

images

Figure 1-2: Example of a Figure Window.

Editor Window: The Editor Window is used for writing and editing programs. This window is opened by clicking on the New Script icon in the Toolstrip, or by clicking on the New icon and then selecting Script from the menu that opens. An example of an Editor Window is shown in Figure 1-3. More details on the Editor Window are given in Section 1.8.2, where it is used for writing script files, and in Chapter 7, where it is used to write function files.

Help Window: The Help Window contains help information. This window can be opened from the Help icon in the Toolstrip of the Command Window or the toolbar of any MATLAB window. The Help Window is interactive and can be used to obtain information on any feature of MATLAB. Figure 1-4 shows an open Help Window.

When MATLAB is started for the first time, the screen looks like that shown in Figure 1-1. For most beginners it is probably more convenient to close all the windows except the Command Window. The closed windows can be reopened by selecting them from the layout icon in the Toolstrip. The windows shown in Figure 1-1 can be displayed by clicking on the layout icon and selecting Default in the menu that opens. The various windows in Figure 1-1 are docked to the desktop. A window can be undocked (become a separate, independent window) by dragging it out. An independent window can be redocked by clicking on the pull-down menu at the top right-hand side of the window and then selecting Dock.

images

Figure 1-3: Example of an Editor Window.

images

Figure 1-4: The Help Window.

1.2 WORKING IN THE COMMAND WINDOW

The Command Window is MATLAB’s main window and can be used for executing commands, opening other windows, running programs written by the user, and managing the software. An example of the Command Window, with several simple commands that will be explained later in this chapter, is shown in Figure 1-5.

images

Figure 1-5: The Command Window.

Notes for working in the Command Window:

  • To type a command, the cursor must be placed next to the command prompt (≫).
  • Once a command is typed and the Enter key is pressed, the command is executed. However, only the last command is executed. Everything executed previously (that might be still displayed) is unchanged.
  • Several commands can be typed in the same line. This is done by typing a comma between the commands. When the Enter key is pressed, the commands are executed in order from left to right.
  • It is not possible to go back to a previous line that is displayed in the Command Window, make a correction, and then re-execute the command.
  • A previously typed command can be recalled to the command prompt with the up-arrow key (images). When the command is displayed at the command prompt, it can be modified if needed and then executed. The down-arrow key (images) can be used to move down the list of previously typed commands.
  • If a command is too long to fit in one line, it can be continued to the next line by typing three periods … (called an ellipsis) and pressing the Enter key. The continuation of the command is then typed in the new line. The command can continue line after line up to a total of 4,096 characters.

The semicolon (;):

When a command is typed in the Command Window and the Enter key is pressed, the command is executed. Any output that the command generates is displayed in the Command Window. If a semicolon (;) is typed at the end of a command, the output of the command is not displayed. Typing a semicolon is useful when the result is obvious or known, or when the output is very large.

If several commands are typed in the same line, the output from any of the commands will not be displayed if a semicolon instead of a comma is typed between the commands.

Typing %:

When the symbol % (percent) is typed at the beginning of a line, the line is designated as a comment. This means that when the Enter key is pressed the line is not executed. The % character followed by text (comment) can also be typed after a command (in the same line). This has no effect on the execution of the command.

Usually there is no need for comments in the Command Window. Comments, however, are frequently used in a program to add descriptions or to explain the program (see Chapters 4 and 6).

The clc command:

The clc command (type clc and press Enter) clears the Command Window. After typing in the Command Window for a while, the display may become very long. Once the clc command is executed, a clear window is displayed. The command does not change anything that was done before. For example, if some variables were defined previously (see Section 1.6), they still exist and can be used. The up-arrow key can also be used to recall commands that were typed before.

The Command History Window:

The Command History Window lists the commands that have been entered in the Command Window. This includes commands from previous sessions. A command in the Command History Window can be used again in the Command Window. By double-clicking on the command, the command is reentered in the Command Window and executed. It is also possible to drag the command to the Command Window, make changes if needed, and then execute it. The list in the Command History Window can be cleared by selecting the lines to be deleted and then right-clicking the mouse and selecting Delete Selection. The whole history can be deleted by right-clicking the mouse and selecting choose Clear Command History in the menu that opens.

1.3 ARITHMETIC OPERATIONS WITH SCALARS

In this chapter we discuss only arithmetic operations with scalars, which are numbers. As will be explained later in the chapter, numbers can be used in arithmetic calculations directly (as with a calculator) or they can be assigned to variables, which can subsequently be used in calculations. The symbols of arithmetic operations are:

images

It should be pointed out here that all the symbols except the left division are the same as in most calculators. For scalars, the left division is the inverse of the right division. The left division, however, is mostly used for operations with arrays, which are discussed in Chapter 3.

1.3.1 Order of Precedence

MATLAB executes the calculations according to the order of precedence displayed below. This order is the same as used in most calculators.

Precedence Mathematical Operation
First Parentheses. For nested parentheses, the innermost are executed first.
Second Exponentiation.
Third Multiplication, division (equal precedence).
Fourth Addition and subtraction.

In an expression that has several operations, higher-precedence operations are executed before lower-precedence operations. If two or more operations have the same precedence, the expression is executed from left to right. As illustrated in the next section, parentheses can be used to change the order of calculations.

1.3.2 Using MATLAB as a Calculator

The simplest way to use MATLAB is as a calculator. This is done in the Command Window by typing a mathematical expression and pressing the Enter key. MATLAB calculates the expression and responds by displaying ans = followed by the numerical result of the expression in the next line. This is demonstrated in Tutorial 1-1.

Tutorial 1-1: Using MATLAB as a calculator.

images

1.4 DISPLAY FORMATS

The user can control the format in which MATLAB displays output on the screen. In Tutorial 1-1, the output format is fixed-point with four decimal digits (called short), which is the default format for numerical values. The format can be changed with the format command. Once the format command is entered, all the output that follows is displayed in the specified format. Several of the available formats are listed and described in Table 1-2.

MATLAB has several other formats for displaying numbers. Details of these formats can be obtained by typing help format in the Command Window. The format in which numbers are displayed does not affect how MATLAB computes and saves numbers.

Table 1-2: Display formats

images

1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

In addition to basic arithmetic operations, expressions in MATLAB can include functions. MATLAB has a very large library of built-in functions. A function has a name and an argument in parentheses. For example, the function that calculates the square root of a number is sqrt(x). Its name is sqrt, and the argument is x. When the function is used, the argument can be a number, a variable that has been assigned a numerical value (explained in Section 1.6), or a computable expression that can be made up of numbers and/or variables. Functions can also be included in arguments, as well as in expressions. Tutorial 1-2 shows examples of using the function sqrt(x) when MATLAB is used as a calculator with scalars.

Tutorial 1-2: Using the sqrt built-in function.

images

Some commonly used elementary MATLAB mathematical built-in functions are given in Tables 1-3 through 1-5. A complete list of functions organized by category can be found in the Help Window.

Table 1-3: Elementary math functions

images

Table 1-4: Trigonometric math functions

images

The inverse trigonometric functions are asin(x), acos(x), atan(x), acot(x) for the angle in radians; and asind(x), acosd(x), atand(x), acotd(x) for the angle in degrees. The hyperbolic trigonometric functions are sinh(x), cosh(x), tanh(x), and coth(x). Table 1-4 uses pi, which is equal to π (see Section 1.6.3).

Table 1-5: Rounding functions

images

1.6 DEFINING SCALAR VARIABLES

A variable is a name made of a letter or a combination of several letters (and digits) that is assigned a numerical value. Once a variable is assigned a numerical value, it can be used in mathematical expressions, in functions, and in any MATLAB statements and commands. A variable is actually a name of a memory location. When a new variable is defined, MATLAB allocates an appropriate memory space where the variable’s assignment is stored. When the variable is used the stored data is used. If the variable is assigned a new value the content of the memory location is replaced. (In Chapter 1 we consider only variables that are assigned numerical values that are scalars. Assigning and addressing variables that are arrays is discussed in Chapter 2.)

1.6.1 The Assignment Operator

In MATLAB the = sign is called the assignment operator. The assignment operator assigns a value to a variable.

Variable_name = A numerical value, or a computable expression

  • The left-hand side of the assignment operator can include only one variable name. The right-hand side can be a number, or a computable expression that can include numbers and/or variables that were previously assigned numerical values. When the Enter key is pressed the numerical value of the right-hand side is assigned to the variable, and MATLAB displays the variable and its assigned value in the next two lines.

    The following shows how the assignment operator works.

    images

    The last statement (x = 3x − 12) illustrates the difference between the assignment operator and the equal sign. If in this statement the = sign meant equal, the value of x would be 6 (solving the equation for x).

    The use of previously defined variables to define a new variable is demonstrated next.

    images

  • If a semicolon is typed at the end of the command, then when the Enter key is pressed, MATLAB does not display the variable with its assigned value (the variable still exists and is stored in memory).
  • If a variable already exists, typing the variable’s name and pressing the Enter key will display the variable and its value in the next two lines.

    As an example, the last demonstration is repeated below using semicolons.

    images

  • Several assignments can be typed in the same line. The assignments must be separated with a comma (spaces can be added after the comma). When the Enter key is pressed, the assignments are executed from left to right and the variables and their assignments are displayed. A variable is not displayed if a semicolon is typed instead of a comma. For example, the assignments of the variables a, B, and C above can all be done in the same line.

    images

  • A variable that already exists can be reassigned a new value. For example:

    images

  • Once a variable is defined it can be used as an argument in functions. For example:

    images

1.6.2 Rules About Variable Names

A variable can be named according to the following rules:

  • Must begin with a letter.
  • Can be up to 63 characters long.
  • Can contain letters, digits, and the underscore character.
  • Cannot contain punctuation characters (e.g., period, comma, semicolon).
  • MATLAB is case-sensitive: it distinguishes between uppercase and lowercase letters. For example, AA, Aa, aA, and aa are the names of four different variables.
  • No spaces are allowed between characters (use the underscore where a space is desired).
  • Avoid using the name of a built-in function for a variable (i.e., avoid using cos, sin, exp, sqrt, etc.). Once a function name is used to for a variable name, the function cannot be used.

1.6.3 Predefined Variables and Keywords

There are 20 words, called keywords, that are reserved by MATLAB for various purposes and cannot be used as variable names. These words are:

break    case    catch  classdef   continue   else    elseif    end    for    function    global    if    otherwise    parfor   persistent   return   spmd    switch   try   while

When typed, these words appear in blue. An error message is displayed if the user tries to use a keyword as a variable name. (The keywords can be displayed by typing the command iskeyword.)

A number of frequently used variables are already defined when MATLAB is started. Some of the predefined variables are:

ans A variable that has the value of the last expression that was not assigned to a specific variable (see Tutorial 1-1). If the user does not assign the value of an expression to a variable, MATLAB automatically stores the result in ans.
pi The number π.
eps The smallest difference between two numbers. Equal to 2∧(–52), which is approximately 2.2204e–016.
inf Used for infinity.
i Defined as images, which is: 0 + 1.0000i.
j Same as i.
NaN Stands for Not-a-Number. Used when MATLAB cannot determine a valid numeric value. Example: 0/0.

The predefined variables can be redefined to have any other value. The variables pi, eps, and inf, are usually not redefined since they are frequently used in many applications. Other predefined variables, such as i and j, are sometime redefined (commonly in association with loops) when complex numbers are not involved in the application.

1.7 USEFUL COMMANDS FOR MANAGING VARIABLES

The following are commands that can be used to eliminate variables or to obtain information about variables that have been created. When these commands are typed in the Command Window and the Enter key is pressed, either they provide information, or they perform a task as specified below.

Command Outcome
clear Removes all variables from the memory.
clear x y z Removes only variables x, y, and z from the memory.
who Displays a list of the variables currently in the memory.
whos Displays a list of the variables currently in the memory and their sizes together with information about their bytes and class (see Section 4.1).

1.8 SCRIPT FILES

So far all the commands were typed in the Command Window and were executed when the Enter key was pressed. Although every MATLAB command can be executed in this way, using the Command Window to execute a series of commands—especially if they are related to each other (a program)—is not convenient and may be difficult or even impossible. The commands in the Command Window cannot be saved and executed again. In addition, the Command Window is not interactive. This means that every time the Enter key is pressed only the last command is executed, and everything executed before is unchanged. If a change or a correction is needed in a command that was previously executed and the result of this command is used in commands that follow, all the commands have to be entered and executed again.

A different (better) way of executing commands with MATLAB is first to create a file with a list of commands (program), save it, and then run (execute) the file. When the file runs, the commands it contains are executed in the order that they are listed. If needed, the commands in the file can be corrected or changed and the file can be saved and run again. Files that are used for this purpose are called script files.

IMPORTANT NOTE: This section covers only the minimum required in order to run simple programs. This will allow the student to use script files when practicing the material that is presented in this and the next two chapters (instead of typing repeatedly in the Command Window). Script files are considered again in Chapter 4, where many additional topics that are essential for understanding MATLAB and writing programs in script file are covered.

1.8.1 Notes About Script Files

  • A script file is a sequence of MATLAB commands, also called a program.
  • When a script file runs (is executed), MATLAB executes the commands in the order they are written, just as if they were typed in the Command Window.
  • When a script file has a command that generates an output (e.g., assignment of a value to a variable without a semicolon at the end), the output is displayed in the Command Window.
  • Using a script file is convenient because it can be edited (corrected or otherwise changed) and executed many times.
  • Script files can be typed and edited in any text editor and then pasted into the MATLAB editor.
  • Script files are also called M-files because the extension .m is used when they are saved.

1.8.2 Creating and Saving a Script File

In MATLAB script files are created and edited in the Editor/Debugger Window. This window is opened from the Command Window by clicking on the New Script icon in the Toolstrip, or by clicking New in the Toolstrip and then selecting Script from the menu that open. An open Editor/Debugger Window is shown in Figure 1-6.

images

Figure 1-6: The Editor/Debugger Window.

The Editor/Debugger Window has a Toolstrip at the top and three tabs EDITOR, PUBLISH, and VIEW above it. Clicking on the tabs changes the icons in the Toolstrip. Commonly, MATLAB is used with the HOME tab selected. The associated icons are used for executing various commands, as explained later in the Chapter. Once the window is open, the commands of the script file are typed line by line. MATLAB automatically numbers a new line every time the Enter key is pressed. The commands can also be typed in any text editor or word processor program and then copied and pasted in the Editor/Debugger Window. An example of a short program typed in the Editor/Debugger Window is shown in Figure 1-7. The first few lines in a script file are typically comments (which are not executed, since the first character in the line is %) that describe the program written in the script file.

images

Figure 1-7: A program typed in the Editor/Debugger Window.

Before a script file can be executed it has to be saved. This is done by clicking Save in the Toolstrip and selecting Save As... from the menu that opens. When saved, MATLAB adds the extension .m to the name. The rules for naming a script file follow the rules of naming a variable (must begin with a letter, can include digits and underscore, no spaces, and up to 63 characters long). The names of user-defined variables, predefined variables, and MATLAB commands or functions should not be used as names of script files.

1.8.3 Running (Executing) a Script File

A script file can be executed either directly from the Editor Window by clicking on the Run icon (see Figure 1-7) or by typing the file name in the Command Window and then pressing the Enter key. For a file to be executed, MATLAB needs to know where the file is saved. The file will be executed if the folder where the file is saved is the current folder of MATLAB or if the folder is listed in the search path, as explained next.

1.8.4 Current Folder

The current folder is shown in the “Current Folder” field in the desktop toolbar of the Command Window, as shown in Figure 1-8. If an attempt is made to execute a script file by clicking on the Run icon (in the Editor Window) when the current folder is not the folder where the script file is saved, then the prompt shown in Figure 1-9 opens. The user can then change the current folder to the folder where the script file is saved, or add it to the search path. Once two or more different current folders are used in a session, it is possible to switch from one to another in the Current Folder field in the Command Window. The current folder can also be changed in the Current Folder Window, shown in Figure 1-10, which can be opened from the Desktop menu. The Current Folder can be changed by choosing the drive and folder where the file is saved.

images

Figure 1-8: The Current folder field in the Command Window.

images

Figure 1-9: Changing the current directory.

images

Figure 1-10: The Current Folder Window.

An alternative simple way to change the current folder is to use the cd command in the Command Window. To change the current folder to a different drive, type cd, space, and then the name of the directory followed by a colon : and press the Enter key. For example, to change the current folder to drive E (e.g., the flash drive) type cd E:. If the script file is saved in a folder within a drive, the path to that folder has to be specified. This is done by typing the path as a string in the cd command. For example, cd('E:Chapter 1') sets the path to the folder Chapter 1 in drive F. The following example shows how the current folder is changed to be drive E. Then the script file from Figure 1-7, which was saved in drive E as ProgramExample.m, is executed by typing the name of the file and pressing the Enter key.

images

1.9 EXAMPLES OF MATLAB APPLICATIONS

Sample Problem 1-1:   Trigonometric identity

A trigonometric identity is given by:

images

Verify that the identity is correct by calculating each side of the equation, substituting images.

Solution

The problem is solved by typing the following commands in the Command Window.

images

Sample Problem 1-2:   Geometry and trigonometry

Four circles are placed as shown in the figure. At each point where two circles are in contact, they are tangent to each other. Determine the distance between the centers C2 and C4.

The radii of the circles are:
R1 = 16 mm, R2 = 6.5mm, R3 = 12mm, and R4 = 9.5mm.

images

Solution

The lines that connect the centers of the circles create four triangles. In two of the triangles, ΔC1C2C3 and ΔC1C3C4, the lengths of all the sides are known. This information is used to calculate the angles γ1 and γ2 in these triangles by using the law of cosines. For example, γ1 is calculated from:

images

(C2C3)2 = (C1C2)2 + (C1C3)2 − 2(C1C2)(C1C3)cosγ1 Next, the length of the side C2C4 is calculated by considering the triangle ΔC1C2C4. This is done, again, by using the law of cosines (the lengths C1C2 and C1C4 are known and the angle γ3 is the sum of the angles γ1 and γ2).

The problem is solved by writing the following program in a script file:

images

When the script file is executed, the following (the value of the variable C2C4) is displayed in the Command Window:

images

Sample Problem 1-3:   Heat transfer

An object with an initial temperature of T0 that is placed at time t = 0 inside a chamber that has a constant temperature of Ts will experience a temperature change according to the equation

T = Ts + (T0Ts)ekt

where T is the temperature of the object at time t, and k is a constant. A soda can at a temperature of 120° F (after being left in the car) is placed inside a refrigerator where the temperature is 38°F. Determine, to the nearest degree, the temperature of the can after three hours. Assume k = 0.45. First define all of the variables and then calculate the temperature using one MATLAB command.

Solution

The problem is solved by typing the following commands in the Command Window.

images

Sample Problem 1-4:   Compounded interest

The balance B of a savings account after t years when a principal P is invested at an annual interest rate r and the interest is compounded n times a year is given by:

images

If the interest is compounded yearly, the balance is given by:

images

Suppose $5,000 is invested for 17 years in one account for which the interest is compounded yearly. In addition, $5,000 is invested in a second account in which the interest is compounded monthly. In both accounts the interest rate is 8.5%. Use MATLAB to determine how long (in years and months) it would take for the balance in the second account to be the same as the balance of the first account after 17 years.

Solution

Follow these steps:

(a) Calculate B for $5,000 invested in a yearly compounded interest account after 17 years using Equation (2).

(b) Calculate t for the B calculated in part (a), from the monthly compounded interest formula, Equation (1).

(c) Determine the number of years and months that correspond to t.

The problem is solved by writing the following program in a script file:

images

When the script file is executed, the following (the values of the variables B, t, years, and months) is displayed in the Command Window:

images

1.10 PROBLEMS

The following problems can be solved by writing commands in the Command Window, or by writing a program in a script file and then executing the file.

1. Calculate:

images

images

2. Calculate:

images

images

3. Calculate:

images

images

4. Calculate:

images

images

5. Calculate:

images

images

6. Define the variable x as x = 6.7, then evaluate:

(a)  0.01x5 − 1.4x3 + 80x + 16.7

images

7. Define the variable t as t = 3.2, then evaluate:

images

(b)  14e−0.1tsin(2πt)

8. Define the variables x and y as x = 5.1 and y = 4.2, then evaluate:

images

images

9. Define the variables a, b, c, and d as:

images

10. A sphere has a radius of 24 cm. A rectangular prism has sides of a, a/2, and a/4.

(a) Determine a of a prism that has the same volume as the sphere.

(b) Determine a of a prism that has the same surface area as the sphere.

images

11. The arc length of a segment of a parabola ABC of an ellipse with semi-minor axes a and b is given approximately by:

images

(a) Determine LABC if a = 11 in. and b = 9 in.

images

12. Two trigonometric identities are given by:

(a) sin5x = 5sinx − 20sin3 x + 16 sin5x

(b) images

For each part, verify that the identity is correct by calculating the values of the left and right sides of the equation, substituting images.

13. Two trigonometric identities are given by:

images

(b)  cos 4x = 8(cos4x − cos2x) + 1

For each part, verify that the identity is correct by calculating the values of the left and right sides of the equation, substituting x = 24°.

14. Define two variables: alpha = π/6, and beta = 3π/8. Using these variables, show that the following trigonometric identity is correct by calculating the values of the left and right sides of the equation.

images

15. Given: images Use MATLAB to calculate the following definite integral: images.

16. In the triangle shown a = 5.3 in., γ = 42°, and b = 6 in. Define a, γ, and b as variables, and then:

(a) Calculate the length b by using the Law of Cosines.

(Law of Cosines: c2 = a2 + b2 − 2abcosγ)

(b) Calculate the angles β and γ (in degrees) using the Law of Cosines.

(c) Check that the sum of the angles is 180°.

images

17. In the triangle shown a = 5 in., b = 7 in., and γ = 25°. Define a, b, and γ as variables, and then:

(a) Calculate the length of c by substituting the variables in the Law of Cosines.
(Law of Cosines: c2 = a2 + b2 − 2abcosγ)

(b) Calculate the angles α and β (in degrees) using the Law of Sines.

(c) Verify the Law of Tangents by substituting the results from part (b) into the right and left sides of the equation.

images

images

18. In the ice cream cone shown, L = 4 in. and θ = 35°. The cone is filled with ice cream such that the portion above the cone is a hemisphere. Determine the volume of the ice cream.

images

19. For the triangle shown, a = 48 mm, b = 34 mm, and γ = 83°. Define a, b, and γ as variables, and then:

(a) Calculate c by substituting the variables in the Law of Cosines.

(Law of Cosines: c2 = a2 + b2 − 2abcosγ)

(b) Calculate the radius r of the circle circumscribing the triangle using the formula:

images

images

20. The parametric equations of a line in space are: x = x0 + at, y = y0 + bt, and z = z0 + ct. The distance d from a point A (xA, yA, z A) to the line can be calculated by:

images

where images

Determine the distance of the point A (2,−3, 1) from the line x = −4 + 0.6t, y = −2 + 0.5t, and Z = −3 + 0.7t. First define the variables x0, y0, z0, a, b, and c, then use the variable (and the coordinates of point A) to calculate the variable dA0, and finally calculate d.

images

21. The circumference of an ellipse can be approximated by:

images

Calculate the circumference of an ellipse with a = 16 in. and b = 11 in.

images

22. 315 people have to be transported using buses that have 37 seats. By typing one line (command) in the Command Window, calculate how many seats will remain empty if enough buses will be ordered to transport all the people. (Hint: use MATLAB built-in function ceil.)

23. 739 apples are to be packed and shipped such that 54 are placed in a box. By typing one line (command) in the Command Window, calculate how many apples will remain unpacked if only full boxes can be shipped. (Hint: use MATLAB built-in function fix.)

24. Assign the number 316,501.673 to a variable, and then calculate the following by typing one command:

(a) Round the number to the nearest hundredth.

(b) Round the number to the nearest thousand.

25. The voltage difference Vab between points a and b in the Wheatstone bridge circuit is:

images

images

Calculate the voltage difference when V = 14 volts, R1 = 120.6 ohms, R2 = 119.3 ohms, R3 = 121.2 ohms, and R4 = 118.8 ohms.

26. The resonant frequency f (in Hz) for the circuit shown is given by:

images

Calculate the resonant frequency when L = 0.15 henrys, R = 14 ohms, and C = 2.6 × 10−6 farads.

images

27. The number of combinations Cn, r of taking r objects out of n objects is given by:

images

(a) Determine how many combinations are possible in a lottery game for selecting 6 numbers that are drawn out of 49.

(b) Using the following formula, determine the probability of guessing two out of the six drawn numbers.

images

(Use the built-in function factorial.)

28. The formula for changing the base of a logarithm is:

images

(a) Use MATLAB’s function log(x) to calculate log4 0.085.

(b) Use MATLAB’s function log10(x) to calculate log6 1500.

29. The equivalent resistance, Req, of four resistors, R1, R2, R3, and R4, that are connected in parallel is given by:

images

Calculate Req if R1 = 120Ω, R2 = 220Ω, R3 = 75Ω, and R4 = 130Ω.

images

30. The voltage VC t seconds after closing the switch in the circuit shown is:

VC = V0(1 − et/(RC))

images

Given VC = 36 V, R = 2500 Ω, and C = 1600 μ F, calculate the current 8 seconds after the switch is closed.

31. Radioactive decay of carbon-14 is used for estimating the age of organic material. The decay is modeled with the exponential function f(t) = f(0)ekt, where t is time, f(0) is the amount of material at t = 0, f(t) is the amount of material at time t, and k is a constant. Carbon-14 has a half-life of approximately 5,730 years. A sample taken from the ancient footprints of Acahualinca in Nicaragua shows that 77.45% of the initial (t = 0) carbon-14 is present. Determine the estimated age of the footprint. Solve the problem by writing a program in a script file. The program first determines the constant k, then calculates t for f(t) = 0.7745f(0), and finally rounds the answer to the nearest year.

32. The greatest common divisor is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4. Use the MATLAB Help Window to find a MATLAB built-in function that determines the greatest common divisor of two numbers. Then use the function to show that the greatest common divisor of:

(a) 91 and 147 is 7.

(b) 555 and 962 is 37.

33. The Moment Magnitude Scale (MMS), denoted MW, which measures the total energy released by an earthquake, is given by:

images

where M0 is the magnitude of the seismic moment in dyne-cm (measure of the energy released during an earthquake). Determine how many times more energy was released from the largest earthquake in the world, in Chile (MW = 9.5), 1960, than the earthquake in Rat Island, Alaska (MW = 8.7), in 1965.

34. According to special relativity, a rod of length L moving at velocity v will shorten by an amount δ, given by:

images

where c is the speed of light (about 300 × 106 m/s). Calculate how much a rod 2 m long will contract when traveling at 5,000 m/s.

35. The value B of a principal P that is deposited in a saving account with a fixed annual interest rate r after n years can be calculated by the formula:

images

where m is the number of times that the interest is compounded annually. Consider a $80,000 deposit for 5 years. Determine how much more money will be earned if the interest is compounded daily instead of yearly.

36. Newton’s law of cooling gives the temperature T(t) of an object at time t in terms of T0, its temperature at t = 0, and Ts, the temperature of the surroundings.

T(t) = Ts + (T0Ts)ekt

A police officer arrives at a crime scene in a hotel room at 9:18 PM, where he finds a dead body. He immediately measures the body’s temperature and find it to be 79.5°F. Exactly one hour later he measures the temperature again, and find it to be 78.0°F. Determine the time of death, assuming that victim body temperature was normal (98.6° F) prior to death, and that the room temperature was constant at 69° F.

37. The stress intensity factor K predicts the stress state (stress intensity) near a crack tip. For a plate with a crack and loading shown in the figure, K is given by:

images

Determine K for the case where σ = 12000psi, h = 5in., and b = 4in., and a = 1.5 in.

images

38. The spread of a computer virus through a computer network can be modeled by:

N(t) = 20e0.15t

where N(t) is the number of computers infected and t time in minutes.

(a) Determine how long it takes for the number of infected computers to double.

(b) Determine how long it takes for 1,000,000 computers to be infected.

39. Use the Help Window to find a display format that displays the output as a ratio of integers. For example, the number 3.125 will be displayed as 25/8. Change the display to this format and execute the following operations:

(a) 5/8 + 16/6

(b) 1/3 − 11/13 + 2.72

40. Stirling’s approximation for large factorials is given by:

images

Use the formula for calculating 20!. Compare the result with the true value obtained with MATLAB’s built-in function factorial by calculating the error (Error = (TrueValApproxVal)/ TrueVal).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.249.92