"MATLAB The Language of Technical Computing" is a document for an introductory course in MATLAB and technical computing. This document is not a comprehensive introduction or a reference manual. Instead, it focuses on the specific features of MATLAB that are useful for engineering classes. The lab sessions are used with one main goal: to allow students to become familiar with computer software (e.g., MATLAB) to solve application problems. We assume that the students have no prior experience with MATLAB.
The availability of technical computing environments such as MATLAB is now reshaping the role and applications of computer laboratory projects to involve students in more intense problem-solving experiences. This availability also provides an opportunity to easily conduct numerical experiments and to tackle realistic and more complicated problems. Originally, the manual is divided into computer laboratory sessions.
The tutorials are independent of the rest of the document. The primary objective is to help you learn quickly the first steps. The emphasis here is “learning by doing”. Therefore, the best way to learn is by trying it yourself. Working through the examples will give you a feel for the way MATLAB operates.
In this introduction, we will describe how MATLAB handles simple numerical expressions and mathematical formulas. The following tutorial lessons are designed to get you started quickly in MATLAB. The lessons are intended to make you familiar with the basics of MATLAB. We urge you to complete the exercises given at the end of each lesson.
Chapter 1
Starting with MATLAB
This chapter begins by describing the characteristics and purposes of the different windows in MATLAB. Next, the Command Window is introduced in detail and is the only one that is used in the rest of the Chapter. Chapter 1 shows how to use MATLAB for arithmetic operations with scalars, similar to the way that the 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 variables in arithmetic calculations.
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 windows that opens, shown in figure 1-1, contains three smaller windows which are command window, the workspace window, and the command history window. This is the default view of MATLAB. A list of the various windows and their purpose is given in Table 1-1. The start button on the lower left side is a new addition in MATLAB 6.5. it can be used to access MATLAB tools and features.
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 page. More detailed descriptions are included in the chapters where they are used.
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, and this can be done by either closing all the other windows (click on the x at the top right hand side of the window you want to close), or by choosing the Command Window in the view menu in the tool bar. How to Work in the Command Window is described in detail in section 1.2.
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:
Abbildung in dieser Leseprobe nicht enthalten
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 the x would be 6 (solving the equation for x).
The use of previously defined variables to define a new variable is demonstrated next.
Abbildung in dieser Leseprobe nicht enthalten
- 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 exist 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.
For example, the last demonstration is repeated below using semicolons:
Abbildung in dieser Leseprobe nicht enthalten
- Several assignments can be typed in the same line. The assignment 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 variable a, B, and C, above can all be done in the same line.
Abbildung in dieser Leseprobe nicht enthalten
- A variable that already exists can be reassigned a new value. For example:
Abbildung in dieser Leseprobe nicht enthalten
- Once a variable is defined it can be used as an argument in functions. For example:
Abbildung in dieser Leseprobe nicht enthalten
- Variable names:
- Can be up to 63 (in MATLAB 6.5) characters along (31 characters in MATLAB 6.0)
- Can contain letters, digits, and the underscore character.
- Must begin with a letter.
- 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.
- Avoid using the names of a built-in function for a variable (i.e. avoid using: cos, sin, exp, sqrt, etc.). Once a function name is used to define a variable, the function cannot be used.
1.6.3 Predefined Variables
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. Equals to 2^(-52), which is approximately 2.2204e-016.
Inf used for infinity.
i defined as , which is: 0 1.+1.0000i.
j same as i.
NaN stands for a Not-a-Number. Used when MATLAB cannot determine a valid numeric value. For example 0/0.
Predefined variables can be redefined to have any another value. The variables pi, eps, and inf, are usually not to redefined since they are Frequently used in many applications. Other predefined variables like I and j are sometimes 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 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, they either provide information, or they perform a task as a listed below.
Clear Removes all variables from the memory.
Clear x y z Removes only variables x, y, 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 size together with information about their bytes and class.
1.8 EXAMPLES OF MATLAB APPLICATIONS
Sample Problem 1-1: Trigonometric Identity
A trigonometric identity is given by:
Cos[2] =
Verify that the identity is correct by calculating each side of the equation,
substituting x =
Solution:
Abbildung in dieser Leseprobe nicht enthalten
Sample Problem 1-2: Geometry and Trigonometry
Four circles are placed, as shown in the figure. At each point that 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 = 16mm, r2 = 6.5mm, R3 = 12mm, and R4 = 9.5mm.
Solution
The lines that connect the centers of the circles create four triangles. In two of the triangles, ΔC1C2C3 and Δ C1C2C3 the lengths of all the sides are known. This information is used to calculate the angles Y1 and Y2 in these triangles by using the law of cosine. For example, Y1 is calculated from:
Abbildung in dieser Leseprobe nicht enthalten
Next, the length of the side C2C4 is calculated by considering the triangle Δ CC4. This is done, again, by using the law of cosines (the lengths C1C2 and C1 C4 are known and the angle Y3 is the sum of the angles Y1Y2).
Abbildung in dieser Leseprobe nicht enthalten
The clc Command:
The clc command (type clc and press Enter) clears the Command Window. After working in the Command Window for a while, the display may be 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.
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, number 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:
Operation Symbol Example
Addition + 5 + 3
Subtraction - 5 – 3
Multiplication * 5 * 3
Right Division / 5 / 3
Left Division \ 5 \ 3 = 3 / 5
Exponentiation ^ 5 ^ 3(means 5[3] = 125)
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 inverse of the arrays, which are discussed in chapter 3.
1.3.1 Order of Procedures
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 Parenthesis. For nested parenthesis, 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, parenthesis 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 = and 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:
Abbildung in dieser Leseprobe nicht enthalten
1.4 DISPLAY FORMATS
The user can control the format in which MATLAB displays utput on the screen. In tutorial 1-1, the output format is fixed-point with 4 decimal digits (called short), which is the default format for numerical values. The format can 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 MARLAB computes and saves numbers.
Table 1-2: Displays formats
Abbildung in dieser Leseprobe nicht enthalten
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS
In addition to basic arithmetic operations, expressions in, the MATLAB can include functions. MATLAB has a very large library of built-in functions. A function has a name and an argument in parenthesis. For example, the function that calculates the square root of a number is sqrt (x). its name is sqrt, and the argu ment 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.
Abbildung in dieser Leseprobe nicht enthalten
Lists of some commonly used elementary MATLAB mathematical built-in functions are given in Table 1-3 through 1-5. A complete list of functions organized by name of category can bne found in the help window.
Table 1-3: Elementary Math Functions
Abbildung in dieser Leseprobe nicht enthalten
Table 1-4: Trigonometric math Function
Abbildung in dieser Leseprobe nicht enthalten
The inverse trigonometric functions are a sin (x), a cos (x), a tan (x), and a cot (x). the hyperbolic trigonometric functios are sinh (x), cosh (x), tanh (x), and coth (x). the previous table uses pi which is equal to ∏ (see section 1.6.3
[...]
- Quote paper
- Alan Nebrida (Author), 2022, Matlab. The Language of Technical Computing, Munich, GRIN Verlag, https://www.grin.com/document/1284770
-
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X. -
Upload your own papers! Earn money and win an iPhone X.