You are on page 1of 24

Revision Calculus

Study the content and do the excercises in this document. Read all the comments for the instructions. It will help
you to use Matlab more optimally.

To prevent confusion with notation, only the decimal point will be used when decimal numbers are presented.
Consider the following examples.

1.
The sequence differs from the sequence .

2.
The interval against which represents a four-number.

Executable instructions have to be in grey instruction blocks.

Make instruction blocks with "Code" under "Live editor".

Execute instructions with "Run Section" under "Live Editor".

Comments are indicated by "%".

The output can be viewed inline or on the right under "View".

When calculated values have to be reused, use the full value as saved in Matlab's memory, not a
rounded value.

Use decimal point for decimal numbers.

Table of Contents
1. Calculus concepts................................................................................................................................................2
1.1 Limit................................................................................................................................................................2
Example............................................................................................................................................................2
Example 1........................................................................................................................................................ 3
Exercise 1.........................................................................................................................................................5
1.2 Continuity....................................................................................................................................................... 5
Example 2........................................................................................................................................................ 6
Exercise 2.........................................................................................................................................................6
1.3 Row convergence.......................................................................................................................................... 6
Example 3........................................................................................................................................................ 7
Exercise 3.........................................................................................................................................................7
1.4 Differentiability................................................................................................................................................7
Example 4........................................................................................................................................................ 8
Exercise 4.........................................................................................................................................................8
Exercise 5.........................................................................................................................................................8
1.5 Riemann integral.......................................................................................................................................... 8
Example 5........................................................................................................................................................ 9
Example 6........................................................................................................................................................ 9
Exercise 7.......................................................................................................................................................10
Exercise 8.......................................................................................................................................................10
1.6 Average value.............................................................................................................................................. 10
Exercise 9.......................................................................................................................................................10
2. Calculus theorems............................................................................................................................................. 10
2.1 Mean Value Theorem.................................................................................................................................. 11

1
Example 7...................................................................................................................................................... 11
Example 8...................................................................................................................................................... 13
Exercise 9.......................................................................................................................................................14
2.2 Extreme value theorem............................................................................................................................... 15
Example 10.................................................................................................................................................... 15
Exercise 11.....................................................................................................................................................19
2.3 Intermediate value theorem......................................................................................................................... 19
Exercise 12.....................................................................................................................................................20
2.4 Mean value theorem for integrals................................................................................................................20
Exercise 13.....................................................................................................................................................20
2.5 Taylor theorem.............................................................................................................................................20
Exercise 14.....................................................................................................................................................21
Exercise 15.....................................................................................................................................................21
Exercise 16.....................................................................................................................................................22
3. Can you do the following in Matlab?.................................................................................................................22
Functions................................................................................................................................................................ 22
Riemann sum with right end points................................................................................................................... 23
Answers.................................................................................................................................................................. 23

1. Calculus concepts
The following Calculus concpets are important for the numerical handling of Mathematical problems.

1.1 Limit

Definition 1: A function f defined on a set X of real numbers has a limit L at if, given any real number ,
there exists a real number such that , whenever and (it is
indicated by Fig. 1.1, Faires/Burden).

We denote the limit of f at by

Example

2
Use the definition to show that , where

Answer: Let . We must just show that there exist a constant δ (that may depend on ϵ) such that
whenever . We have:

Take .

Example 1

Determine the limit of the function at the point by executing the instructions below.

% Limiete in Matlab word met die ingeboude Matlabfunksie limit in Symbolic Toolbox bereken.
% Limits in Maltab are calculated with the built-in Matlab function limit in Symbolic Toolbox.
clear
syms x
f(x)=cos(x^2)

f(x) =

L=limit(f(x),x,pi/4)

L =

% Om die reële waarde van die limiet te bepaal, gebruik eval().


% To determine the value of the limit, use eval().
Le=eval(L)

Le = 0.8157

% Om meer desimale waardes van die antwoord te sien, gebruik format long. Die verstekstelling i
% To see more decimals of the answer, use format long. The default setting is fornat short.
format long
Lel=eval(L)

Lel =
0.815704517422009

Look at the "workspace". The saved expressions and value are shown there.

eval(limit(6*log(exp(1)+x)*sin(pi*x)/(pi^2*x),x,0))

ans =
1.909859317102744

Remark: Defining and evaluating a function symbolically and numerically

3
% Funksie simbolies gedefinieer.
% Function defined symbolically.
clear
syms x
f(x)=sin(x^2)+2/x+x*exp(x)

f(x) =

f(pi/3)

ans =

eval(f(pi/3))

ans = 5.7837

% Funksie numeries gedefinieer.


% Function defined numerically.
g=@(z)sin(z.^2)+2./z+z.*exp(z)

g = function_handle with value:


@(z)sin(z.^2)+2./z+z.*exp(z)

g(pi/3)

ans = 5.7837

Let ook op na die verskil in afvoer vir die volgende instruksies. / Also note the difference in output for the
following instructions.

% Funksie numeries gedefinieer.


% Function defined numerically.
k=@(z)1./z

k = function_handle with value:


@(z)1./z

limit(k(x),x,0,'left')

ans =

limit(k(x),x,0,'right')

ans =

eval(limit(k(x),x,0,'left'))

ans = -Inf

eval(limit(k(x),x,0,'right'))

4
ans = Inf

k(0)

ans = Inf

% Funksie simbolies gedefinieer.


% Function defined symbolically.
h(x)=1/x

h(x) =

limit(h(x),x,0,'left')

ans =

limit(h(x),x,0,'right')

ans =

h(0)

Error using symengine


Division by zero.

Error in sym/subs>mupadsubs (line 160)


G = mupadmex('symobj::fullsubs',F.s,X2,Y2);

Error in sym/subs (line 145)


G = mupadsubs(F,X,Y);

Error in symfun/feval>evalScalarFun (line 42)


y = subs(formula(Ffun), Fvars, inds);

Error in symfun/feval (line 28)


varargout{1} = evalScalarFun(F, Fvars, varargin);

Error in symfun/subsref (line 183)


B = feval(A,inds{:});

Exercise 1

Copy and paste the instructions and determine the limit of the function at the point and 0.

% Kopieer, verander en voer die instruksies uit.


% Copy, change and execute the instruction.

1.2 Continuity

5
Definition 2: Let f be a function defined on a set X of real numbers and . Then f is continuous at if
both and exist, and

The function f is continuous on the set X if it is continuous at each number in X.

Example 2

Determine whether the function is continuous in the point by executing the instructions below.

% Bepaal kontinuïteit.
% Determine continuity.
clear
syms x
f(x)=sin(x^2)
%evaluate the lim f(x) as x approaches pi/3
eval(limit(f(x),x,pi/3))
%evaluate f(pi/3)
eval(f(pi/3))
%compare
eval(limit(f(x),x,pi/3))-eval(f(pi/3))

Exercise 2

Copy and paste the instructions and determine whether the function is continuous in the point 100.

% Kopieer, verander en voer die instruksies uit.


% Copy, change and execute the instruction.

Copy and paste the instructions and determine whether the function is continuous in the point 0.

% Kopieer, verander en voer die instruksies uit.


% Copy, change and execute the instruction.

The output for the limit is "NaN" which means "Not a Number". The limit therefore does not exists. From the
error message in the output, the function does not exist in the point 0 since it causes division by zero which is
not defined. The function is therefore discontinous in the point 0.

1.3 Row convergence


A row or sequence has elements

6
is indicated by .

Definition 3: An infinite row converges to the number x if for any real value , there exists a positive

integer such that whenever . Notation

Example 3
Consider the row

Determine the limit of the row by executing the instructions below.

% Bepaal die limiet van 'n ry.


% Determine the limit of a row.
clear
syms n
limit(1+1/n,n,inf)

ans =

limit(1+1/n,n,0)

ans =

Exercise 3
Copy and paste the intstructions and determine the limit of

% Kopieer, verander en voer die instruksies uit.


% Copy, change and execute the instruction.

1.4 Differentiability
A function f defined on the real set X with is differentiable in the point if

exists. In this case, the drivative of f at is given by

7
(Fig 1.2, Faires/Burden)

Remark: If a function f defined on the real set X with is differentiable in the point , then it is continuous
in the point .

Example 4
Use the definition to show that is differentiable in the point , and find .

Answer:

Since the limit exists, then is differentiable in the point , and .

Exercise 4

Determine whether the function is differentialble in the point 2.

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.
clear
syms x
f(x)=1/x
limit((f(x)-f(2))/(x-2),x,2)

Exercise 5

Determine whether the function is differentialble in the point 0.

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.

1.5 Riemann integral

8
The Riemann integral of the function f over the interval is the following limit, provided it exists (Fig. 1.6,
Faires/Burden)

with and .

In practise and are chosen at fixed point, for example

Example 5
A Matlab fuction which calculate Riemann sums is given at the end of this document. To calucluate the
Riemann sum for as defined above, over the interval for 10 subintervals, the Matlab function can
be called by executing the instruction below. Study the code in the Matlab function and execute the instruction.

% Bepaal 'n Riemannsom met 'n Matlabfunksie.


% Determine a Riemann sum with a Matlab function.
h=@(x)exp(2*x).*tan(x)./x.^3

h = function_handle with value:


@(x)exp(2*x).*tan(x)./x.^3

R1=riemannsum(h,0.4,1.4,10)

R1 =
15.083127439231083

Example 6

9
The exact integral value can be determined by using the built-in Matlab function integral. To calucluate the
integral for as defined above, over the interval , the Matlab functions can be called by executing
the instruction below. Determine the integral and compare the answer with the Riemann sum.

% Bepaal 'n integraal oor 'n interval.


% Determine an integral over an interval.

Exercise 7
Calucluate the Riemann sum for as defined above, over the interval for 100 subintervals and
compare the value with the integral value.

% Gee die instruksie en voer dit uit.


% Give the instruction and execute it.

Exercise 8
Calucluate the Riemann sum for over the interval for 15 and 200 subintervals and compare
the value with the integral value.

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.

1.6 Average value


The average value of the function f over the interval is

Exercise 9
Calucluate the average value for over the interval .

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.

2. Calculus theorems
The following Calculus theorems are important for the numerical handling of Mathematical problems.

Consider a real set X. The set of all functions having order continuous derivatives on X is indicated by
.

means the set of all functions continuous on .

10
means the set of all functions with continuous first order derivatives (and therefore the function as well)
on .

2.1 Mean Value Theorem


Theorem: If and differentiable on , there exists a number such that (Fig. 1.3, Faires/
Burden)

Example 7

Consider the function . Sketch the function on the interval and the secant through the

eindpoints on the same set of axes. Read the comments and execute the instructions.

% Teken funksies met Symbolic Toolbox.


% Sketch functions with Symbolic Toolbox.
clear
syms x
f(x)=sin(x^2)

f(x) =

df(x)=diff(f(x))

df(x) =

g(x)=(f(pi/2)-f(0))/(pi/2-0)*(x-0)+f(0)

g(x) =

fplot(f(x),[0 pi/2])

11
hold on
fplot(g(x),[0 pi/2])
hold off

% Teken funksies met numeries. Gebruik verkieslik hierdie metode aangesien Matlab die instruksi
% Sketch functions with numerically. Preferably use this method since Matlab execute the instru
clear
f=@(x)sin(x.^2)

f = function_handle with value:


@(x)sin(x.^2)

g=@(x)(f(pi/2)-f(0))/(pi/2-0).*(x-0)+f(0)

g = function_handle with value:


@(x)(f(pi/2)-f(0))/(pi/2-0).*(x-0)+f(0)

fplot(f,[0 pi/2])
hold on
fplot(g,[0 pi/2])
hold off

12
Example 8

To determine the point c for the function over the interval , the following instructions can be

used. Read the comments and execute the instructions. Note the three different methods and their output.

% solve is 'n ingeboude Matlabfunksie in Symbolic Toolbox en los die vergelyking simbolies op.
% solve is a built-in Matlab function int Symbolic Toolbox and solve the equation symbolically.
% Nie alle vergelykings kan simbolies opgelos word nie.
% Not all equations can be solved symbolically.
% vpasolve is 'n ingeboude Matlabfunksie wat 'n vergelyking numeries oplos vir 'n simboliese ve
% vpasolve is a built-in Matlab function that solves an equation numerically for a symbolic var

% Metode / Method 1
clear
syms x c
f(x)=sin(x^2)

f(x) =

df(x)=2*x*cos(x^2)

df(x) =

solve(df(c)==(f(pi/2)-f(0))/(pi/2-0))

13
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
ans =

% Metode / Method 2
clear
syms x c
f(x)=sin(x^2)

f(x) =

df(x)=2*x*cos(x^2)

df(x) =

vpasolve(df(c)==(f(pi/2)-f(0))/(pi/2-0))

ans =

% Metode / Method 3
clear
syms c
f=@(x)sin(x.^2)

f = function_handle with value:


@(x)sin(x.^2)

df=@(x)2*x.*cos(x.^2)

df = function_handle with value:


@(x)2*x.*cos(x.^2)

vpasolve(df(c)==(f(pi/2)-f(0))/(pi/2-0))

ans =

The third method is the prefered method since Matlab execute instructions faster when functions are defined
numerically.

Exercise 9

Consider the function . Sketch the function on the interval and the secant through the eindpoints

on the same set of axes. Also determine the point c for the function over the interval . Compare

your answer with your sketch.

% Kopieer, verander en voer die instruksies uit.


% Copy, change and execute the instruction.

14
2.2 Extreme value theorem
Theorem: If , then there exist numbers such that

, for all

If in addition f is differentiable on , the m and n are where or at the eindpoints of .

Example 10
Consider the function and differentiable on defined by

Local minimum and maximum values will occur at and global minimum and maximum values at
or at the endpoints of the interval. Matlab can be used to determine these values on the interval
as follows. Execute the instructions.

% Bepaal ekstreemwaardes.
% Determine extreme values.
clear
% Funksie / Function
f=@(x)9*cos(x)-x.^2+sqrt(x)

f = function_handle with value:


@(x)9*cos(x)-x.^2+sqrt(x)

fplot(f,[0 7])
grid on

15
% Eerste afgeleide / First derivative
df=@(x)-9*sin(x)-2*x+0.5./sqrt(x)

df = function_handle with value:


@(x)-9*sin(x)-2*x+0.5./sqrt(x)

fplot(df,[0 7])
grid on

16
fplot(df,[4 5])
grid on

17
Zeros for is expected more or less at 0.1, 4.4 and 4.6. Execute the instructions.

syms c

df = function_handle with value:


@(x)x.*exp(x)+sin(2*x)
f = function_handle with value:
@(x)(1+x).*exp(x)+2*cos(2*x)

C=vpasolve(df(c)==0)

C =

Double click on the variable C in the Workspace to see the full value.

This solution is a complex number not corresponding to the graph of . Although vpasolve is more versatile
than solve, it does not always give accurate results. Matlab also uses other numerical methods to solve
equations or determine zeros.

% fzero is 'n numeriese metode wat een nulpunt bepaal indien 'n geskatte waarde naby die nulpun
% fzero is a numerical method which determines one zero if an estimate value is given.
c1=fzero(df,0.1)

c1 = 5.5220e-30

c2=fzero(df,4.4)

c2 = 4.1434e-23

c3=fzero(df,4.6)

c3 = 1.0538e-22

The extreme values can now be determined.

% Tweede afgeleide / Second derivative


ddf=@(x)-9*cos(x)-2-0.25./x./sqrt(x)

ddf = function_handle with value:


@(x)-9*cos(x)-2-0.25./x./sqrt(x)

f0=f(0)

f0 = 3

fc1=f(c1)

fc1 = 3

fc2=f(c2)

fc2 = 3

fc3=f(c3)

fc3 = 3

18
f7=f(7)

f7 = 8.7733e+03

dfc1=df(c1)

dfc1 = 1.6566e-29

dfc2=df(c2)

dfc2 = 1.2430e-22

dfc3=df(c3)

dfc3 = 3.1615e-22

ddfc1=ddf(c1)

ddfc1 = -1.9266e+43

ddfc2=ddf(c2)

ddfc2 = -9.3735e+32

ddfc3=ddf(c3)

ddfc3 = -2.3109e+32

From these answers, the following can be deducted:

: local and global maximum value.

: local minimum value

: local maximum value

: global minimum value

Exercise 11
Follow the same steps as above and use the Matlab function fzero to determine the extreme values for
.

% Kopieer, verander en voer die instruksies uit.


% Copy, change and execute the instruction.

2.3 Intermediate value theorem


Theorem: If and K is any number between and , then there exists a number
such that (Fig 1.5, Faires/Burden).

19
Exercise 12
Determine whether the function has a zero on the interval without skethcing the function.

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.

2.4 Mean value theorem for integrals


If , then there exists a number such that (Fig 1.7, Faires/Burden)

Exercise 13

Consider the function on the interval . Determine the point according to the

expression above. Sketch the function over the interval en on the same set of axes.

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.

2.5 Taylor theorem

20
Theorem: Suppose , exists on , and . For each there exists a
number between and x such that

where

is the Taylor polynomial for f about and

the remainder term (or troncated error) associated with

Remark: The number is dependent on the x where is determined, but not easy to determine. In
practice an absolute maximum is determined on an interval.

Exercise 14

Determine , , , and for the function about the point and sketch

them together with on the same set of axes for . Sketch them one by one together with the function to
see the build up.

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.

Exercise 15

Use some of your results of Exercise 14 for the function about the point to determine the

following values.

1.
for

2.
for

3.
for

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.
% WENK: stoor die waardes vir x in 'n vektor en bereken al die waardes met een instruksie.
% HINT: save the values for x in a vector and compute all the values at once
% X=[0.001 1 1.2 1.5 pi/2 1.7 2 pi 5]
% f(X)

21
% P3(X)
% abs(f(X)-P3(X))
% Dubbelkliek op die veranderlikes in Workspace om al die waardes te sien.
% Double click on the varialbes in Workspace to see all the values.
% Kliek op die afvoer om al die waardes te sien.
% Click on the output to see all the values.

Exercise 16

Determine for the function about the point and use it to determine the following.

1. The maximum absolute error for and compare your answer with above. (Determine

.) HINT: Sketch over the interval to get an estimate maximum value for

2. The maximum absolute error for on the interval and compare your answer with

for above. (Determine .) HINT: Sketch and

over the interval to get estimate maximum values for and .

% Gee die instruksies en voer dit uit.


% Give the instructions and execute them.

3. Can you do the following in Matlab?


1. Function define
2. Function value calculate
3. Limit caluculate
4. Riemann sum calculate
5. Integral over an interval
6. Function's graph sketch
7. Simple equations solve
8. Zeros determine
9. Extreme values determine
10. Value of Taylor polynomial in a point determine
11. Absolute error calculate
12. Maximum remainder terms at a point and over an interval determine

Functions

22
Copy and save the code of the function in a new "script" as name.m where "name" is the name of the function.
You can then call and execute it directly from the "Command Window" if it is saved in the "Current Folder", the
folder as indicated just below the toolbar. You can change the "Current Folder" by clicking on it. The function
can also be copied in a "script" or "live script" together with other instructions, including the instruction calling
the function. It just has to be at the end of the document as done here.

Riemann sum with right end points


function R=riemannsum(f,a,b,n)
% Riemannsom met regtereindpunte / Riemann sum with right end points
% f: Funksie in @(x)f(x) formaat / Function in @(x)f(x) format
% byvoorbeeld / for example @(x) x.^5+2./y.^3+3.*y
% a: Beginpunt van interval / Starting point of interval a
% b: Eindpunt van interval / Endpoint of interval b
% n: Aantal deelintervalle / Number of subintervals
% Waarde word in veranderlike R gestoor / Value is saved in variable R
format long
dx=(b-a)/n;
R=0;
for k=1:n
R=R+f(a+k*dx)*dx;
end
end

Answers
1. 6; NaN
2. Continuous; Discontinous does not exist

3. e
4.

5. Non-differentiable
6. 25.7963
7. 13.9867
8. 0.7062; 0.7692; 0.7727
9. 0.2459
10. 3.4641
11. g(1.9173) = 3,7561 local maximum value; g(4.9338) = -8.9935 local and global minimum value;
g(8.02861) = 16.7459 local and global minimum value
12. f(1) = 0.8415 > 0, f(2) = -0.7568 < 0, f continuous, thus there exists a c so that f(c) = 0 on [1,2].
13. 0.7452
14. Note how the polynomials follows larger parts of the function as the terms increase.
15. (1) 0.000; 0.8415; 0.9915; 0.7781; 0.6243; 0.2489; -0.7568; -0.4303; -0.1324. (2) -13.0700; 0.3809;
0.8975; 0.7779; 0.6243; 0.2472; -0.9764; -4.7139; 30.5064. (3) 13.0700; 0.4606; 0.0940; 0.0001; 0;
0.0018; 0.2196; 4.2836; 30.6387

23
16. (1) For : compare with . (2) For
: for . Compare with for .

24

You might also like