Assignment

From ComputingForScientists

Jump to: navigation, search
Assignment

Contents

  1. Assignment
    1. Objectives
    2. Motivation
    3. Introduction
    4. Connection to Bit Patterns
    5. Assignment versus Equal
    6. Whitespace Around Equal Sign
    7. The End-of-line Semicolon
    8. Assignment Example
    9. Assignment Example
    10. Variable Name Rules
    11. Syntax Rules Example
    12. The Comment Character
    13. The clear Command
  2. Problems
    1. Syntax
    2. Syntax
    3. Syntax
    4. Syntax
    5. Syntax
    6. Syntax
    7. Syntax
    8. Syntax
    9. Syntax
    10. Syntax
    11. Syntax
    12. Syntax
    13. The clear command
  3. Summary Video

1. Assignment

1.1. Objectives

  • To introduce the notion of assignment and how it corresponds to a bit pattern.
  • To distinguish between assignment and equality.
  • To define a scalar in programming.
  • To introduce the clear command.
  • To introduce the meaning of the end-of-line semicolon.
  • To introduce the comment character.
  • To understand how to form valid variable names.

1.2. Motivation

  • One of the most frequent operations in programming is assignment.

1.3. Introduction

In programming, assignment is associating a value to a variable. For example, in a program, you may assign the number 1.999999999999 to the variable named x. Later in the program, you may assign the number 3.14159 to the variable named x. At any point in the program, you may ask "what is the last value that was assigned to x?" or say "take the current value assigned to x and double it".

A scalar variable is a variable associated with a single number, for example, 1.999999999999. In Arrays, you will see how a variable can be associated with a collection of numbers. In this book, we usually use lower-case letters for the names of scalar variables and upper-case letters for the names of arrays and matrices, however, that naming system is not required.

In a spreadsheet, we enter a number 1.0 into cell a1. We have assigned the value of 1.0 to a1. In MATLAB, the equal sign is used to indicate assignment. The statement a1 = 1.0 assigns the value of 1.0 to the variable named a1. When you assign a decimal number to a variable, MATLAB writes the bit pattern associated with the decimal number into memory.

1.4. Connection to Bit Patterns

If you entered

a1 = 1
b1 = 2
a1 + b1

the last line is telling MATLAB to do the following binary addition

   0 0 0 1
   0 0 1 0
 + ________

(The above is an approximation of what actually happens behind-the-scenes. In reality, MATLAB writes a bit pattern with 64 values to represent the number 1. Also, the bit pattern for the decimal number 1 MATLAB uses is not sixty-three zeros and then a one. Internally, it uses a different convention than what we have been using for relating bit patterns to decimal numbers.)

1.5. Assignment versus Equal

In a MATLAB program, the equal sign does not mean equal in the sense that the left side (of the equal sign) is equal to the right side (of the equal sign). In MATLAB, the equal sign is used for assignment. Some languages attempt to avoid this confusion by using a different symbol for assignment; for example, instead of A1 = 1.0, the expression A1 := 1.0 or A1 <- 1.0 is used.

The reason these languages use a different symbol for assignment is to avoid confusion. If you were thinking in the mathematical sense and saw this in a computer program

a1 = a1 + 1.0

you might re-write this equation as

a1 - a1 = 1.0

and then

0 = 1.0

which does not make sense. If a1 had previously been assigned a value, then

a1 = a1 + 1.0

is a valid programming statement. It reads "Assign to a1 the value of whatever a1 is now plus 1.0." Said another way, the above statement reassigns the value of a1.

1.6. Whitespace Around Equal Sign

Whitespace is optional around the equal character. The following three statements are equivalent.

a1=1
a1 = 1
a1      =1

Some programmers use the convention of connecting the equal sign to the variable, as in

a= 1

to emphasize that a is being assigned to the value of 1.

1.7. The End-of-line Semicolon

If a line does not have a semi-colon at the end, MATLAB assumes that you want a value of a variable to be displayed.

If you enter

a1 = 20.0;

MATLAB assigns the value of 20.0 to a variable named a1 but does not display the value of a1.

If you had entered

a1 = 20.0

MATLAB will assign the value of 20.0 to a variable named a1 and then display the value of a1:

a1 = 20.0

You can also display the value of a variable after it has been assigned. For example, you could enter

a1 = 20;
b1 = 20;
a1

and MATLAB will display

 a1 =
     20

If you had omitted the semi-colon on the first line, you would see

 a1 =
     20
 a1 =
     20

1.8. Assignment Example

Do the following in MATLAB syntax:

  1. Assign a variable named a1 the value 20.0
  2. Assign a variable named b1 the value of a1 plus 13.0
  3. Re-assign the variable named b1 to the current value of b1 plus 12.0

What is b1 after the instructions are executed?

Could you do the above in Excel?

1.9. Assignment Example

Write a MATLAB program that does the following:

  1. Assigns the decimal number 1.0 to a variable named a1
  2. Assigns the value of a1 plus 2.0 to a variable named a2
  3. Assigns the value of a2 plus 4.0 to a variable named a3
  4. Assigns the value of a3 plus 8.0 to a variable named a4
  5. Displays the value of a4

1.10. Variable Name Rules

Thus far, we have assigned values to variables named a1, b1, etc. following the way cells are named in a spreadsheet. MATLAB allows other variable names besides a capital letter followed by an integer. However, in the same way that the hospital won't let you name your baby "l33t", there are rules for variable names.

According to [1],

A valid variable name starts with a letter, followed by letters, digits, or underscores. MATLAB® is case sensitive, so A and a are not the same variable. The maximum length of a variable name is the value that the namelengthmax command returns.

In addition, use descriptive variable names to make the program easier to read. For example, if a variable represents a population, use p. If a variable represents a bank account balance, use b. Some variable names are not recommended, such as if, pi, for, end, matrix, array. To determine if a variable name is "not recommended", type which NAME (NAME would be the variable name you choose) on the command line. If MATLAB says 'NAME' not found, then NAME is a safe variable name to use.

1.11. Syntax Rules Example

Which of the following are valid names? Which are not recommended?

  • 3BBBB
  • testing123
  • a1a
  • A1*b1
  • while
  • vector

1.12. The Comment Character

A comment character instructs the interpreter to ignore everything (the comments) that follows it. Most programming languages have a comment character (some use multiple characters to represent a comment, e.g., //). MATLAB's comment character is the percent sign: %. Comments are added to make programs easier for a human to understand.

For example, the program

a1 = 20.0;
b1 = a1 + 13.0;
b1 = b1 + 12.0;
b1

could have been written as

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start of program to demonstrate assignment
a1 = 20.0;      % Assign the value of 20 to a variable named a1
b1 = a1 + 13.0; % Assign the value of a1 plus 13.0 to a variable named b1
b1 = a1 + 12.0; % Assign the previous value of b1 plus 12.0 to the variable named b1
b1              % Display the value of b1
% End of program to demonstrate assignment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

1.13. The clear Command

When you start MATLAB, all of the variables you have previously assigned values to no longer exist. Said another way, the values and the associated names are cleared from memory. Sometimes you will want to remove a variable (most often an array) from memory without exiting and restarting MATLAB. To see how the clear command works, note that if you enter

a1 = 10;
clear a1;
b = a1 + 1.0

you will see an error message. On the second line you have instructed MATLAB to remove the variable a1 from memory. On the last line, you have asked MATLAB to add 1.0 to the value of a variable that does not have a value associated with it. To clear all variables, type clear or clear all.

When writing a script, clear or clear all is usually the first line so all variables previously assigned are cleared from MATLAB.

2. Problems

2.1. Syntax

What will happen when the following commands are entered (take into account semi-colons)?

A = 1;
B = 2
C = A+B

2.2. Syntax

What will happen when the following commands are entered (take into account semi-colons)?

A = 1;
B = A+1;
B = B*2

2.3. Syntax

What will happen when the following commands are entered (take into account semi-colons)?

a = 2
b = 4
c = a+b

2.4. Syntax

What will happen when the following commands are entered (take into account semi-colons)? Each of the following has one error in it that will prevent the code from running if typed on the command line. What is the error?

clear;
a = b;

2.5. Syntax

What will happen when the following commands are entered (take into account semi-colons)?

clear;
b = 1;
c = a+b;

2.6. Syntax

What will happen when the following commands are entered (take into account semi-colons)?

clear;
A1 = 10;
1A = A1;

2.7. Syntax

What will happen when the following commands are entered (take into account semi-colons)?

clear;
A = 1;
B = A+2;
C = B+2;
D = D+2;

2.8. Syntax

What will happen when the following commands are entered (take into account semi-colons)?

clear;
A = 1;
B = 2;
A+B = 3;

2.9. Syntax

What will be displayed when each of the following commands are executed on the command line?

a = 1;

2.10. Syntax

What will be displayed when each of the following commands are executed on the command line?

a = 1;
a

2.11. Syntax

What will be displayed when each of the following commands are executed on the command line?

a = 1
a

2.12. Syntax

Which of the following are valid names? Which are not recommended?

  • plot
  • matrix
  • _twelve
  • seven
  • My Variable
  • MyVariable
  • A1^3
  • case

2.13. The clear command

A friend is working on the command line and calls you on the phone for help. When they enter

sin(pi/2)

on the command line, they see

 ans =

      -1

You start a MATLAB session on your computer and enter

sin(pi/2)

and see

 ans =

      1

You also try this on a few computers in the computer lab and get the same result.

Explain a possible reason why your friend is getting a different answer.

Save your answer in a file named HW1.txt. Create this file (and all .txt files for this class unless otherwise specified) using a text editor such as Notepad or Notepad++ on Windows, TextWrangler on OS X, or GEdit on Linux.

3. Summary Video

Personal tools