Essential MATLAB for Engineers and Scientists, Third Edition

The objective of this chapter is to introduce some of the fundamentals of MATLAB programming, including:
variables, operators and expressions;
arrays (including vectors and matrices);
basic input and output;
repetition ( for);
decisions ( if).
The tools introduced are sufficient to begin solving numerous scientific and engineering problems you may encounter in your course work and in your profession. Thus, the last part of this chapter and the next chapter describe an approach to designing reasonably good programs to initiate the building of tools in your own toolbox.
Variables are fundamental to programming. In a sense, the art of programming is
getting the right values in the right variables at the right time.
A variable name (like the variable balance that we used in the previous chapter) must comply with the following two rules:
It may consist only of the letters a z, the digits 0 9 and the underscore ( _).
It must start with a letter.
A variable name may be as long as you like, but MATLAB only remembers the first 31 characters.
| Examples of valid variable names: | r2d2 pay_day |
| Examples of invalid names (why?): | pay-day 2a name$ _2a |
A variable is created simply by assigning a value to it at the command line or in a program, e.g.
a = 98
If you attempt to refer to a non-existent variable you will get the error message
???