Essential MATLAB for Engineers and Scientists, Third Edition

Chapter 2: MATLAB Fundamentals

Overview

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.

2.1 Variables and the Workspace

2.1.1 Variables

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:

  1. It may consist only of the letters a z, the digits 0 9 and the underscore ( _).

  2. 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

???

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Robot Software
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.