Introduction To Matlab: Numerical Computation - As You Might Guess From Its Name, MATLAB Deals Mainly With
Introduction To Matlab: Numerical Computation - As You Might Guess From Its Name, MATLAB Deals Mainly With
INTRODUCTION TO MATLAB
MATLAB prompt (») will be used to indicate where the commands are entered.
Anything you see after this prompt denotes user input (i.e. a command) followed by a
carriage return (i.e. the “enter” key). Often, input is followed by output so unless
otherwise specified the line(s) that follow a command will denote output (i.e.
MATLAB’s response to what you typed in). MATLAB is case-sensitive, which means
that a + B is not the same as a + b. Different fonts, like the ones you just
witnessed, will also be used to simulate the interactive session. This can be seen in the
example below:
e.g. MATLAB can work as a calculator. If we ask MATLAB to add two numbers, we get
the
answer we expect.
» 3 + 4
ans =7
As we will see, MATLAB is much more than a “fancy” calculator.
10161
MATLAB BASICS
» who
Your variables are:
ans x y
If we no longer need a particular variable we can “erase” it from memory using the
command clear variable_name. Let us clear the variable ans and check that
we indeed did so.
» clear ans
» who
Your variables are:
x y
The command clear used by itself, “erases” all the variables from the memory. Be
careful, as this is not reversible and you do not have a second chance to change your
mind.
You may exit the program using the quit command. When doing so, all variables are
lost.
However, invoking the command save filename before exiting, causes all variables
to be written to a binary file called filename.mat. When we start MATLAB again,
we may retrieve the information in this file with the command load filename. We
can also create an ascii (text) file containing the entire MATLAB session if we use the
command diary filename at the beginning and at the end of our session. This will
create a text file called filename (with no extension) that can be edited with any text
editor, printed out etc. This file will include everything we typed into MATLAB during
the session (including error messages but excluding plots). We could also use the
command save filename at the end of our session to create the binary file described
above as well as the text file that includes our work.
One last command to mention before we start learning some more interesting things
about MATLAB, is the help command. This provides help for any existing MATLAB
command.
Let us try this command on the command who.
» help who
WHO List current variables.
WHO lists the variables in the current workspace.
WHOS lists more information about each variable.
WHO GLOBAL and WHOS GLOBAL list the variables in the
global workspace.
Try using the command help on itself!
On a PC, help is also available from the Window Menus. Sometimes it is easier to look up
command from the list provided there, instead of using the command line help.
To define vectors (and matrices) that contain equally spaced entries. This can be done by
specifying the first entry, an increment, and the last entry. MATLAB will automatically
figure out how many entries you need and their values. For example, to create a vector
whose entries are 0, 1, 2, 3, …, 7, 8, you can type
10161
» u = [0:8]
u = 0 1 2 3 4 5 6 7 8
Here we specified the first entry 0 and the last entry 8, separated by a colon ( : ).
MATLAB automatically filled-in the (omitted) entries using the (default) increment 1.
You could also
specify an increment as is done in the next example.To obtain a vector whose entries are
0, 2, 4, 6, and 8, you can type in the following line:
» v = [0:2:8]
v = 0 2 4 6 8
Here we specified the first entry 0, the increment value 2, and the last entry 8. The two
colons ( :) “tell” MATLAB to fill in the (omitted) entries using the specified increment
value.
MATLAB will allow you to look at specific parts of the vector. If you want, for example,
to onlylook at the first 3 entries in the vector v, you can use the same notation you used to
create the
vector:
» v(1:3)
ans = 0 2 4
Note that we used parentheses, instead of brackets, to refer to the entries of the vector.
Since weomitted the increment value, MATLAB automatically assumes that the
increment is 1. The following command lists the first 4 entries of the vector v, using the
increment value 2 :
» v(1:2:4)
ans =0 4
8 Defining a matrix is similar to defining a vector. To define a matrix A, you can treat it
like a column of row vectors. That is, you enter each row of the matrix as a row vector
(remember to separate the entries either by commas or spaces) and you separate the rows
by semicolons ( ; ).
» A = [1 2 3; 3 4 5; 6 7 8]
A =1 2 3
3 4 5
6 7 8
We can avoid separating each row with a semicolon if we use a carriage return instead. In
other
words, we could have defined A as follows
» A = [1 2 3
3 4 5
6 7 8]
A=1 2 3
3 4 5
6 7 8
which is perhaps closer to the way we would have defined A by hand using the linear
algebra notation.
You can refer to a particular entry in a matrix by using parentheses. For example, the
number 5 lies in the 2nd row, 3rd column of A, thus
» A(2,3)
ans = 5
10161
PRACTICAL NO.1
AIM: Illustrate the simple mathematical expression in MATLAB.
1. z=2^5/2^5-1
PROGRAM
clc;
clear all;
close all;
x=2^5
y=x-1
z=x/y
RESULT:
x =32
y =31
z =1.0323
RESULT
x =1.2361
y =3.236
z =10.4721
a =0.3541
b = -0.6459
3. Area=pi*r^2; r=pi^1/3
PROGRAM
clc;
clear all;
close all;
r=pi^1/3
Area= pi*r^2
RESULT
r =1.0472
Area = 3.4451
10161
RESULT
x = 40.1092
y =2.6254e+017
RESULT
a = 0.5000
b = 0.2500
c = 0.8660
d = 0.7500
y=1
6.exp(i*pi/4)
PROGRAM
clc;
clear all;
close all;
x= exp (i*pi/4)
RESULT
x = 0.7071 + 0.7071i
10161
7. t= exp(pi/(4*i))
PROGRAM
clc;
clear all;
close all;
x= 4*i
t= exp(pi/x)
RESULT
x = 0 + 4.0000i
t = 0.7071 - 0.7071i
RESULT
t=1 2 3 4 5 6 7 8 9 10
z = -0.0052