Chapter 3
Chapter 3
Applications in MATLAB
Author
image here
Integration
Derivation
Taylor series
Outline
Derivation and Integration
Matlab can also compute many integrals and derivatives that you might
find in Calculus or many advanced engineering courses.
The key functions are int for integration and diff for derivation.
>> syms x;
>> f = sin(5*x)
>>diff(f)
ans = ?
3
Derivation and Integration (2nd and Higher Derivative)
>> f = x^3
4
Partial Differential Equations
>> syms x t;
>> f = sin( x * t )
>> diff(f,t) % derivative of f with respect to t
ans = ?
5
Integration
By using the int function, in the same way we use the diff function, we
can ask Matlab to do symbolic integration for us.
>> syms x t;
>> f = x * x ;
>>int(f)
ans = ?
6
Definite Integrals
As you (should) know, a definite integral represents the area under a curve
from a given start point to a given end point.
Try to find how much area is under the curve f(x) = x^3; for [0..10], [-
10..10], [-10..0]
>> syms x;
f= x^3
>> int(x^3,0,10)
ans = ?
>> int(x^3,-10,10)
ans = ?
>> int(x^3,-10,0)
ans = ?
7
MATLAB can find the derivative of
symbolic expressions. Higher derivatives
can also be found easily. These
derivatives can be evaluated for arbitrary
values of the independent variable.
Example:1 Enter:
First we declare x to be a symbolic syms x
g = x^2*cos(x)
variable and define g to be the
function given by g(x) = x2 cos(x).
Now enter:
diff(g, x)
Then enter:
diff(g, x, 2)
9
Example 2
Then enter:
subs(gp, x, 2.1)
>> syms x
>> f=x^3-cos(x);
>> g=diff(f)
g = ?
13
Example 5
ans = ?
14
Example 6
>> syms x
>> f=sin(exp(x));
>> diff(f)
ans = ?
15
Example 7
>> syms x y
>> f=x^3*y^4+y*sin(x)
>> diff(f,x)
ans = ?
>> diff(f,y)
ans = ?
16
Taylor Series
>> syms x
>> f = taylor(log(1+x))
f = ?
18
Taylor Series
Two methods for MATLAB to find the Taylor series expansion of f(x) at
x=a
Method 1: Using the general Taylor series summation and
programming it using function and for loops as explained in the
example 5.
Method 2: Using the MATLAB function : taylor, In general:
taylor(f,x,a,’Order’,n) returns the Taylor series expansion of f(x)
of degree n-1 at the base point x=a.
taylor(f,n) returns the Taylor series expansion of f(x) of order n-
1 at the base point x=0.
taylor(f,a) returns the Taylor series expansion of f(x) of order 5
at the base point x=a.
Note: f must be a symbolic expression representing f(x) 19
Example 8
20
Solution (1/2)
21
Solution (2/2)
22
Example 8(Another Method)
syms x;
f=sin(x);
t3=taylor(f,4); %3rd deg Taylor series at x=0
f_inline=inline(char(f)); %f:symbolic function
t3_inline=inline(char(t3));
fplot(f_inline,[-pi ,pi],'b');
hold on;
fplot(t3_inline,[-pi, pi],'r');
Legend('exact','Taylor 3');
xlabel('x');
ylabel('function')
23
Example 9
1
x2
Find the following integration using Taylor series of order 6: e dx
1
syms x;
f=exp(x^2);
t6=taylor(f,7); %Taylor series at x=0 of degree 6
int(t6,-1,1) %Integration of t6
Ans= ?
24
Example 9 Solution (2nd Method) (1/3)
i
x
The series expansion of ex is known: e
x
i 0 i!
2
The series expansion of ex can then be found by replacing x with
x² 2 i 2i
(x ) x
e
x2
i 0 i! i 0 i!
2
The integration of e
2 ican then
x
be2 i found 2 i 1
x x x
dx dx dx
2
x
e f ( x)
i 0 i! i 0 i! i 0 ( 2i 1)(i!)
1
x2
e
1
dx f (1) f ( 1) 25
Example 9 Solution (2nd Method) (2/3)
So, this method depends on writing the MATLAB function f(x) and finding
f(1) - f(-1)
function s=f(x)
s=0;
for i=0:100
s=s+ x^(2*i+1)/((2*i+1)*factorial(i));
end
≤1
syms x;
f=exp(x^2);
xd=-1:0.1:1;
yd=subs(f,xd);
plot(xd,yd,’r’);
hold on;
t6=taylor(f,7); %Taylor of degree 6
td=subs(t6,xd);
plot(xd,td,’b’);
xlabel(‘x’);
ylabel(‘functions’);
legend(‘f(x)’,’Taylor’)
27
Example 10
Find the taylor series expansion of degree 3 about the point x=1 of the
function 1
f ( x)
1 x
syms x;
f = 1/(1+x);
t3 = taylor(f,x,1,’Order’,4)
T3 = ?
28
Example 11
Find the taylor series expansion of degree 3 about the point x=0 of the
function 1
f ( x)
1 x
syms x;
f=1/(1+x);
t3=taylor(f,4)
t3 = ?
29