A variable is a named storage location that can be set to a particular value which can be used in subsequent computations. For example, we store a value of 5 in the variable
with the statement
This value remains in
until we store a different value (for example, using the command
) or we clear
using the command
Once a variable is set to a particular value, we can get this value by using the variable name in an expression (e.g.
Suppose we wish to compute the circumference of a circle of diameter 5 units using the formula c=πd. We could first set the variable
to a value of 5 using the following input to the m-file environment. In the following,
is the prompt displayed by the m-file environment:
>> d = 5
Then we could compute the circumference and assign its value to the variable
>> c = pi*d
In this command, the product of the value of
(which is known because we earlier set it to 5) and the value of pi (which is a pre-defined variable) is computed and the value of the product is stored in the variable
Variable names must begin with an upper- or lower-case letter. They may contain letters, digits, and underscores; they may not contain spaces or punctuation characters. Variable names are case sensitive, so
are different variables.
Which of the following are valid variable names?
ecky ecky ecky ecky ptang zoo boing
There are several predefined variables. The most commonly used include
Once assigned, variable names remain until they are reassigned or eliminated by the
Variables can contain several types of numerical values. These types include the following:
Figure 1 shows a Sharp GP2D12 infrared distance sensor http://www.acroname.com/robotics/info/articles/sharp/sharp.html and a BasicX-24 microprocessor http://www.basicx.com/.
The infrared distance sensor and microprocessor.
The distance sensor uses a beam of infrared light to measure the distance from the sensor to an object; the sensor provides an output voltage that has a fairly complicated relationship to this distance. The BasicX processor converts the voltage from the sensor into a number between zero and one. Let us denote this number as x, and the distance (measured in inches) between the sensor and object as d. The relationship between x and d is
Compute the value of d for the following values of x:
The terminal velocity reached by a sky diver depends on many factors, including their weight, their body position as they fall, and the density of the air through which they fall. The terminal velocity is given by http://en.wikipedia.org/wiki/Terminal_velocity
m is the sky diver's mass
g is Earth's gravitational constant
r is the atmospheric density
A is the sky diver's effective area
Cd is the sky diver's coefficient of drag
Compute the terminal velocity of the sky diver for each of the following values of m:
Use the following values for the other variables: