Professional Documents
Culture Documents
Ababa
University
Addis
Ababa
Institute
of
Technology
School
of
Electrical
and
Computer
Engineering
Matlab
Tutorial
Lab
Four:
Calculus
in
Matlab
The
goal
of
this
laboratory
session
is
to
learn
how
algebra
and
calculus
are
done
in
Matlab.
Exercise
1:
Solving
Algebraic
Equations
The
solve
function
is
used
for
solving
algebraic
equations.
In
its
simplest
form,
the
solve
function
takes
the
equation
enclosed
in
quotes
as
an
argument.
Procedure:
1. Write
the
following
script
in
algebra.m
file
and
execute
it
to
see
the
solution
to
an
algebraic
equation.
% This script demonstrates solving algebraic equations
%%
%solving a quadratic equation
eq='x^2 - 7*x +12=0';
s=solve(eq);
disp('The first root is:'),disp(s(1));
disp('The second root is:'),disp(s(2));
2. Add
the
following
code
cell
and
execute
it
to
see
solving
for
a
variable
%%
%solving for a variable
eq2='v-u-3*t^2';
s2=solve(eq2,'v'); %solving for v
fprintf(1,'%s %s\n','v =',s2);
3. Add
the
following
code
cell
and
execute
it
to
see
solution
to
systems
of
equations
%%
%solving for systems of equations
eq3= '5*x+9*y=5';
eq4='3*x -6*y=4';
s3=solve(eq3,eq4);
fprintf(1,'%s %f \n %s %f\n','x = ',s3.x,'y = ',s3.y);
4. Add
the
following
code
cell
and
execute
it
to
see
solution
to
systems
of
equations
1
%%
% expanding, collecting, factorizing and simlifying equations
syms x %symbolic variable
syms y %symbolic variable
eq5=expand((x+5)*(x+9));
eq6=expand((x+2)*(x-3)*(x-5)*(x+7));
eq7=collect(x^3*(x-7));
eq8=factor(eq6);
eq9=simplify((x^4-16)/(x^2-4));
disp(eq5);
disp(eq6);
disp(eq7);
disp(eq8);
disp(eq9);
Exercise
2:
Calculus:
limits
MATLAB
provides
the
limit
command
for
calculating
limits.
In
its
most
basic
form,
the
limit
command
takes
expression
as
an
argument
and
finds
the
limit
of
the
expression
as
the
independent
variable
goes
to
zero.
Procedure:
1. Write
the
following
script
in
limits.m
file
and
execute
it
%This script demontrates limits in matlab
%%
% simple limits
syms x
f=((x^3+5)/(x^4+7));
l=limit(f);
disp(l);
f2=((x-3)/(x-1));
l2=limit(f2,1); % limit of f2 as goes to 1
disp(l2);
2. Add
the
following
code
cell
to
see
left
and
right
sided
limits
%%
% left and right sided limits
f3=(x-3)/abs(x-3); %f3 (x-3)/|x-3|
ezplot(f3,[-1,5]);
grid on;
l3=limit(f3,x,3,'left');
r=limit(f3,x,3,'right');
disp(l3);
disp(r);
2
Exercise
3:
Calculus:
Differential
MATLAB
provides
the
diff
command
for
computing
symbolic
derivatives.
In
its
simplest
form,
you
pass
the
function
you
want
to
differentiate
to
diff
command
as
an
argument.
Procedure:
1. Write
the
following
script
in
differentials.m
file
and
execute
it
%%
% computing higher oder derivatives
f=x*exp(-3*x);
diff(f,2) % second order derivative
diff(f,3) % third order derivative
%%
% Given y=f(x)=3sin(x) + 7cos(x) would
% f'' + f= -5cos(2x) holds true
syms x
y=3*sin(x) + 7*cos(5*x); %defining the function
ezplot(y,[-2*pi 2*pi]);
lhs =diff(y,2)+y; %evaluating the left hand side of the
equation
rhs = -5*cos(2*x); %right hand side of the equation
if(isequal(lhs,rhs))
disp('Yes, the equation holds true');
else
disp('No, the equation does not hold true');
end
disp('Value of LHS is: '),disp(lhs);
2. Add
the
following
code
cell
to
see
application
of
finding
minima
and
maxima
of
a
curve
%%
% Minima and Maxima finding (local)
y1=2*x^3 + 3*x^2 - 12 *x + 17;
ezplot(y1,[-2 2]);
g=diff(y1);
disp('diff(y)');disp(g);
s=solve(g);
disp('Minima and Maxima:'),disp(s);
3. Add
the
following
code
cell
to
see
solutions
of
differential
equations
%%
% Solving differential equations with
dsolve('eqn','cond1','cond2', ...)
% f'(t)=-2f + cos(t)
syms t
deq='Df=-2*f+cos(t)';
dsolve(deq)
%f''(x)+2f'(x)=5sin3x
deq2='D2y-2*Dy=5*sin(3*x)';
dsolve(deq2)
%with initial conditions
3
%y''-y=0, y(0)=-1, y'(0)=2
deq3='D2y-Dy=0';
dsolve(deq3,'y(0)=-1','Dy(0)=2')
Exercise
4:
Calculus:
Integration
Integration
deals
with
two
essentially
different
types
of
problems.
In
the
first
type,
derivative
of
a
function
is
given
and
we
want
to
find
the
function.
Therefore,
we
basically
reverse
the
process
of
differentiation.
This
reverse
process
is
known
as
anti-‐differentiation,
or
finding
the
primitive
function,
or
finding
an
indefinite
integral.
Procedure:
1. Write
the
following
script
in
integrals.m
file
and
execute
it
%% Indefinite integrals
syms x n
int (sym(x^n));
f='sin(n*t)'
int(sym(f))
syms a t
int ( a*cos(pi*t))
int (a^x)
pretty(int(x^5*cos(5*x)))
The
second
type
of
problems
involve
adding
up
a
very
large
number
of
very
small
quantities
and
then
taking
a
limit
as
the
size
of
the
quantities
approaches
zero,
while
the
number
of
terms
tend
to
infinity.
This
process
leads
to
the
definition
of
the
definite
integral.
Definite
integrals
are
used
for
finding
area,
volume,
center
of
gravity,
moment
of
inertia,
work
done
by
a
force,
and
in
numerous
other
applications.
Procedure:
1. Add
the
following
code
cell
in
integrals.m
file
and
execute
it
%% Definite Integrals
f=x^3 -2*x +5;
a=int(f,1,2);
display('Area: '), disp(double(a));
f=x^2*cos(x);
ezplot(f,[-4,9]);
a=int(f,-4,9);
disp('Area:'),disp(double(a));
4
5