0% found this document useful (0 votes)
10 views29 pages

Chapter 3

Uploaded by

Syed Sadiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views29 pages

Chapter 3

Uploaded by

Syed Sadiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 3: Mathematical

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

>> diff(f) % 1st derivative


ans = ?

>> diff(f,2) % 2nd derivative


ans = ?

>> diff(f,3) % 3rd derivative


ans = ?

>> diff(f,4) % 4th derivative


ans = ?

4
Partial Differential Equations

Sometimes, an expression can have multiple variables.


To compute the derivative of the function with respect to one
variable, use a second parameter to the diff function:

>> 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)

How would you calculate the third


derivative?

9
Example 2

To calculate the derivative of an expression that you have not yet


entered, just replace g by the expression. For example, enter:
diff(x^3-x^2+2, x)

Now insert a symbolic constant a into the expression. Enter:


syms a
diff(x^3 - a*x^2 + 2, x)

Then change the final x to an a. That is, enter:


diff(x^3 - a*x^2 + 2, a)

What is the role of the symbol after the comma in the


differentiation expression?
10
Example 3

Evaluate the derivative dg/dx at x = 2.1.


Enter:
gp=diff(g, x)

Then enter:
subs(gp, x, 2.1)

Alternatively, you could enter:


subs( diff(g, x), x , 2.1 )
eval(subs( diff(g, x), x , 2.1 ))
11
Check your understanding so far by using
MATLAB to calculate the second derivative
of
tan(x6 - 3x + 5) at x = 3/2.
Example 4

Calculate the derivative of the function F(x)=x3 - cos(x)

>> syms x
>> f=x^3-cos(x);
>> g=diff(f)

g = ?

13
Example 5

Obtain the derivative of the following function according to y:


F(x,y)=x² +(y+5)3

Matlab command entries:


>> syms x y
>> f=x^2+(y+5)^3;
>> diff(f,y)

ans = ?

Note that in this case, the command diff(f,y) is equivalent to

14
Example 6

Find the derivative of f(x)=sin(ex).

>> syms x
>> f=sin(exp(x));
>> diff(f)

ans = ?

15
Example 7

To compute the partial derivative of an expression with respect to some


variable we specify that variable as an additional argument ‘diff’
f(x,y)=x3y4+ysin(x)

>> syms x y
>> f=x^3*y^4+y*sin(x)
>> diff(f,x)

ans = ?

>> diff(f,y)
ans = ?
16
Taylor Series

The Taylor series is a representation of a given function as an


infinite sum of terms calculated from its derivatives values at a
single point.
If the series is centered at zero, the Taylor series is Maclaurin
series.
It is a common practice to use a finite number of terms to
approximate the function.
Taylor series of order 6 is an approximation of f(x) as a polynomial
of degree 5. m

The general expression of Taylor series for an



f ( xanalytic m
 a)
f
)  ( x function
(a)
f(x)
about the base point x=a is: m 0 m!
17
Taylor Command

Use the "taylor" function to create a 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

Given a function f(x)=sin(x). Write a program that finds the Taylor


series expansion of f(x) at x=0 of degree 3.
Plot f(x) and its Taylor series approximation of degree 3 on the
same graph for x in the range [-pi,pi]
Add Taylor series of degrees 5, 7 to the same plot.
Which Taylor series better approximates f(x).
What is your conclusion?

20
Solution (1/2)

Open a script file example6.m


syms x;
f=sin(x);
t3=taylor(f,4); %Taylor series at x=0 of degree 3
xd=-pi:pi/20:pi;
yd=subs(f,x,xd); %This evaluates f(x) at xd (symbolic).
td3=subs(t3,x,xd); %This evaluates t(x) at xd.
plot(xd,yd,’y’);
hold on;
plot(xd,td3,’m’);

21
Solution (2/2)

t5=taylor(f,6); %5th deg Taylor series at x=0


t7=taylor(f,8); %7th deg Taylor series at x=0
td5=subs(t5,x,xd);
td7=subs(t7,x,xd);
plot(xd,td5,’r’);
hold on;
plot(xd,td7,’b’);
Legend(‘exact’,’Taylor 3’,’Taylor 5’,’Taylor
7’);
xlabel(‘x’);
ylabel(‘function’)

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

In the command window, type:


>> int_result=f(1)-f(-1)
int_result=?
26
Example 9 Solution (2nd Method) (3/3)

approximation t6(x) on the same graph for -1 ≤ x


In the previous example, plot f(x) and its Taylor

≤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

You might also like