Professional Documents
Culture Documents
com
An Introductory Lecture
Key Features
www.mcdtu.wordpress.com 1
www.mcdtu.wordpress.com
Desktop
When you start MATLAB, the desktop appears in its default layout.
www.mcdtu.wordpress.com 2
www.mcdtu.wordpress.com
Figure window
Editor window
Built-in functions
Scalar Functions
Certain MATLAB functions are essentially used on scalars, but operate element-
wise when applied to a matrix (or vector). They are summarized in the table
below.
Note: It is strongly recommended to get help on all of them to find out exactly
how they are used.The trigonometric functions take as input radians.
Vector Functions
Matrix Functions
Much of MATLABs power comes from its matrix functions. These can be further
separated into two sub-categories. The first one consists of convenient matrix
building functions, some of which are given in the table below.
www.mcdtu.wordpress.com 4
www.mcdtu.wordpress.com
Start Working
>>3 + 4
ans =
= 3.1415
www.mcdtu.wordpress.com 5
www.mcdtu.wordpress.com
>>sin(pi/2)
ans =
1
a =
b =
ans =
5
NOTE: Use clear command to erase the value stored in a and b.
Array Creation
To create an array with four elements in a single row, separate the elements
with either a comma (,)oraspace.
>>clear
a =
1 2 3 4
www.mcdtu.wordpress.com 6
www.mcdtu.wordpress.com
>> b=[1 2 3; 3 4 6; 6 4 1]
b =
1 2 3
3 4 6
6 4 1
>>b^2 %square
ans =
25 22 18
51 46 39
24 32 43
The command whos gives all sorts of information on what variables are active.
A similar command, called who, only provides the names of the variables that are
active.
>>whos
a 1x4 32 double
ans 3x3 72 double
b 3x3 72 double
>> c=[0:8]
c =
0 1 2 3 4 5 6 7 8
www.mcdtu.wordpress.com 7
www.mcdtu.wordpress.com
To obtain a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following
line:
>> d=[0:2:8]
d =
0 2 4 6 8
>> r=linspace(0,8,5)
r =
0 2 4 6 8
>>clear
>> a=[1 1 1; 2 5 7; 2 1 -1]
a =
1 1 1
2 5 7
2 1 -1
>> b=[9;52;0]
b =
9
52
0
>> a\b
www.mcdtu.wordpress.com 8
www.mcdtu.wordpress.com
ans =
1
3
5
Note: The table below summarizes the operators that are available in MATLAB.
' transpose
\ left division
/ right division
.* multiplication
.^ Exponentiation
./ Right division
.\ \left division
>>clear
>> a=[1 2 3; 0 3 4; 2 3 2]
a =
1 2 3
0 3 4
2 3 2
>> b=[1 2 3; 0 3 4; 2 3 2]
b =
1 2 3
0 3 4
2 3 2
>> a.*b
www.mcdtu.wordpress.com 9
www.mcdtu.wordpress.com
ans =
1 4 9
0 9 16
4 9 4
>>a(:,2)
ans =
2
3
3
>>a(2,:)
ans =
0 3 4
Plotting
Oneof the most important and basic features of MATLAB is the commands for
data visualization (i.e. plotting. For two-dimensional plotting):
The PLOT command: The plot command has the following syntax
plot(x,y,linespecifier, property_name,property_value )
x, y: are vectors
Line_specifiers : defines and specify the type and color of the line and markers
www.mcdtu.wordpress.com 10
www.mcdtu.wordpress.com
Property name and value: are optional can be use to define the color and width of
line and marker.
A marker is a symbol thatappears at each plotted data point, such as a +, o,or *. For
example, 'g:*'requests a dotted green line with * markers.
If x and y are two vectors of the same length then plot(x,y) plots x versus y.
>>clear
>> x=[-pi:.1:pi];
>> y=cos(x);
>>plot(x,y) % x and y are vectors
>>xlabel('x')
>>ylabel('cos x')
>>title('graph of cos x between -pi to pi')
>> y=cos(x);
>>plot(x,y)
>>xlabel('x')
>>ylabel('sin x')
>> x = 0:pi/100:2*pi;
>>y = sin(x);
www.mcdtu.wordpress.com 11
www.mcdtu.wordpress.com
>>plot(x,y)
>>hold on
>>y2 = cos(x);
>>plot(x,y2,'r:')
>>legend('sin','cos')
The fplotcommand :
>> x=[-2:0.1:4];
>> y=3*x.^3-26*x+6;
>>yd=9*x.^2-26;
>>ydd=18*x;
>>plot(x,y,'-b',x,yd,'--r', x,ydd,':k')
www.mcdtu.wordpress.com 13
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com 14
www.mcdtu.wordpress.com
Syntax:
>>clear
>> f=@(x,y) x*y^2-3*y+4*x^4
f =
@(x,y)x*y^2-3*y+4*x^4
>>f(0,1)
ans =
-3
>>f(0 1)
www.mcdtu.wordpress.com 15
www.mcdtu.wordpress.com
The problem is that with the Matlab that the commands in the command window
cant be saved and executed again. In, addition to that the command window is not
interactive. A different way of executing commands with Matlab is first to create
a file with a list of commands, save it, and then run the file. I f the change is
required it can be made easily. Such files are called m-files because they must
have the filename extension .m. This extension is required in order for these
files to be interpreted by MATLAB.
There are two types of m-files: script files and function files.
Script files contain a sequence of usual MATLAB commands, that are executed
(in order) once the script is called within MATLAB. For example, if such a file
has the name compute.m , then typing the command compute at the MATLAB
prompt will cause the statements in that file to be executed. Script files can be
very useful when entering data into a matrix.
Ex.
Function files, on the other hand, play the role of user defined commands that
often have input and output. A user defined function is a matlab program that is
created by the user, saved as a function file, and then can be used like build-in
function.
www.mcdtu.wordpress.com 16
www.mcdtu.wordpress.com
The first executable line in a function file must be the function definition line.
Otherwise it will considered as a script file. I t has the following format:
EX.
ans =
4.5401
% If x is a vector then
>> x=1:2:11
x =
1 3 5 7 9 11
>>value(x)
ans =
0.7071 3.0307 4.1347 4.8971 5.5197
6.0638
www.mcdtu.wordpress.com 17
www.mcdtu.wordpress.com
Ex.
>> y=inline('(x.^4.*sqrt(3*x+5))./(x.^2+1).^2')
% one variable
y =
Inline function:
y(x) = (x.^4.*sqrt(3*x+5))./(x.^2+1).^2
>>y(6)
ans =
4.5401
>> z=inline('2*x^2-4*x*y+y^2','x','y')
% x , y shows the order arguments
z =
Inline function:
z(x,y) = 2*x^2-4*x*y+y^2
>>z(2,3)
ans =
-7
The feval command:This command evaluates the value of the function
for a given value/s of the functions arguments. I t has the following
syntax:
Variable=feval(function_name, argument_value)
www.mcdtu.wordpress.com 18
www.mcdtu.wordpress.com
Ex.
>>feval('sqrt',84)
ans =
9.1652
>>value=feval('sqrt',64)
value =
Note: This command is useful in the situations when the value of the
function has to be calculated inside another function. For example built-
in function fzero finds the zero of a function with one variable.
www.mcdtu.wordpress.com 19
www.mcdtu.wordpress.com
PROGRAMMING IN MATLAB
Operators:
>> 2>0
ans =
>> 3<-0
ans =
Conditional Statements:
If conditional expression
www.mcdtu.wordpress.com 20
www.mcdtu.wordpress.com
group of commands
end
>>wrkpay
Input the number of hrs worked 19
Hourly wages in Rs 10 % no output
>>wrkpay
Input the number of hrs worked 50
Hourly wages in Rs 40
The workers pay is Rs2200.00
if conditional_exp
group of commands
else
group of commands
end
www.mcdtu.wordpress.com 21
www.mcdtu.wordpress.com
if conditional_exp1
group of commands1
elseif conditional exp2
group of commands2
else
group of commands3
end
www.mcdtu.wordpress.com 22
www.mcdtu.wordpress.com
>> wrkpay3
Input the number of hrs worked 15
Hourly wages in Rs 10
The workers pay is Rs75.00The workers pay is Rs75.00
>> wrkpay3
Input the number of hrs worked 20
Hourly wages in Rs 10
The workers pay is Rs200.00The workers pay is Rs200.00
>> wrkpay3
Input the number of hrs worked 19
Hourly wages in Rs 5
The workers pay is Rs 0.00The workers pay is Rs 0.00>>
clear
The switch-case statement is another method that can be used to affect the flow of
a program. It provides a means for choosing one group of commands for execution
out of several possible groups.
www.mcdtu.wordpress.com 23
www.mcdtu.wordpress.com
>>syms x
>> y=diff(x^3)
>> y=diff(x^3)
y =
3*x^2
diff(f,x)
ans =
2*x*cos(x^2)
The same techniques work with expressions involving two or more variables:
syms x y
www.mcdtu.wordpress.com 24
www.mcdtu.wordpress.com
q=x^2*y^3*exp(x)
q =
x^2*y^3*exp(x)
pretty(q)
2 3
x yexp(x)
diff(q,y)
ans =
3*x^2*y^2*exp(x)
Define
syms a t
u=exp(a*t)
u=
exp(a*t)
One can then compute the left side of the differential equation, and see if it agrees
with the right side (zero):
diff(u,t)-a*u
ans =
0
Thus the given function u is a solution. Is the function v(t)=at another solution?
We can check it as follows:
>>v=a*t
a*t
www.mcdtu.wordpress.com 25
www.mcdtu.wordpress.com
diff(v,t)-a*v
ans =
a - a^2*t
Since the result is not zero, the function v is not a solution.
syms x y
w=sin(pi*x)+sin(pi*y)
w =
sin(pi*x) + sin(pi*y)
diff(w,x,2)+diff(w,y,2)
ans =
- pi^2*sin(pi*x) - pi^2*sin(pi*y)
simplify(ans)
ans =
-pi^2*(sin(pi*x) + sin(pi*y))
diff(w,x,5)
ans =
pi^5*cos(pi*x)
syms x y
w=x^2*exp(y)+x*y^2
w =
x^2*exp(y) + x*y^2
diff(diff(w,x),y)
ans =
2*y + 2*x*exp(y)
clear
syms a x
f=@(x)exp(a*x)
f =
@(x)exp(a*x)
diff(f(x),x)-a*f(x)
ans =
0
www.mcdtu.wordpress.com 27
www.mcdtu.wordpress.com
Boundary value problems are similar to initial value problems. A boundary value
problem has conditions specified at the extremes ("boundaries") of the independent
variable in the equation whereas an initial value problem has all of the conditions
specified at the same value of the independent variable (and that value is at the
lower boundary of the domain, thus the term "initial" value). Finding the
temperature at all points of an iron bar with one end kept at absolute zero and the
other end at the freezing point of water would be a boundary value problem.
For example, if the independent variable is time over the domain [0,1], a boundary
value problem would specify values for at both and , whereas an
initial value problem would specify a value of and at time .
Boundary value problems arise in several branches of physics as any physical
differential equation will have them. Problems involving the wave equation, such
as the determination of normal modes, are often stated as boundary value
problems. A large class of important boundary value problems are theSturm
Liouville problems. The analysis of these problems involves the eigenfunctions of
a differential operator.
Among the earliest boundary value problems to be studied is the Dirichlet problem,
of finding the harmonic functions (solutions to Laplace's equation); the solution
was given by the Dirichlet's principle.
EXAMPLE 1: Solve: =+
With 0 = 0.
=+
2. To compute the new derivative: and save this file with name
simpode.m.
xdot= x+t;
3. Type the following codes in the command window:
>> ylabel('x')
>> xlabel('t')
4. The following graph will be appear
>> clear
>> x=dsolve('Dx=x+t','x(0)=0')
x =
exp(t) - t 1
www.mcdtu.wordpress.com 30
www.mcdtu.wordpress.com
1. ODE of order >=2, write the differential equation as a set of first order
ODEs. Basically we need to form the equation in the vector form
= (, ), where
www.mcdtu.wordpress.com 31
www.mcdtu.wordpress.com
x=[x1,x2,x3,,xn].
3. Use the built-in ODEs solvers ode23 or ode45 to solve the set of
equations: It has the following syntax
[time, solution]=ode(your_function,
tspan, x0)
% time: time vector
% solution matrix
% ode: in-built ode solver
% your_function: user written function with
the title line xdot =(your_function(t,x);)
this function contains the ODEs you want to
solve.
% tspan: time span [t0, t_final]
% x0: in initial condition
% inputs: t=time
www.mcdtu.wordpress.com 32
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com 33
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com 34
www.mcdtu.wordpress.com
Concretely, an example of a boundary value (in one spatial dimension) is the problem
www.mcdtu.wordpress.com 35