Matlab Fundamentals Data PDF
Matlab Fundamentals Data PDF
INDEX
TOPICS PAGE.NO
1. INTRODUCTION 3-3
1.1 HISTORY 3-3
1.2 ALTERNATIVES 3-3
1.3 MATLAB FUNCTIONING AND WINDOWS 3-8
2. INPUTS IN MATLAB 8-8
3. ERRORS IN MATLAB 9-9
4. MATLAB PROGRAM FILES 9-9
5. RULES FOR NAMING A FILE 9-9
6. KEYWORDS 10-10
7. DATA TYPES 10-11
8. OPERATORS 11-11
8.1 CLASSIFICATION BASED ON NO.OF OPERANDS 11-12
8.2 CLASSIFICATION BASED ON TYPE OF OPERATION 12-25
9 CONDITIONAL STATEMENTS 26-28
10 SWITCH STATEMENTS 28-31
11 LOOPS
11.1 FOR LOOP 31-32
11.2 WHILE LOOP 32-33
12 BREAK AND CONTINUE IN LOOPS
12.1 BREAK AND CONTINUE IN FOR LOOP 33-34
12.2 BREAK AND CONTINUE IN WHILE LOOP 34-36
13 NESTED LOOPS 36-37
14 BREAK AND CONTINUE IN NESTED LOOPS
14.1 BREAK IN NESTED LOOP 38-39
14.2 CONTINUE IN NESTED LOOP 39-40
15 FUNCTIONS 41-43
1. INTRODUCTION
1.1. HISTORY
Cleve Moler, the chairman of the computer science department at the University of New Mexico,
started developing MATLAB in the late 1970s. He designed it to give his students access to
LINPACK and EISPACK without them having to learn Fortran. It soon spread to other
universities and found a strong audience within the applied mathematics community. Jack Little,
an engineer, was exposed to it during a visit Moler made to Stanford University in 1983.
Recognizing its commercial potential, he joined with Moler and Steve Bangert. They rewrote
MATLAB in C and founded MathWorks in 1984 to continue its development. These rewritten
libraries were known as JACKPAC. In 2000, MATLAB was rewritten to use a newer set of
libraries for matrix manipulation, LAPACK.
MATLAB was first adopted by researchers and practitioners in control engineering, Little's
specialty, but quickly spread to many other domains. It is now also used in education, in
particular the teaching of linear algebra and numerical analysis, and is popular amongst scientists
involved in image processing.
1.2. ALTERNATIVES
Although MATLAB is intended primarily for numerical computing, an optional toolbox uses the
MuPAD symbolic engine, allowing access to symbolic computing capabilities. An additional
package, Simulink, adds graphical multi-domain simulation and Model-Based Design for
dynamic and embedded systems.
In 2004, MATLAB had around one million users across industry and academia. MATLAB users
come from various backgrounds of engineering, science, and economics. MATLAB is widely
used in academic and research institutions as well as industrial enterprises.
windows in matlab
Work space: work space stores all the variables used in the program and it stores in the
following format.
A 3*3double 1 9
Current folder: lists all the files present in the current folder. ‘m’ is the extension for matlab files
or programs.
Example: main.m
We can see the execution of the file by typing file name in command window.
If values of minimum and maximum in a variable editor are changed,then those are also
modified automatically in workspace.
2. INPUTS IN MATLAB
In dynamic input, values for variables are provided during run time or execution on the
command window. The state of the command window is known at the left bottom corner of the
window.
1. Syntax error
Can be caused due to omitting operators in an expression.
Example: b(a+c) error expression
b*(a+c) correct expression
2. Execution time errors/run time errors
Can be caused while execution or compilation.
In matlab compilation and execution are done at a time line by line. Untill and
unless you clear the errors in first line you can’t proceed with further lines and so
on..
In MAT LAB no need to terminate the statements with semi colon(;).
No need to declare any variables to use them in the program.
1. Script
We write programs in script or edit file. Procedure to start with the edit file is
FILE NEW SCRIPT (or) edit(type edit in command window) (or) control-N
2. Function
Function calls are defined in function file.procedure to start with function file is
FILE NEW FUNCTION
Keywords are written in lower case only. they cannot be used as variable names.
7. DATA TYPES
There are 10 Data types in mat lab that reveals two different informations. They are
3. Int8 : int 8 has 1 byte of memory in which 1st bit or most significant bit is reserved as
sign bit.positive or negative sign of data is indicated with 1 or 0.
1 2 3 4 5 6 7 8
5. Int16: memory is 2 bytes(16 bits in which msb is sign bit). memory allocation is similar
to int8
msb
10. Logical: memory is only 1 byte and is used to store logical result of a function i,e 0 or 1.
8. OPERATORS
1. unary operators : we use only one operand and one operator to perform an operation.
Example: a’(transpose of a)
~a(not a)
1. Arithmetic operators
All the arithmetic operations can be performed between teo scalars,two matrices and a scalar
and a matrix.
clc
clear all
a=[1 2 3;3 4 5;5 6 7];
b=[4];
c=a+b;
display(c)
Result:
c=
5 6 7
7 8 9
9 10 11
Subtraction operator(-)
Example:
clc
clear all
a=[1 2 3;3 4 5;5 6 7];
b=[4];
c=a-b;
display(c)
Result:
c=
-3 -2 -1
-1 0 1
1 2 3
Multiplication operator(*)
Example:
clc
clear all
a=[1 2 3;3 4 5;5 6 7];
b=[4 5 6;7 8 9;0 3 2];
c=a*b;
display(c)
Result:
c=
18 30 30
40 62 64
62 94 98
clc
clear all
a=[1 2 3;4 5 6;7 8 9];
b=[2 3 1;5 4 3;9 0 8];
c=a.*b;
result :
c=
2 6 3
20 20 18
63 0 72
Left division(/)
Example:
clc
clear all
a=[7];
b=[4];
c=a/b;
display(c)
Result:
c = 1.7500
right division(\)
Example:
clc
clear all
a=[7];
b=[4];
c=a\b;
display(c)
Results:
(array division(./)
Example :
clc
clear all
a=[1 2 3;4 5 6;7 8 9];
b=2;
c=a./b;
display(c)
result :
c=
Power(^)
Example:
clc
clear all
a=10;
b=4;
c=a^b;
display(c)
Result:
c=
10000
clc
clear all
a=[1 2 3;4 5 6;7 8 9];
b=2;
c=a.^b;
display(c)
result:
c=
1 4 9
16 25 36
49 64 81
Transpose(')
Example:
clc
clear all
a=[1 2 3;4 5 6;7 8 9];
c=a';
display(c)
result:
c=
1 4 7
2 5 8
3 6 9
clc
clear all
a=[1+i 2-i 3;4 5+i 6;7 8 9-i];
c=a.';
display(c)
Result:
c=
2. Relational operators : result of relational operators will be in logical form. And these
operators can be used between 2 scalars, 2 matrices or a scalar and a matrix.
greater than(>)
Example :
clc
clear all
a=[1 2 3;4 5 6;7 8 9];
b=[1 0 9;5 4 3;6 7 1];
c=a>b;
display(c)
result:
c=
0 1 0
0 1 1
1 1 1
less than(<)
Result:
c=
1 1 1
1 1 0
0 0 0
Result:
c=0
1 0 1
1 0 0
0 0 0
equal to(==)
Example:
clc
clear all
a=[1 2 3;4 5 6;7 8 9];
b=[1 0 9;5 4 3;6 8 1];
c=a==b;
display(c)
Result:
c=
1 0 0
0 0 0
0 1 0
1 0 1
1 0 1
1 1 0
3. Logical operators
logical AND(&)
Example:
clc
clear all
a=[1 0 1;0 1 1;1 0 0];
b=[1 0 0;1 1 1;1 1 0];
c=a&b;
display(c)
Result:
c=
1 0 0
0 1 1
1 0 0
logical OR( | )
Example:
clc
clear all
a=[1 0 1;0 1 1;1 0 0];
b=[1 0 0;1 1 1;1 1 0];
c=a|b;
display(c)
Result:
c=
1 0 1
1 1 1
1 1 0
logical NOT(~)
Example:
clc
clear all
a=[1 0 1;0 1 1;1 0 0];
c= ~a;
display(c)
Result:
c=
0 1 0
1 0 0
0 1 1
4. Assignment operator
Assignment(=)
Example:
clc
clear all
a=6;
b=9;
a=b;
display(a)
Result:
a=9
5. Semicolon operator(;)
Semicolon is has two applications in matlab. Former is it is used as row saperator in matrix
entries and secondly it is used to suppress the output at command window. The assignment
statements are displayed on command window if they are not suppressed. To avoid that we
suppress those statements with semicocolon and write display statements wherever required.
Example:
clc
clear all
c=[12 23 34;5 67 45;23 46 89];
display(c)
Result:
c=
12 23 34
5 67 45
23 46 89
6. Colon operator (:) : colon operator is used for generation and accessing values from a
matrix.
clc
clear all
t=0:2:20;
x=sin(t);
result
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
0 2 4 6 8 10 12 14 16 18 20
Example:
clc
clear all
a=[1 2 3 4 5;5 6 7 8 9;10 11 12 13 14;15 16 17 18 19;3 45 12 13 78];
b=a(1,1:5)
c=a(2:4,1:3)
d=a(1:4:5,1:3:4)
e=a(end-2,end-3)
Result:
b= 1 2 3 4 5
c=
5 6 7
10 11 12
15 16 17
d=
Example:
clc
clear all
a=[1 2 3 4 5;5 6 7 8 9;10 11 12 13 14;15 16 17 18 19;3 45 12 13 78];
b=uint8(a)
Result:
b=
1 2 3 4 5
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
3 45 12 13 78
Size and data type of a variable are stored in variable editor in workspace.
Simple if
Syntax:
if condition
----
----
-----
End
Example:
clc
clear all
a=input('enter a value');
b=input('enter b value');
if a>b
display('a is greater')
end
If-else
Syntax:
if condition
----
----
-----
else
----
----
end
example
clc
clear all
a=input('enter a value');
b=input('enter b value');
if a>b
display('a is greater')
else
display('b is greater')
end
Elseif
Syntax:
if condition
----
----
-----
elseif condition
----
----
else
---
---
end
example:
clc
clear all
a=input('enter a value');
b=input('enter b value');
if a>b
display('a is greater')
elseif b>a
display('b is greater')
else
display('a and b are equal')
end
Nested if
Syntax:
if condition
----
----
if condition
----
----
end
end
example:
10.SWITCH STATEMENTS
In these statements the program can be executed by choosing different parts of it.
Example1
clc
clear all
dsply=input('enter choice');
switch dsply
case 1
display('case 1')
case 2
display('case2')
case 3
display('case3')
otherwise
display('wrong choice')
end
example2
clc
clear all
t=0:0.5:20;
dsply=input('enter choice');
SV MATLAB Solutions 9652458410 Anji Reddy
29
MATLAB fundamentals of programming
switch dsply
case 1
x=sin(t);
plot(t,x)
title('sine wave')
xlabel('time period')
ylabel('amplitude')
case 2
y=sin(t);
plot(t,y)
title('cos wave')
xlabel('time period')
ylabel('amplitude')
case 3
z=tan(t);
plot(t,z)
title('tan wave')
xlabel('time period')
ylabel('amplitude')
otherwise
display('wrong choice')
end
result
enter choice 1
sine wave
1
0.5
amplitude
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
time period
cos wave
1
0.5
amplitude
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
time period
enter choice 3
tan wave
50
-50
amplitude
-100
-150
-200
-250
0 2 4 6 8 10 12 14 16 18 20
time period
enter choice 4
9. LOOPS
Loops are used to repeat a set of statements until a condition fails or for a particular number of
times. There are two loops in matlab.
Syntax:
For (expression)
_____
____
End
Example:
clc
clear all
for i=1:10
display(a)
end
as a result we get display of numbers from 1 to 10.so number of iterations are 10.
Syntax:
while (condition)
_____
____
End
clc
clear all
a=input('enter a value');
b=input('enter b value');
while a<b
display(a)
display(b)
a=a+2;
b=b-3;
end
execution
enter a value30
enter b value40
a = 30
b = 40
a = 32
b = 37
Break when break statement is used in loop,then the execution comes out of the loop.
Continue when continue is used in the loop,then execution shifts to initial step.
program
execution
a =1
a=2
a=3
a= 4
program
clc
clear all
for a=1:10
if a==5
continue
end
display(a)
end
execution
a=1
a=2
a=3
a=4
a=7
a=8
a=9
a = 10
Program
clc
clear all
a=1;
while a<=10
if a==5
break
end
display(a)
a=a+1;
end
execution
a =1
a=2
a=3
a= 4
program
clc
clear all
a=0;
end
execution
a=1
a=2
a=3
a=4
a=6
a=7
a=8
a=9
a = 10
a = 11
One loop can be a part of other loop. Nesting can be done in any manner.
Syntax:
for (expression1)
_____
______
For (expression2)
_____
End
Mostly nested loops are written to represent matrix in mtlab.where first loop represents number
of rows and second loop represents number of columns.
Example:
clc
clear all
a=[1 2 3;3 4 5;5 6 7];
[m n]= size(a);
for i=1:m
for j=1:n
display(a(i,j))
end
end
execution
ans = 1
ans = 2
ans = 3
ans =3
ans = 4
ans = 5
ans =5
ans = 6
ans = 7
execution
s = 12
clc
clear all
for i=1:5
display(i)
for j=1:5
if j==3
break
end
display(j)
end
end
execution
i=1
j=2
i=2
j=1
j=2
i=3
j =1
j=2
i=4
j=1
j=2
i =5
j =1
j= 2
12.2 Continue
clc
clear all
for i=1:5
display(i)
for j=1:5
if j==3
continue
end
display(j)
end
end
i=1
j =1
j= 2
j=4
j=5
i=2
j=1
j=2
j=4
j=5
i=3
j =1
j =2
j=4
j =5
i=4
j=1
j=2
j=5
i= 5
j =1
j=2
j= 4
j =5
13. FUNCTIONS
A function is a set of statements which are written to perform a particular task.the function name
and file name must be same. Here in a function call we provide input arguments to perform a
task and result is returned by storing in the output argument.
end
functions are classified as
1. Primary functions: in which function is directly called in main program
Example:
Main program
clc
function call
end
execution
enter a value45
enter b value55
s=
100
Main program
clc
clear all
close all
a=input('enter a value');
b= input('enter b value');
[s,d]=add(a,b);
display(s)
display(d)
function call
function d = sub(a,b)
execution
enter a value30
enter b value20
s= 50
d= 10
Write a program to find out area and circumference of a circle using functions.
Program
clc
clear all
r=input('enter radius of a circle');
[area,circumference]= areacircum(r);
display(area)
display(circumference)
function window
area=2*pi*r;
circumference=pi*r*r;
end
Result
area = 31.4159
circumference = 78.5398
In dynamic input, values for variables are provided during run time or execution on the
command window. The state of the command window is known at the left bottom corner of the
window.
16.2Predefined functions
Predefined functions are of various types.they are:
a. Zeros( )
This function generates matrix with all 0s of defined size. This can be used for initializing a
matrix.
Example: i=zeros(2,3);
i=[ 0 0] 0
000
Example: i=ones(2);
i=[ 0 ]0
00
c. rand( ): generates matrix with random numbers.
rand(3)
ans =
0 0 1
0 0 0
1 1 0
Save: is used to save workspace data.the data must be in valid matrix form.
Syntax: save ‘file name’ variables
Example: a=zeros(2);
b=[1 2;3 4];
save example.mat a b
now a file ‘example.mat’ is created in the current folder with data of a and b.
a. Load: is used to load the data from the file to command window
Example:
execution
m= 4
n=4
2. Sum( ): gives sum of individual column elements
ans = 17 21 25 19
3. diag( ) : displays diagonal elements of a matrix
diag(a)
ans =
1
5
7
0
4. fliplr( ) :flips the matrix from left to right
fliplr(a)
ans =
4 3 2 1
7 6 5 4
8 7 6 5
0 9 8 7
5. det( ): produces the determinant for a given matrix
b=[1 2 3;3 4 5;5 8 6];
det(b)
ans = 10.0000
6. inv( ) :gives inverse of a matrix
inv(b)
ans =
magic(3)
ans =
8 1 6
3 5 7
4 9 2
example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
plot(t,x)
0.5
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
plot(t,x)
title('sine wave')
sine wave
1
0.5
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
plot(t,x)
title('sine wave')
xlabel('frequency')
sine wave
1
0.5
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
frequency
example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
sine wave
1
0.5
amplitude
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
frequency
example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
figure, plot(t,x)
title('sine wave')
xlabel('frequency')
ylabel('amplitude')
example:
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
frequency
example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
stem(t,x)
title('sine wave')
xlabel('frequency')
ylabel('amplitude')
amplitude 0.5
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
frequency
example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
y=cos(t);
subplot(2,1,1)
plot(t,x)
title('sine wave')
xlabel('frequency')
ylabel('amplitude')
subplot(2,1,2)
plot(t,y)
title('cos wave')
xlabel('frequency')
ylabel('amplitude')
sine wave
1
0.5
amplitude
0
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
frequency
cos wave
1
0.5
amplitude
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
frequency
example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
y=cos(t);
plot3(t,x,y)
title('helical structure')
0.5
-0.5
-1
1
0.5 20
0 15
10
-0.5 5
-1 0
Properties of plots can also be changed by using different colors and symbols.
Example:
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
plot(t,x,'*')
title('sine wave')
sine wave
1
0.5
-0.5
-1
0 2 4 6 8 10 12 14 16 18 20
clc
clear all
close all
t=0:0.5:20;
x= sin(t);
stem(t,x,'r')
title('sine wave')
sine wave
1
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 2 4 6 8 10 12 14 16 18 20
GUIDE
GUIDE or the Graphical User Interface development environment is a tool for laying out and
programming GUI’s. A GUI uses graphics and text input to make using Matlab much more user
friendly for people who are unfamiliar with it. GUI’s can be created without using GUIDE but
laying out the design of the window can be very time consuming. To open GUIDE click on the
start button in the bottom left corner of Matlab and select START→MATLAB→GUIDE
In computing, graphical user interface (GUI) is a type of user interface that allows users
to interact with electronic devices using images rather than text commands. GUIs can be used in
computers, hand-held devices such as MP3 players, portable media players or gaming devices,
household appliances, office, and industry equipment. A GUI represents the information and
actions available to a user through graphical icons and visual indicators such as secondary
notation, as opposed to text-based interfaces, typed command labels or text navigation. The
actions are usually performed through direct manipulation of the graphical elements.
The term GUI is restricted to the scope of two-dimensional display screens with display
resolutions able to describe generic information, in the tradition of the computer science
research.
19.1 Overview
Making Graphical User Interfaces in Matlab is very simple. A good place to begin learning about
GUI development on the Matlab platform is to first understand how Matlab manages graphical
objects. This particular tutorial focuses primarily on Matlab 6. This platform makes and excellent
choice for developing interactive interfaces as the previous versions of Matlab had a noticeably
clumsier and less mature feel when to came to developing GUI’s. Developing GUI’s on Matlab 6
is a breeze and hopefully this tutorial will be sufficient to get most anyone started. If you are
running an older version of Matlab, this tutorial will help you get started however it will not be
able to guide you all the way. I would recommend a migration to Matlab 6 as it as a more stable
and a more mature platform where many of the bugs, especially in mat lab’s ability to handle
Quick Re-Cap:
GUI’s are a collection of objects.
Every object has a unique handle.
Every object has properties.
The user can alter these properties at design time.
String property
Tag property
handles =
figure1: 102.0034
edit1: 3.0043
pushbutton1: 103.0039
>> guide
STEP 3: Right Click on the Push Button and a pop up menu should appear.
From that menu pick the ‘Inspect Properties’ option.
STEP 4: Right click on the lowest ‘Static Text’ object and select ‘Inspect properties’.
STEP 5: Right click on the other ‘Static Text’ object and select ‘Inspect properties’.
STEP 6: Right click on the third ‘Static Text’ object and select ‘Inspect properties’.
STEP 6: Right click on the third ‘Edit Text’ object and select ‘Inspect properties’.
If you have made a mistake in the GUI you can easily correct it at this point without moving on.
For instance, if you look closely in this GUI there is really no way to select which of the two
(sin(x) or cos(x) are to be plotted. We should have used a different object in the first place..
perhaps a check box. We can easily delete the wrong object and replace it with the one we want.
Since this is our first GUI we will keep it simple and get rid on one of the static text boxes.
Click on the Cos(x) box and hit the delete key and the cos(x) should disappear from the
GUI. Once that is done, save as ‘mygui’.
As soon as you save it Matlab should generate the skeleton source code for you and the
source code should automatically open in an editor.
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = demodemogui_OutputFcn(hObject, eventdata, handles)
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = slidergui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t = imread('cameraman.tif');
a = handles.th;
b = t>=a;
axes(handles.axes1)
imshow(t)
axes(handles.axes2)
imshow(b)
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = edttxtpgm_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
th=str2double(get(hObject,'string'));
handles.th=th;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Outputs from this function are returned to the command line.
function varargout = calculatorpgm_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
Result for addition operation between two digits entered in edit texts
Result for subtraction operation between two digits entered in edit texts
Result for multiplication operation between two digits entered in edit texts
Result for division operation between two digits entered in edit texts
Before you begin installation of MATLAB on your laptop, read through these installation
instructions completely so you have an idea of what you are about to do. The installation process
can take as long as 30 minutes depending on your laptop model. The installation will require
about 536 MBytes of disk space on your C: drive.
If you already have MATLAB installed on your laptop, determine what version you have
installed by starting MATLAB and selecting the "Help" menu and the "About MATLAB"
command. You should see a window similar to the one below.
If you have Version 7.4, R2007b installed, you do not need to reinstall MATLAB.
If you have an earlier version of MATLAB installed, you need to install the newer version.
Please do the following BEFORE you install the newer version.
1. Close all applications running on your computer except your web browser.
2. Make sure you are connected to the USAFANet, preferably wired, and can access
USAFA's M: drive.
4. When prompted with the File Download dialog box shown below, click on Run.
If prompted with the Security Warning dialog box shown below, click on Run.
5. Select the Install radio button and press the Next button when prompted with the
dialog box shown below.
6. In the License Information dialog box, enter your name, USAFA as the company, and
copy/paste the following PLP into the appropriate box.
18-59647-32838-57019-09758-36473-36893-56275-55067-05204-41411-13629-06779-
64385-14716
7. In the License Agreement dialog box, select the "Yes" radio button after reviewing the
license agreement and then press the Next button.
8. In the Installation Type dialog box, select the Custom radio button (not the default
Typical) and then press the Next button.
9. In the Product and Folder Selection dialog box, do not change the installation
directory from C:\Program Files\MATLAB\R2007b\ (unless you really know what
you are doing). In the Select products to install window, deselect (uncheck) all
products except MATLAB 7.4. You will have to scroll down in this window to
deselect all products. While you can install the additional products listed, these
products are not required for CS211 and will take up extra disk space. If you already
have written MATLAB code that depends on a toolbox (such as the Symbolic
toolbox), then you should select that toolbox for installation. If necessary, you may
install toolboxes any time after this MATLAB installation. After deselecting the
products, your screen should appear similar to the one below. Click on Next.
10. In the Custom Installation dialog box, you should not change any of the defaults
shown below. However, you may want to uncheck the .fig file association box if you
have another application which you prefer remain associated with .fig files. It is
important that the .m file association be set for MATLAB. Click on Next.
11. In the Confirmation dialog box shown below, click on Install to begin the installation.
12. During the installation you will see a progress bar similar to that shown below. This
part of the installation is likely to take from 15 to 30 minutes depending on your
laptop model and configuration.
13. When the installation is compete, you will likely see a screen about available
updates. Continue to the next page which should look like the screen shown below.
Check the Start MATLAB box, uncheck any download product updates box and click
on Finish to start MATLAB.
14. If MATLAB was successfully installed, MATLAB should begin and you should see a
screen similar to that shown below. Don't work about any expiration notice you may
see. We'll get you a new PLP before MATLAB conks-out.
15. To exit MATLAB, select the File menu, Exit MATLAB command. You can restart
MATLAB by clicking on the MATLAB icon on your desktop or by selecting Start --
> All Programs -> MATLAB -> R2007b -> MATLAB R2007b.
16. If you were not successful in installing MATLAB, try starting over from the
beginning and following the instructions very carefully. If you still cannot get
MATLAB installed, send a message to your instructor explaining the problem you are
having.
MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning. This allows you to solve many technical computing problems, especially those
with matrix and vector formulations, in a fraction of the time it would take to write a program in
a scalar noninteractive language such as C or Fortran.
The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide
easy access to matrix software developed by the LINPACK and EISPACK projects, which
together represent the state-of-the-art in software for matrix computation.
MATLAB has evolved over a period of years with input from many users. In university
environments, it is the standard instructional tool for introductory and advanced courses in
mathematics, engineering, and science. In industry, MATLAB is the tool of choice for high-
productivity research, development, and analysis.
MATLAB features a family of application-specific solutions called toolboxes. Very important to
most users of MATLAB, toolboxes allow you to learn and apply specialized technology.
Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the
MATLAB environment to solve particular classes of problems. Areas in which toolboxes are
available include signal processing, control systems, neural networks, fuzzy logic, wavelets,
simulation, and many others.
How to represent exponential growth and decay of population using mat lab?
In fact, this is a simple program for computing iteratively the values of a sequence an = f (an−1),
n ≥ 1, provided you have previously entered the formula for the function f and the initial value of
the sequence a0. Note the extra parameter r built into the algorithm. Now let’s use the program
to compute two populations at five-year intervals for different values of r:
ans =
1.0e+006 *
0.00010000000000
0.00016105100000
0.00025937424601
0.00041772481694
0.00067274999493
0.00108347059434
0.00174494022689
0.00281024368481
ans =
1.0e+002 *
1.00000000000000
0.59049000000000
0.34867844010000
0.20589113209465
0.12157665459057
0.07178979876919
0.04239115827522
0.02503155504993
0.01478088294143
0.00872796356809
0.00515377520732
0.00304325272217
0.00179701029991
0.00106111661200
0.00062657874822
0.00036998848504
0.00021847450053
0.00012900700782
0.00007617734805
0.00004498196225
0.00002656139889
In the first case, the population is growing rapidly; in the second, it is decaying rapidly. In fact, it
is clear from the model that, for any n, th e quotient Pn+1/Pn = (1 + r), and therefore it follows
that Pn = P0(1 + r)n, n ≥ 0. This accounts for the expression exponential growth and decay. The
model predicts a population growthwith out bound (for growing populations) and is therefore not
where t represents time in seconds, x represents the angle of the pendulum from the vertical in
radians (so that x = 0 is the rest position), y represents the velocity of the pendulum in radians per
second, and 9.81 is approximately the acceleration due to gravity in meters per second squared.
Here is a phase portrait of the solution with initial position x(0) = 0 and initial velocity y(0) = 5.
This is a graph of x versus y as a function of t, on th e time interval 0 ≤ t ≤ 20.
Recall that the x coordinate corresponds to the angle of the pendulum and the y coordinate
corresponds to its velocity. Starting at (0, 5), as t increases we follow the curve as it spirals
clockwise toward (0, 0). The angle oscillates back and forth, but witheac hswing it gets smaller
until the pendulum is virtually at rest by the time t = 20. Meanwhile the velocity oscillates as
This time the angle increases to over 14 radians before the curve spirals in to a point near (12.5,
0). More precisely, it spirals toward (4π, 0), because 4π radians represents the same position for
the pendulum as 0 radians does. The pendulum has swung overhead and made two complete
revolutions before beginning its damped oscillation toward its rest position. The velocity at first
decreases but then rises after the angle passes through π, as th e pendulum passes the upright
position and gains momentum. The pendulum has just enough momentum to swing through the
upright position once more at the angle 3π.
Now suppose we want to find, to within 0.1, the minimum initial velocity required to make the
pendulum, starting from its rest position, swing overhead once. It will be useful to be able to see
the solutions corresponding to several different initial velocities on one graph. First we consider
the integer velocities 5 to 10.
hold on
for a = 5:10
[t, xa] = ode45(g, [0 20], [0 a]);
plot(xa(:, 1), xa(:, 2))
end
hold off
Initial velocities 5, 6, 7 are not large enoughfor the angle to increase past π,
but initial velocities 8, 9, 10 are enoughto make the pendulum swing overhead.
Let’s see what happens between 7 and 8.
hold on
We see that the cutoff is somewhere between 7.2 and 7.4. Let’s make one
more refinement.
hold on
for a = 7.2:0.05:7.4
[t, xa] = ode45(g, [0 20], [0 a]);
184 Chapter 9: Applications
plot(xa(:, 1), xa(:, 2))
end
hold off