Math 130 exercise quiz, Assignments of Mathematics

Math 130 math 125 assignment quiz

Typology: Assignments

2022/2023

Uploaded on 07/23/2023

shouraie
shouraie 🇵🇭

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
If none of these predefined numeric display formats is right for you, you can
control individual lines of output with the fprintf function, described in a later
chapter.
2.4 SaVinG YOUr WOrk
Working in the command window is similar to performing calculations on your sci-
entific calculator. When you turn off the calculator or when you exit the program,
your work is gone. It is possible to save the values of the variables you defined in the
command window and that are listed in the workspace window, but while doing so
is useful, it is more likely that you will want to save the list of commands that gener-
ated your results. The diary command allows you to do just that. Also we will show
you how to save and retrieve variables (the results of the assignments you made and
thecalculationsyouperformed)toMAT-filesortoDAT-files.Finallywe’llintroduce
scriptM-files,whicharecreatedintheeditwindow.ScriptM-filesallowyoutosave
a list of commands and to execute them later. You will find script M-files especially
useful for solving homework problems. When you create a program in MATLAB®,
it is stored in an M-file.
2.4.1 diary
The diary function allows you to record a MATLAB® command window session in a
file and retrieve it for later review. Both the MATLAB® commands and the results
are stored— including all your mistakes. To activate the diary function simply type
diary
or
diary on
at the command prompt. To end a recording session type diary again, or diary
off. A file named diary should appear in the current folder. You can retrieve the
file by double-clicking on the file name in the current folder window. An editor
MatlaB® Command display example
format bank 2 decimal digits 3.14
only real values are displayed
format short eng 4 decimal digits 3.1416e+000
engineering notation 123.4568e+000
format long eng 14 decimal digits 3.141592653589793e+000
engineering notation 123.456789000000e+000
format +
+, -,
blank +
format rat fractional form 355/113
format short g MATLAB® selects the best format 3.1416
123.46
format long g MATLAB® selects the best format 3.14159265358979
123.456789
table 2.2 (Continued)
M02_MOOR0538_04_SE_C02.INDD 58 3/1/14 5:49 PM
pf3
pf4
pf5

Partial preview of the text

Download Math 130 exercise quiz and more Assignments Mathematics in PDF only on Docsity!

If none of these predefined numeric display formats is right for you, you can control individual lines of output with the fprintf function, described in a later chapter.

2.4 SaVinG YOUr WOrk

Working in the command window is similar to performing calculations on your sci- entific calculator. When you turn off the calculator or when you exit the program, your work is gone. It is possible to save the values of the variables you defined in the command window and that are listed in the workspace window, but while doing so is useful, it is more likely that you will want to save the list of commands that gener- ated your results. The diary command allows you to do just that. Also we will show you how to save and retrieve variables (the results of the assignments you made and the calculations you performed) to MAT-files or to DAT-files. Finally we’ll introduce script M-files, which are created in the edit window. Script M-files allow you to save a list of commands and to execute them later. You will find script M-files especially useful for solving homework problems. When you create a program in MATLAB ®, it is stored in an M-file.

2.4.1 diary

The diary function allows you to record a MATLAB ®^ command window session in a file and retrieve it for later review. Both the MATLAB®^ commands and the results are stored— including all your mistakes. To activate the diary function simply type

diary

or

diary on

at the command prompt. To end a recording session type diary again, or diary off. A file named diary should appear in the current folder. You can retrieve the file by double-clicking on the file name in the current folder window. An editor

MatlaB ®^ Command display example

format bank 2 decimal digits 3. only real values are displayed format short eng 4 decimal digits 3.1416 e + engineering notation 123.4568e+ format long eng 14 decimal digits 3.141592653589793 e + engineering notation 123.456789000000e+ format + + , - , blank + format rat fractional form 355/ format short g MATLAB®^ selects the best format **3.

format long g** MATLAB®^ selects the best format 3. 123.

table 2.2 (Continued)

window will open with the recorded commands and results. You can also open the file in any text editor, such as Notepad. Subsequent sessions are added to the end of the file. If you prefer to store the diary session in a different file, specify the file- name

diary

or

diary('filename')

In this text, we’ll use angle brackets (< >) to indicate user-defined names. Thus, to save a diary session in a file named My_diary_file type

diary My_diary_file

or

diary('My_diary_file')

2.4.2 Saving Variables

To preserve the variables you created in the command window (check the work- space window on the left-hand side of the MATLAB®^ screen for the list of variables), you must save the contents of the workspace window to a file. The default format is a binary file called a MAT-file. To save the workspace (remember, this is just the variables, not the list of commands in the command window) to a file, type

save <file_name>

at the prompt. Recall that, although save is a MATLAB®^ command, file_name is a user-defined file name. It can be any name you choose, as long as it conforms to the naming conventions for variables in MATLAB ®^. Actually, you don’t even need to supply a file name. If you don’t, MATLAB®^ names the file matlab.mat. You could also choose

Save Workspace

from the toolstrip, which will then prompt you to enter a file name for your data. To restore a workspace, type

load <file_name> Again, load is a MATLAB ®^ command, but file_name is the user-defined file name. If you just type load, MATLAB®^ will look for the default matlab.mat file. The file you save will be stored in the current folder. For example, type clear, clc This command will clear both the workspace and the command window. Verify that the workspace is empty by checking the workspace window or by typing

whos Now define several variables—for example, a = 5; b = [1,2,3]; c = [1, 2; 3,4]; Check the workspace window once again to confirm that the variables have been stored. Now, save the workspace to a file called my_example_file:

save my_example_file

You can retrieve the data from the current folder with the load command: load <file_name> For example, to create the matrix z and save it to the file data_2.dat in eight- digit text format, use the following commands: z = [5 3 5; 6 2 3]; save data_2.dat z –ascii Together, these commands cause each row of the matrix z to be written to a separate line in the data file. You can view the data_2.dat file by double-clicking the file name in the current folder window (see Figure 2.12). If your file contains a single array, the easiest way to retrieve data from an ASCII .dat file is to enter the load command followed by the file name. This causes the information to be read into a matrix with the same name as the data file. This won’t work if your file con- tains multiple variables. However, it is quite easy to use MATLAB ®^ ’s interactive Import Wizard to load the data. When you double-click a data file name in the cur- rent folder to view the contents of the file, the Import Wizard will automatically launch. Just follow the directions to load the data into the workspace, with the same name as the data file. You can use this same technique to import data from other programs, including Excel spreadsheets, or you can select import data from the toolstrip.

2.4.3 Script M-Files

Using the command window for calculations is an easy and powerful tool. However, once you close the MATLAB ®^ program, all of your calculations are gone. Fortunately, MATLAB ®^ contains a powerful programming language. As a programmer, you can create and save code in files called M-files. These files can be reused anytime you wish to repeat your calculations. An M-file is an ASCII text file similar to a C or FORTRAN source-code file. It can be created and edited with the MATLAB®^ M-file editor/debugger (the edit window discussed in Section 2.2.7), or you can use another text editor of your choice. To open the editing window, select New Script from the MATLAB ®^ toolstrip. The MATLAB ®^ edit window is shown in Figure 2.13. Many programmers prefer to dock the editing window onto the MATLAB ®

Figure 2. Double-clicking the file name in the command directory launches the import Wizard.

desktop, using the show window actions drop-down menu from the upper right- hand corner of the window (select dock editor). This allows you to see both the contents of the M-file and the results displayed when the program is executed. The results from an M-file program are displayed in the command window. If you choose a different text editor, make sure that the files you save are ASCII files. Notepad is an example of a text editor that defaults to an ASCII file structure. Other word processors, such as WordPerfect or Word, will require you to specify the ASCII structure when you save the file. These programs default to proprietary file structures that are not ASCII compliant and may yield some unexpected results if you try to use code written in them without specifying that the files be saved in ASCII format. When you save an M-file, it is stored in the current folder. You’ll need to name your file with a valid MATLAB®^ variable name—that is, a name starting with a letter and containing only letters, numbers, and the underscore 1 _ (^2). Spaces are not allowed (see Section 2.3.1). There are two types of M-files, called scripts and functions. A script M-file is simply a list of MATLAB®^ statements that are saved in a file with a .m file extension. The script can use any variables that have been defined in the workspace, and any variables cre- ated in the script are added to the workspace when the script executes. You can exe- cute a script created in the MATLAB®^ edit window by selecting the Save and Run icon from the menu bar, as shown in Figure 2.13. (The Save and Run icon changed appear- ance with MATLAB®^ 7.5. Previous versions of the program used an icon similar to an exclamation point.) You can also execute a script by typing a file name or by using the run command from the command window as shown in Table 2.3. No matter how you do it, you can only run an M-file if it is in the current folder. You can find out what M-files and MAT files are in the current folder by typing what into the command window. You can also browse through the current folder by look- ing in the current folder window. Using script M-files allows you to work on a project and to save the list of com- mands for future use. Because you will be using these files in the future, it is a good idea to sprinkle them liberally with comments. The comment operator in MATLAB ® is the percentage sign, as in % This is a comment MATLAB ®^ will not execute any code on a commented line.

The Editor Action Drop Down Menu

The Save and Run Icon

Figure 2. The MATLAB®^ edit window, also called the editor/ debugger.

M-FIle A list of MATLAB ® commands stored in a separate file

Key Idea The two types of M-files are scripts and functions