Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 2

Task 2.1 Detail: Create variables, make calculations and display results.

Summary of new tools and commands.

Task: Create a variable holding the radius of a circle by assigning it a convenient value. Calculate the circumference and area of the circle. Display the results of these calculations.

Matlab creates a variable simply be assigning a value to a variable. A=10 both creates the variable A and assigns it the value of 10. If there was already a variable named A, then it will be erased and replaced by the current variable and value.

By default, all variables are real numbers. These numbers are represented by about 15 significant digits, so the arithmetic operations on them are relatively precise.

There are a number of other variable types which we will see as the class progresses. In many cases, the value assigned to a variable determines its type. In other cases, the type can be specified by a command creating the variable (details will be provided as the issues arrise).

When creating variables, use descriptive names. There is no real limit on the length of names, so don't feel constrained. For this problem, you might use variable names such as "area", "radius", or "circum".

Variable names must start with a letter, but can contain numbers. The names are case sensitive so upper and lower case letters are different. Names can also contain underscore (_). They should not contain other non-number and non-letter characters such as periods (.), dollar signs ($), percent signs (%), spaces ( ), and so forth. Some of these characters have special meaning in MATLAB.

Variable names can be re-used. However, whatever values were contained by the old variable are replaced by the new value. The previous values cannot be recovered.

The value of a variable can be seen in the Workspace panel on the MATLAB window. However, the simplest way to see the value of a variable is to type its name without the following ;. The variable name followed by its value is printed in the command window.

A few variables are pre-defined in MATLAB:   pi,   e (the base of the natural logarithm), i ( = sqrt(-1)), and j ( = sqrt(-1)) are some examples.

You may define your own variable called pi; MATLAB will not declare an error or give a warning. The original value of pi is now gone. However, the command clear pi will remove your variable and restore the original value.

It is sometimes necessary to specify the order of arithmetic operators which is done with parenthesis. For example, C = 2*(A+B) will add A and B first and then double the answer.

Script to complete this task:

%   task21.m
%    no ; at the end of the line, so results are printed.
  radius=5
  circum=2*pi*radius
  area=pi*radius^2

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 2


email: J. Klinck