You are on page 1of 12

IM 512 3.

0 (I) Mathematical Software

Dr. TGI Fernando

September 16, 2011

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 1 / 12
The Basics A minimum Matlab session

A minimum Matlab session

Arithmetic operators

+ addition
- substraction
* multiplication
/ division
ˆ exponentiation

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 2 / 12
The Basics A minimum Matlab session

Trigonometric Functions

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 3 / 12
The Basics A minimum Matlab session

Hyperbolic Functions

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 4 / 12
The Basics A minimum Matlab session

Some simple calculations

Enter 2+2 and hit the return/enter key. Note that the result of an
unassigned expression is saved in the default variable ans.
Assign an expression to a variable (e.g. x = 2+2).
A semicolon at the end suppress screen output.
Enter y = 2 ˆ 2 + log(pi) * sin(x);
Matlab remembers y and you can recall the value of y later.
To display the value of y, enter y at the Matlab prompt and press the
return key.
Enter theta = acos(-1) at the prompt and press the return key.
Implement the following commands at the prompt.
format short e
theta
format long
theta
Exercises 1
Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 5 / 12
The Basics A minimum Matlab session

Creating and working with arrays

An array is a list numbers or expressions arranged in horizontal rows and


vertical columns.
When an array has only one row or one column, it is called a vector.
An array with m rows and n columns is called a matrix of size m × n.
Array operations

.* term-by-term multiplications
./ term-by-term division
.ˆ term-by-term exponentiation

Implement the following commands at the prompt


x = [1 2 3] (x is a row vector with three elements)
y = [2; 1; 5;] (y is a column vector with three elements)
z = [2 1 0]

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 6 / 12
The Basics A minimum Matlab session

Creating and working with arrays (Cont.)

a = x + z (You can add or subtract two vectors of the same size)


b = x + y (But you cannot add or subtract a row vector to a column
vector)
a = x .*z (You can multiply or divide the elements of two same-sized
vectors term by term with the array operator .* or ./)
b = 2*a (But multiplying a vector with a scalar does not need any
special operation - no dot before the *)
x = linspace (0, 10, 5) (Create a vector x with 5 elements linearly
spaced between 0 and 10)
y = sin(x) (Trigonometric functions sin, cos, etc., as well as
elementary math functions sqrt, exp, log, etc., operate on vectors
term by term)
z = sqrt(x).*y

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 7 / 12
The Basics A minimum Matlab session

Creating simple plots

plot creates a 2-D line plot


axis changes the aspect ratio of the x-axis and the y-axis
xlabel annotates the x-axis
ylabel annotates the y-axis
title puts a title on the plot
print prints a hard copy of the plot

E.g.
Let’s draw a circle of unit radius.
First generate data (x- and y-coordinates of, say 100 points on the circle),
then plot the data, and finally print the graph.
For generating data, use the parametric equation of a unit circle:

x = cosθ, y = sinθ, 0 ≤ θ ≤ 2π

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 8 / 12
The Basics A minimum Matlab session

Creating simple plots (cont.)

Implement the following commands


theta = linspace (0, 2*pi, 100); (Creates a linearly spaced 100
elements-long vector θ.)
x = cos(theta);
y = sin(theta); (Calculates x- and y-coordinates)
plot (x, y) (plot x vs y)
axis (’equal’); (Set the length scales of the two axes to be the same)
xlabel (’x’) (Label the x-axis with x)
ylabel (’y’) (Label the y-axis with y)
title (’Circle of unit radius’) (Put a title on the plot)
print (Print on the default printer)

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 9 / 12
The Basics A minimum Matlab session

Creating, saving, and executing a script file

A script file is a user-defined file with a sequence of Matlab commands in


it. The file must be saved with a .m extension to its name (M-file). A
script file is executed by typing its name (without the .m extension) at the
command prompt.
E.g. Let’s write a script file to draw the unit circle.
%CIRCLE - A script file to draw a unit circle
%——————————————–
theta = linspace(0, 2*pi, 100); %create vector theta
x = cos(theta); %generate x-coordinates
y = sin(theta); %generate y-coordinates
plot(x, y); %plot the circle
axis(’equal’); %set equal scale on axes
title(’Circle of unit radius’); %put a title

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 10 / 12
The Basics A minimum Matlab session

Creating, saving, and executing a script file (Cont.)

Type the following commands in the command window to execute the


script file.
help circle (seek help on the script files to see if Matlab can access it)
circle (execute the file)

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 11 / 12
The Basics A minimum Matlab session

Creating and executing a function file


A function file is also a M-file, just like a script file, except it has a
function definition line on the top that defines the input and output
explicitly.
E.g. Let’s write a function file to obtain the cube of a number.
function y = cube(x);
% CUBE - Function to return the cube of a number
% Call syntax: y = cube(x); or just: cube(x);
% Input: x = number to be cubed
% Output: y = cube of x
y = xˆ3;
Type the following commands.
cube(3)
y = cube(3)

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 16, 2011 12 / 12

You might also like