Integral Calculus and Differential Equations Using Mathematica
Integral Calculus and Differential Equations Using Mathematica
Equations using
MATHEMATICA
PRACTICAL INTRODUCTION TO
MATHEMATICA
1.1 CALCULATION NUMERIC WITH MATHEMATICA
Most of the themes of the classical numerical calculus, are treated in this software. It
supports matrix calculus, statistics, interpolation, fit by least squares, numerical integration,
minimization of functions, linear programming, numerical algebraic and differential
equations resolution and a long list of processes of numerical analysis that we’ll see as the
successive issues of this book.
Here are some examples of numerical calculus with Mathematica. (As we all know, to
get the results necessary type mayusculas+enter once written corresponding expressions)
(1) We can simply calculate 4 + 3 and get as a result 7, to do this, just type 4 + 3 (and
then shift + Enter).
In [1]: = 4 + 3
Out [1] = 7
(2) Also we can get the exact value of 3 high at 100, without having previously set
precision, just for this purpose press 3 ^ 100.
In [2]: = 3 ^ 100
Out [2] = 515377520732011331036461129765621272702107522001
(3) Also we can use the N function to pass the result of the operation immediately prior
to scientific notation. To do this, type N [%] (symbol % we use to refer to the immediately
preceding calculation).
In [3]: = N [%]
47
Out [3] = 5.153775207320114 10
(4) Also we can perform operations with a fixed degree of precision. If we find the
square root of 5 with 25 digits, simply enter the expression N [Sqrt [5], 25].
(5) Also we can work with complex numbers. We will get the result of the operation (2 +
3i) raised to 10, by typing the expression (2 + 3I) ^ 10.
In [5]: = (2 + 3 * I) ^ 10
Out [5] = 341525 145668 I
(6) Also we can calculate the value of the Bessel function in section 13.5. This type
BesselJ [0,13.5].
(7) Also can calculate the value of Rieman function Z at the point (1/2 + 13i) with 15
digits. Just press N [Zeta [1/2 + 13I], 15].
(8) Also we can perform numeric integrals. To calculate the integral between 0 and p of
(SIN(x)) sine function type expression NIntegrate [Sin [no [x]], {x, 0, Pi}].
These themes will be treated more thoroughly in successive chapters throughout the
book.
1.2 SYMBOLIC CALCULATION WITH MATHEMATICA
Mathematica perfectly handled the symbolic mathematical computation, manipulating
formulae and algebraic expressions easily and quickly and can perform the majority of
algebraic operations. You can expand, factor and simplify polynomials and rational and
trigonometric expressions, you can find algebraic solutions of polynomial equations and
systems of equations, can evaluate derivatives and integrals symbolically and find functions
solution of differential equations, you can manipulate powers, limits and many other facets
of algebraic mathematics series.
1) We can raise the bucket the following algebraic expression: (x + 1) (x+2) (x+2) ^ 2.
This is done by typing the following expression: Expand [((x + 1) (x+2) (x+2) ^ 2) ^ 3]. The
result will be another algebraic expression:
23
Out [1] = 8- 12 x- 6 x- x
2) We can factor the result of the calculation on the previous example by typing Factor
[%]
3
Out [2] = (2 + x)
In[3] := Integrate[x^2*Sin[x]^2, x]
Out[3]=
3 2
x x Cos [2 x] (1- 2 x) Sin [2 x]
‑‑ ‑ ‑‑‑‑‑‑‑‑‑‑– + ‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑–—
6 4 8
4) We can find the derivative of the result of the integral above by typing D [% x]
In[4] := D[%, x]
Out[4] =
2 2
x Cos[2 x] (1 ‑ 2 x ) Cos[2 x]
‑‑ ‑ ‑‑‑‑‑‑‑‑‑‑– +–-‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
2 4 4
5) We can simplify the result of the derivative before typing Simplify [%]
In[5] := Simplify[%]
2 2
Out[5] = x Sin[x]
6) We can develop in power of order 14 series the result from the previous example by
typing Series [% {x, 0.14}]
6 8 10 12 14
4 x 2x x 2x 2x 15
x ‑ ‑‑ + ‑‑‑‑ ‑ ‑‑‑— +–-‑‑‑‑ ‑ —‑‑‑‑‑‑— + O[x]
3 45 315 14175 467775
7) We can solve the equation 3ax - 7 x ^ 2 + x ^ 3 = 0 (a, is a parameter) by typing Solve
[3ax 7 x ^ 2 + x ^ 3 = 0]
Out[7] =
1/3
7 49 2
{{x ‑>-‑ + ‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑––––––— +
3 2 1/3
3 (686 ‑ 81 ax + 9 Sqrt[‑1372 ax + 81 ax ] )
2 1/3
(686 ‑ 81 ax + 9 Sqrt[‑1372 ax + 81 ax ] )
+ ‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑––––––-},
1/3
32
1/3
7 I ‑49 2
{x ‑> ‑ + ‑ Sqrt[3] (‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑–––––––– +
3 2 2 1/3
3(686‑81ax+9Sqrt[‑1372ax+81ax ] )
2 1/3
(686 ‑ 81 ax + 9 Sqrt[‑1372 ax + 81 ax ] )
+ ‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑–––––––) ‑
1/3
32
1/3
49 2
- (‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑–––––––— +
2 1/3
3 (686 ‑ 81 ax + 9 Sqrt[‑1372 ax + 81 ax ] )
2 1/3
(686 ‑ 81 ax + 9 Sqrt[‑1372 ax + 81 ax ] )
+ ‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑–––––––-) / 2},
1/3
32
1/3
7 I ‑49 2
{x ‑> ‑ ‑ ‑ Sqrt[3] (‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑––––––- +
3 2 2 1/3
3 (686‑81ax+9 Sqrt[‑1372ax+81ax ] )
2 1/3
(686 ‑ 81 ax + 9 Sqrt[‑1372 ax + 81 ax ] )
+ ‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑–––––––-) ‑
1/3
32
1/3
49 2
‑ (‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑––––––––- +
2 1/3
3 (686 ‑ 81 ax + 9 Sqrt[‑1372 ax + 81 ax ] )
2 1/3
(686 81 ax + Sqrt 9 [1372 ax + 81 ax])
+ ‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑–––––––) / 2}}
1/3
32
8) We can find five complex solutions of the equation x ^ 5 + 2 x + 1 = 0 by typing NSolve
[x ^ 5 + 2 x + 1 = 0, x]
2 3
Out [11] = (1 4755 x + 255600 x - 378000 x ) / 378000
12) We can find all the permutations of the three elements {e, f, g}
tecleando Permutations[{e, f, g}]
13) We can define the function f (x) = x ^ 2, variable x, the explicit form:
In [13]: = f [x_] = x ^ 2;
14) We can create pure functions that do not have explicit typing for example, definitions
Map [Function [x, 1 + x ^ 2], {a, b, c, d}]
2 2 2 2
{1 + a, 1 + b, 1 + c, 1 + d }
We can represent the function Seno (1/x) for x ranging between 0 and pi typing Plot
[Sin[1/x], {x, 0, Pi}]. See Figure 1.1
Figure 1.1
We can give options of framework and grid to the graph above, by typing Show [%,
Frame > True, FrameLabel > {“time”, “Signal”}, GridLines > Automatic]. See Figure 1.2.
Figure 1.2
We can generate a graphic of contour for the sine function Sin(x+Sin(y)), by typing
ContourPlot [Sin[x + Sin[y]], {x, 2, 2}, {y, 2, 2}]. See Figure 1.3.
Figure 1.3
We can generate a three-dimensional graph, typing Plot3D [Cos [x + Cos [y]], {x, 3, 3},
{y, 3, 3}]. See Figure 1.4.
These 3D graphics allow perfection to get an idea of the figures in space, and are very
helpful in visually identifying intersections between different bodies, generation of
developments of all kinds and volumes of revolution.
Figure 1.4
We can combine in the same graph the two previous charts by typing Show [% %].
In Mathematica can write procedural programs, i.e., define a sequence of steps standard
to run. As in C or Pascal, a Do, For, or While using repetitive calculation can be performed.
The language of Mathematica also includes structures such as If and Which Swiych.
Mathematica also supports different logic, such as And, Or, Not and Xor functions.
Mathematica supports procedural programming (with entry iterative, recursive, loops…),
functional programming (pure functions…), and the object-oriented programming.
Here are two simple examples of programs. The first calculates the sign of a number, and
the second calculates the median of a set of numbers.
In[1]:= sign[x_] :=
Which[
x < 0., -1,
x == 0., 0,
x > 0, 1
]
In[2]:= sign[3]
Out[2] =1
In[3]:= mediana[lista_List] :=
Block[{
lo,
long
},
long =Length[lista];
lo = Sort[lista];
If[
OddQ[long],
the [[(long + 1) / 2]],
(the [[long/2]] + the [[long/2 + 1]] / 2).
]
]
The most commonly used Mathematica commands for integral calculus are:
Computes
Integrate[f[x,y],{x,a,b},{y,c,d}}]
NIntegrate[f[x,y],{x,a,b},{y,c,d}}]
Computes, using approximate methods, the definite double integral
Integrate[f[x,y,…, z],{x,a,b},{y,c,d},…,{z,e,f}]
Computes
NIntegrate[f[x,y,…, z],{x,a,b},{y,c,d},…,{z,e,f}]
In[1]:= Integrate[1/(x^2-1), x]
Log[1 - x] Log[1 + x]
–––- - –––-
2 2
1
––-
2
-1 + x
In the two previous examples we have shown that differentiation and integration are
inverse functions.
In[4]:= Integrate[x^n,x]
1+n
x
––
1+n
We have seen how Mathematica lets you enter parameters on the whole, they are
treated as generic constant.
In[5]:= Integrate[(x^2)(f’[x]^3),f[x]]
2 3
x f[x] f’[x]
In[6]:=Integrate[f’[x],x]
f[x]
In[7]:= Integrate[x^3,{x,a,b}]
4 4
-a b
–+—
4 4
In[8]:= Integrate[x^3+y^3,{x,0,a},{y,0,b}]
4 4
a b ab
–- + –-
4 4
In[9]:= Integrate[x^3+y^3,{x,0,a},{y,0,x}]
5
a
—
4
In these last examples definite integrals have been solved, one of which has a variable
limit of integration, which is perfectly valid with Mathematica.
2.1 INDEFINITE INTEGRALS
2.1.1 Inmediate integrals
In the examples that follow, we demonstrate the ease with which Mathematica can
find integrals that are not so obvious using classical techniques. Mathematica is able to find
indefinite integrals involving simple, rational, exponential, logarithmic, trigonometric and
inverse trigonometric functions.
In[1]:= Integrate[3x^5+7x+2,x]
2 6
7x x
2 x + –- + —
2 2
In[2]:= Integrate[1/(Sqrt[x^2+1]),x]
ArcSinh[x]
In[3]:= Integrate[x^3 Sqrt[1+x^4],x]
4
1 x 4
(- + —) Sqrt[1 + x ]
6 6
In[4]:=Integrate[1/((2+x)Sqrt[1+x]),x]
2 ArcTan[Sqrt[1 + x]]
In[5]:= Integrate[1/((1-x^2)Sqrt[1-x^2]),x]
2
x Sqrt[1 - x ]
-(––––—)
2
-1 + x
3
Cos[x ]
Integrate[––––, x]
Sqrt[Sin[x]]
We have here an example of an integral that does not solve directly Mathematica.
arc Sin[2 x]
––––
2
In[4]:= Integrate[Sin[Log[x]],x]
In[5]:= Integrate[Log[x]/Sqrt[x],x]
-4 Sqrt[x] + 2 Sqrt[x] Log[x]
In[2]:= Integrate[(x^3)(E^(2x)),x]
2 3
2x 3 3x 3x x
E (-(-) + – - –- + —)
8 4 4 2
In[3]:= Integrate[Cos[x]^11/Sin[x]^2,x]
-4 Log[1 - x] Log[1 + x]
–– - –––- + –––-
-1 + x 2 2
In[2]:= Integrate[x^2/(a^4-x^4),x]
x
-ArcTan[-]
a Log[-a + x] Log[a + x]
–––- - –––— + –––-
2a 4a 4a
In[3]:= Integrate[(x-1)/((x^3))(x^2+1),x]
1 1
–- - - + x - Log[x]
2 x
2x
3 25
x Sqrt[(a + b x ) ]
––––––—
24
3 a (a + b x )
In[5]:= Integrate[Sqrt[-x^2-2x+1],x]
1 x 2 -1 - x
(- + -) Sqrt[1 - 2 x - x ] - ArcSin[––-]
2 2 Sqrt[2]
2.2 INTEGRATION BY SUBSTITUTION (OR CHANGE
OF VARIABLES)
The method is to find a function x = g (t) which replaces x under the integral sign,
converting the integral into another simpler integral in the variable t. The new integrand will
in the majority of cases be immediately integrable or will be a rational function.
· is differentiable:
Once the transformed integral has been calculated as a function of , we must substitute
to give the final solution as a function of .
The substitution method is one of the most wide ranging of integral calculus, because of
the great variety of substitutions that can be used, depending on the type of function that
appears under the integral sign. However, it can easily go wrong in the sense that the
transformed integral may be more difficult than the original integral, so some care is needed
to choose an appropriate substitution.
2.2.1 Exponential, logarithmic, hyperbolic and inverse
circular functions
R(ax) ax = t x = (1/lna)lnt
R(x,ax) a = tx x = (1/lna)lnt
R(ex) ex = t x = lnt
R(x,ex) ex = t x = lnt
R(x,lnx) lnx = t x = et
R(x,senhx) ex = t x = lnt
R(x,coshx) ex = t x = lnt
R(x,tanhx) ex = t x = lnt
R(sinhx,coshx) ex = t x = lnt
In[1]:= f[x_]=(1/(1+E^x))Dt[x]
Dt[x]
––
x
1+E
In[2]:= f[Log[t]]
Dt[t]
–––
t (1 + t)
In[3]:= Integrate[%/Dt[t],t]
Log[t] - Log[1 + t]
In[4]:= % /. t->E^x//Simplify
x x
Log[E ] - Log[1 + E ]
In[1]:= f[x_]=(x/(Cosh[x]+Sinh[x]))Dt[x]
x Dt[x]
–––––—
Cosh[x] + Sinh[x]
In[2]:= f[Log[t]]
Dt[t] Log[t]
––––––—
2 2
-1 + t 1 + t
t (––- + ––)
2t 2t
In[3]:= Integrate[%/Dt[t],t]
1 Log[t]
-(-) - ––
t t
In[4]:= % /. t->E^x//Simplify
x
1 + Log[E ]
-(–––—)
x
E
In[1]:= f[x_]=(ArcSin[x]/(1-x^2)^(3/2))Dt[x]
ArcSin[x] Dt[x]
–––––
2 3/2
(1 - x )
In[2]:= f[Sin[t]]
In[3]:= Integrate[%/Dt[t],t]
2
Log[1 - Sin[t] ] ArcSin[Sin[t]] Sin[t]
–––––- + –––––––
2 2
Sqrt[1 - Sin[t] ]
In[4]:= % /. t->ArcSin[x]//Simplify
2
x ArcSin[x] Log[1 - x ]
–––– + –––—
2 2
Sqrt[1 - x ]
t
For the first integral we make the change Log [x]=t (x=E )
Dt[x] Log[2 x]
––––—
x Log[4 x]
In[2]:= f[E^t]
t
Dt[t] Log[2 E ]
–––––
t
Log[4 E ]
In[3]:= Integrate[%/Dt[t],t]
t t t
t + (Log[2 E ] - Log[4 E ]) Log[Log[4 E ]]
In[4]:= % /. t->Log[x]//Simplify
In[1]:= f[x_]=(1/(x(4-Log[x^2])))Dt[x]
Dt[x]
–––––
2
x (4 - Log[x ])
In[2]:= f[E^t]
Dt[t]
––––-
2t
4 - Log[E ]
In[3]:= Integrate[%/Dt[t],t]
2t
-Log[4 - Log[E ]]
––––––-
2
In[4]:= % /. t->Log[x]//Simplify
2
-Log[4 - Log[x ]]
–––––—
2
2.2.2 Irrational functions, binomial integrals
The following chart summarizes the substitutions to be made, depending on the function
to integrate, where R is a rational function of its corresponding argument.
Function a integrate Substitution Inverse function
M M
m/n p/q ax+b = t t‑b
R (x, (x, (ax+b),…,(ax+b) ) x = ──────
M = mcm(n,…,q) a
M
m/n p/q r/s x=t
R (x, x , x , ….,x )
M = mcm(n,q,..,s)
1 1 1
───────────────────── ───── = t x=a+─
p 2 1/2 x‑a t
(x‑a) (ax +bx +c)
2
2 1/2 2 1/2 t‑c
R (x, (ax +bx + c) a>0 (ax +bx+c) = xÖa +t x= ────────
b ‑ 2tÖa
2 1/2 Öc
R (x, (‑ax +c) ) x = ── SIN(t)
Öa
2 1/2 Öc
R (x, (ax ‑c) ) x = ── SEC(t)
Öa
2 1/2 Öc
R (x, (ax +c) ) x = ── TAN(t)
Öa
BINOMIAS:
m np 1/n
x (a+bx ) with p integer x=t
s 1/n
m np m+1 n s (t ‑a)
x (a+bx ) with ─── integer a+bx = t x = ─────────
n s=denominator of p 1/n
b
s
m np m+1 -n s (t ‑b)
x (a+bx ) with ─── +p integer ax+b = t x = ─────────
n s=denominator of p -1/n
a
Exercise 2-7. Find the following integrals:
2 ½2
ò 1/[(x -x+1) x ]
ò x/(x+1)
2 ½2
ò 1/[(x +4) x ]
The first integral is an binomial integral with (8 + 1) / 3 = 3, which is an integer. After the
change to be made will be:
3 4 4 3 1/4
3+5x=t (x=(t - 3)/5 y t=(3 + 5 x ) )
In[1]:= f[x_]=(x^8/(3+5x^3)^(1/4))Dt[x]
8
x Dt[x]
––––-
3 1/4
(3 + 5 x )
In[2]:= f[((t^4-3)/5)^(1/3)]//Simplify
3 42
4 t (-3 + t ) Dt[t]
–––––––
4 1/4
375 (t )
In[3]:= Integrate[%/Dt[t],t]//PowerExpand
4 8
3 4 8t 4t
t (– - –- + –-)
125 875 4125
In[4]:= % /. t->(3+5x^3)^(1/4)//Simplify
3 3/4 3 6
4 (3 + 5 x ) (96 - 120 x + 175 x )
––––––––––––—
28875
1/x = t (x = 1/t)
In[1]:= f[x_]=(1/(x^2(x^2-x+1)^(1/2)))Dt[x]
Dt[x]
––––––-
2 2
x Sqrt[1 - x + x ]
In[2]:= f[1/t]//Simplify
Dt[t]
-(–––––—)
-2 1
Sqrt[1 + t - -]
t
In[3]:= Integrate[%/Dt[t],t]//PowerExpand
2
2 Log[-1 + 2 t + 2 Sqrt[1 - t + t ]]
-Sqrt[1 - t + t ] - –––––––––––-
2
In[4]:= % /. t->(1/x)
-2 1 2
Log[-1 + 2 Sqrt[1 + x - -] + -]
-2 1 x x
-Sqrt[1 + x - -] - –––––––––––
x 2
2 2 2
x/(x+1) = t ( x = t / (1 - t ) )
In[1]:= f[x_]=(x/(x+1)^(1/2))Dt[x]
x Dt[x]
–––—
Sqrt[1 + x]
In[2]:= f[t^2/(1-t^2)]//Simplify
3 1
2 t Sqrt[––] Dt[t]
2
1-t
–––––––—
22
(-1 + t )
In[3]:= Integrate[%/Dt[t],t]//Simplify
1 2
Sqrt[––] (-2 + ––—)
2 2
1-t 3-3t
In[4]:= % /. t->(x/(x+1))^(1/2)//Simplify
2 (-2 + x) Sqrt[1 + x]
–––––––-
3
In[1]:= f[x_]=(1/(x^2(x^2+4)^(1/2)))Dt[x]
Dt[x]
–––––
2 2
x Sqrt[4 + x ]
In[3]:= Integrate[%/Dt[t],t]//Simplify
2
-(Cot[t] Sqrt[Sec[t] ])
–––––––—
4
In[4]:= % /. t->ArcTan[x/2]//Simplify
2
-Sqrt[4 + x ]
––––-
4x
2.3 INTEGRATION BY PARTS
We have u = f (x)Þdu= f’(x) dx, and v = g (x)Þdv= g’(x) dx, so the final expression of the
integral will be:
This gives us a method of finding the integral of a product of two functions which can
potentially reduce the integral to a simpler integral.
Mathematica does not provide a function to do integrals parties directly, but, as in the
substitution method, it is easy to develop a procedure to perform this task. This procedure is
saved as permanent file and then be charged each time you want to make integral
substitution. It really is as if there was a package that solves the problem. Unlike the
substitution method, this is the option that will be adopted here.
Another alternative is to do with Mathematica the same steps used in normal practice
when a whole is solved piecemeal. This is the option which was adopted in the case of the
integral substitution.
Using a simple program we define a procedure that has as inputs the function to integrate
(function), the integration variable (x), the variable u and the value that takes u as a function
of x (u (x)=sustitution).
Mathematica incorporates the command ‘partes’ which allows you to find an integral
directly by parts.
In[3]:= <<intparts
Once the procedure is loaded, enter its arguments (function f (x) to integrate, the
integration variable x, the variable u, and the value that takes u as a function of x (u (x)) and
execute it.
In[1]:= <<intparts
In[3]:= x Sin[x]-Integrate[nuevointegrando,x]
Cos[x] + x Sin[x]
2
{(2 + 6 x) Sin[x], u -> -7 + 2 x + 3 x , v -> Sin[x]}
2
3 x Sin[x]
2 3
x Cos[Log[x]] x
{––––—, u -> Sin[Log[x]], v -> —}
3 3
In[2]:= Sin[Log[x]](x/3)-Integrate[nuevointegrando,x]//Simplify
3
x Sin[Log[x]] x (3 Cos[Log[x]] + Sin[Log[x]])
––––- - ––––––––––—
3 30
x
For the third integral we put u=E
x
E (Cos[x] + Sin[x])
––––––—
2
2.4 INTEGRATION BY REDUCTION AND CYCLIC
INTEGRATION
The usual procedure is to perform integration by parts. This will lead to the sum of an
integrated part and an integral of a similar form to the original, but with a reduced exponent.
Cyclic integration is similar except we end up with the same integral that we had at the
beginning, except for constants. The resulting equation can be rearranged to give the
original integral.
In both cases the problem lies in the proper choice of the function u(x) in the integration
by parts.
Mathematica directly calculates the value of this type of integral in the majority of cases.
In the worst case, the final value of the integral can be found after one to three applications of
integration by parts.
12
For the first integral we do u = Sen (x)
In[1]:= partes [Sin[x]^13 Cos[x]^15,x,u,Sin[x]^12]
17 11 16
-3 Cos[x] Sin[x] 12 -Cos[x]
{––––––—, u -> Sin[x] , v -> –––}
4 16
In[2]:= -Sin[x]^12(Cos[x]^16)/16-Integrate[nuevointegrando,x]//Simplify
7
For the second integral we do u = x
2a 2x 6 7
{7 (– + –) x Sqrt[a + b x], u -> x ,
3b 3
2a 2x
v -> (– + –) Sqrt[a + b x]}
3b 3
In[2]:=(x^7)(2a/3b+2x/3)Sqrt[a+bx]-Integrate[nuevointegrando,x]
//Simplify
7
2 x (a b + x) Sqrt[a + b x]
–––––––––- -
3
5/2 6 5
(4 (a + b x) (1024 a - 2560 a b x +
4 2 2 3 3 3 2 4 4
4480 a b x - 6720 a b x + 9240 a b x -
5 5 6 6 8
12012 a b x + 15015 b x )) / (109395 b )
9
For the third integral we do u= Sen (x)
2 8 9
{-9 Cos[x] Sin[x] , u -> Sin[x] , v -> -Cos[x]}
In[2]:= (Sin[x]^9)(-Cos[x])-Integrate[nuevointegrando,x]//Simplify
6
For the fourth integral we do u = ArcSen (x)
5
6 x ArcSin[x] 6
{––––—, u -> ArcSin[x] , v -> x}
2
Sqrt[1 - x ]
In[2]:= (ArcSin[x]6)x-Integrate[nuevointegrando,x]//Simplify
6 (-120 x + x ArcSin[x] +
2 2
120 Sqrt[1 - x ] ArcSin[x] + 60 x ArcSin[x] -
2 3 4
20 Sqrt[1 - x ] ArcSin[x] - 5 x ArcSin[x] +
2 5
Sqrt[1 - x ] ArcSin[x] )
12
For the fifth integral we do u = x
In[2]:= (x^12)(Sin[x])-Integrate[nuevointegrando,x]//Simplify
12
x Sin[x] - 12 (39916800 x Cos[x] -
3 5
6652800 x Cos[x] + 332640 x Cos[x] -
7 9 11
7920 x Cos[x] + 110 x Cos[x] - x Cos[x] -
2
39916800 Sin[x] + 19958400 x Sin[x] -
4 6
1663200 x Sin[x] + 55440 x Sin[x] -
8 10
990 x Sin[x] + 11 x Sin[x])
Chapter 3.
The definite integral acquires its strength when it comes to applying the techniques of
integration to practical problems. Here we present some of the most common applications.
3.2 CURVE ARC LENGTH
One of the most common applications of integral calculus is to find lengths of arcs of a
curve.
For a planar curve with equation , the arc length of the curve between points
with coordinates and is given by the expression:
For a planar curve with parametric coordinates and , the arc length
of the curve between the points corresponding to the values and of the
parameter is given by the expression:
For a curve in polar coordinates with equation , the arc length of the curve
between the points corresponding to the parameter values and is given by the
expression:
Exercise 3-1. An electrical cable hangs between two towers which are 80 meters apart.
The cable adopts the shape of a catenary whose equation is:
Calculate the arc length of the cable between the two towers.
Given the symmetry of the curve with respect to the y-axis, the limits of integration will
be - 40 and 40:
In[2]:= Integrate[(1+f’[x]^2)^(1/2),{x,-40,40}]
4 2
100 Sqrt[2] Sqrt[1 + Cosh[-]] Tanh[-]
5 5
In[3]:= N[%]
82.1505
Result: 82.1505 m.
Exercise 3-2. Calculate the arc length of the space curve represented by the parametric
equations:
In[1]:= {x[t_],y[t_],z[t_]}={t,(4/3)t^(3/2),(1/2)t^2}
3/2 2
4t t
{t, ––, —}
3 2
In[2]:= Integrate[(x’[t]^2+y’[t]^2+z’[t]^2)^(1/2),{t,0,2}]
Result: 4.8157.
Exercise 3-3. Find the arc length for values from a = 0 to a = 2pi of the cardioid given in
polar coordinates by r = 3-3cos (a).
In[1]:= r=f[a_]=3-3Cos[a]
3 - 3 Cos[a]
In[2]:= Integrate[(r^2+f’[a]^2)^(1/2),{a,0,2Pi}]
24
Result: 24
In[3]:= <<Graphics`Graphics`
In[3]:= PolarPlot[f[a],{a,0,2Pi}]
Figure 3-1
3.3 THE AREA ENCLOSED BETWEEN CURVES
The area enclosed between a curve with equation and the x-axis is given, in
general, by the integral:
where and are the abscissas of the end points of the curve.
where values of the parameter and correspond to the end points of the curve.
If the curve is given in polar coordinates , the area is given by the integral:
for the parameter values and corresponding to the end points of the curve.
where and are the abscissas of the end points of the two curves.
When calculating these areas it is very important to take into account the sign of the
functions involved since the integral of a negative portion of a curve will be negative. One
must divide the region of integration so that positive and negative values are not computed
simultaneously. For the negative parts one takes the modulus.
Exercise 3-4. Find the area of the region bounded by the curves defined below:
and
We will have to find the abscissas of the points where the curves meet. Consider the
equation:
In[1]:= Solve[2-x^2==x,x]
{{x -> -2}, {x -> 1}}
In[2]:= Integrate[2-x^2-x,{x,-2,1}]
9
-
2
The graph in Figure 3-2 illustrates the area that we had to calculate:
Plot[{f[x]=2-x^2,g[x]=x},{x,-2,1}]
Figure 3-2
Exercise 3-5. Calculate the area under the normal curve between the limits 1.96 and
1.96.
Figure 3-3
Exercise 3-6. Calculate the area of the ellipse with major and minor axes of length a and
b.
As is known, the equation of this ellipse is:
Rearranging this equation for y and assuming the positive root, we obtain
.
By the symmetry of the ellipse, the area will be four times the integral of this expression
In[1]:=Integrate[(4b(a^2-x^2)^(1/2))/a,{x,0,a}]
a b Pi
Exercise 3-7. Calculate the length of the perimeter and the area enclosed by the curves:
First, in order to get an idea of what the problem is, we graphically represent the curve
(see Figure 3-5):
In[1]:= ParametricPlot[{Cos[t](2-Cos[2t])/4,Sin[t](2+Cos[2t])/4},{t,0,2Pi}]
Figure 3-5
We see that as the parameter ranges between 0 and 2p the curve described is closed. We
can then calculate its length and the area that it encloses.
In[1]:= {x[t_],y[t_]}={Cos[t](2-Cos[2t])/4,Sin[t](2+Cos[2t])/4};
In[2]:= L=Integrate[(x’[t]^2+y’[t]^2)^(1/2),{t,0,2Pi}]
3
The length of the curve is 3 units. To calculate the area, we divide it into four equal
pieces (to prevent positive and negative cancellation), where t varies between 0 and 2p.
The area of each piece will be the absolute value of the integral between 0 and 2p of the
product y(t) * x’(t):
In[3]:= S=4*Integrate[y[t]*x’[t],{t,0,Pi/2}]
In[5]:= Abs[N[%]]
2.65466
and
We begin by representing both curves on the same graph (Figures 3-6 and 3-7).
In[1]:= <<Graphics`Graphics`
Figura 3-6
Figura 3-7
Now, we calculate the arc lengths and the areas enclosed by both curves. The first curve
repeats the structure for a between 0 and p/4 four times, and the second for a between 0 and
p/2:
In[3]:= S=4(1/2)Integrate[f[a]^2,{a,0,Pi/4}]
1
In[4]:= r=f[a_]=Sin[2 a]
Sin[2 a]
In[6]:= S=4(1/2)Integrate[f[a]^2,{a,0,Pi/2}]
Pi
—
2
Exercise 3-9. Calculate the area enclosed by the curve y = sin(x) and y = cos (x) for x
varying between 0 and 2p.
Figure 3-8 shows the graph of both curves on the same axis, so we can see the points of
intersection and the region between the curves.
In[1]:=Plot[{Sin[x],Cos[x]},{x,0,2 Pi},
PlotStyle->{GrayLevel[0],Dashing[{.01}]}]
Figure 3-8
We need to know the coordinates of the points of intersection of the two curves. To do
this, we solve the system formed by their two defining equations. The command solve does
not solve the problem. Therefore, since the graph indicates that there are solutions in the
intervals (0,1) and (1,2p). We will to find these solutions:
In[2]:= FindRoot[Sin[x]==Cos[x],{x,0.5}]
{x -> 0.785398}
In[3]:= FindRoot[Sin[x]==Cos[x],{x,4}]
{x -> 3.92699}
In[4]:= Abs[Integrate[Cos[x]-Sin[x],{x,0,0.785398}]]+
Abs[Integrate[Cos[x]-Sin[x],{x,0.785398,3.92699}]]+
Abs[Integrate[Cos[x]-Sin[x],{x,3.92699,2 Pi}]]
5.65685
The area of the surface generated by rotating the curve with equation
around the x-axis is given by the integral:
where and are the x-coordinates of the end points of the revolving curve. If the
curve is given in parametric coordinates and and the end points of the
curve correspond to and then the rotational surface area is given by the
integral:
The area of the surface generated by rotating the curve with equation around
the y axis is given by the integral:
Where and are the y-coordinates of the end points of the rotating curve.
Exercise 3-10. Calculate the area of the surface generated by rotating the cubic
around the OX axis, for x between 0 and 5/2.
In[1]:= <<Graphics`SurfaceOfRevolution`
In[2]:= SurfaceOfRevolution[2x^3-9x^2+12x,{x,0,2.5}]
Figure 3-9
In[3]:= f[x_]=2x^3-9x^2+12x;
In[4]:= Abs[2Pi*NIntegrate[f[x]*Sqrt[1+f’[x]^2],{x,0,5/2}]]
1.73127 Pi
Thus the surface of revolution has area 2p(0.8656) = 1.7312 p square units.
Exercise 3-11. Calculate the area of the surface of revolution given by rotating the
parametric curve defined by x (t) = t - sin (t) and y (t) = 1-cos (t) around the OX axis, for
t between 0 and 2p .
In[1]:= {x[t_],y[t_]}={t-Sin[t],1-Cos[t]};
In[2]:= SurfaceOfRevolution[{t-Sin[t],1-Cos[t]},{t,0,2Pi}]
Figure 3-10
21.3333 Pi
The volume generated by rotating the curve with equation around the x-axis is
given by the integral:
where and are the x-coordinates of the end points of the rotating curve.
The volume generated by rotating the curve with equation around the y-axis is
given by the integral:
where and are the y-coordinates of the end points of the rotating curve.
If one cuts a volume by planes parallel to one of the three coordinate planes (for
example, the plane z=0) and if the equation S(z) of the curve given by the cross section is
given in terms of the distance of the plane from the origin (in this case, z) then the volume is
given by:
We depict half of each of the generated surfaces of revolution next to each other in
Figure 3-11. The final volume will therefore be twice that of the represented figure.
Exercise 3-13. Calculate the volume generated by rotating the curve given in polar
coordinates by r = 1 + cos (a) about the OX axis.
The curve is given in polar coordinates, but there is no problem in calculating the values
of and needed to implement the volume formula in cartesian coordinates. We
have:
In[1]:=SurfaceOfRevolution[{(1+Cos[a])Cos[a],(1+Cos[a])Sin[a]},{a,0,Pi}]
Figure 3-12
In[1]:= {x[a_],y[a_]}={(1+Cos[a])Cos[a],(1+Cos[a])Sin[a]};
8 Pi
–-
3
differentiable curve in R3. We define , and call it the integral of along the curve
c, as follows:
In[2]:= F[Sin[t],Cos[t],t]
k t + j Cos[t] + i Sin[t]
In[3]:={Sin[t],Cos[t],t}.D[{Sin[t],Cos[t],t},t]
In[4]:= Integrate[t,{t,0,2Pi}]
2
2 Pi
In[1]:= {x[a_],y[a_],z[a_]}={Cos[a]^3,Sin[a]^3,a}
3 3
{Cos[a] , Sin[a] , a}
In[2]:= D[%,a]
2 2
{-3 Cos[a] Sin[a], 3 Cos[a] Sin[a] , 1}
1
-(-)
2
Exercise 3-16. Let the curve c be the circle of radius a: x2 + y2 =a2. Calculate the value of
First, the circle can be parameterized (x = a cos (t), y = a sin (t)) and we can then
calculate the integral as follows:
{a Cos[t], a Sin[t]}
Mathematica works with improper integrals in the same way as it works with any other
type of definite integral. We will not discuss theoretical issues concerning the convergence
of improper integrals here, but within the class of improper integrals we will distinguish two
types:
1) Integrals with infinite limits: the domain of definition of the integrand is a half-line
[(a,¥) or (-¥, a)] or the entire line (-¥,¥).
Complicated combinations of these two cases may also occur. One can also generalize
this to the more general setting of Stieltjes integrals, but to discuss this would require a
course in mathematical analysis.
In[1]:= Limit[Integrate[1/x^2,{x,a,b}],b->Infinity]
1
-
a
In[2]:= Integrate[1/x^2,{x,a,Infinity}]
1
-
a
Pi
—
4
It is therefore two convergent improper integrals
b p/2 8 1/3 2 2
ò 1/Öx dx ò Tan(x)dx ò 1/ x dx ò 2x/(x - 1) dx
0 0 -1 -2
In[1]:= Limit[Integrate[1/Sqrt[x],{x,a,b}],a->0]
2 Sqrt[b]
In[2]:= Integrate[1/Sqrt[x],{x,0,b}]
2 Sqrt[b]
The first integral is convergent and its value is 2Öb.
In[3]:= Limit[Integrate[Tan[x],{x,0,Pi/2-b}],b->0]
Infinity
The integral is divergent. If we try to find its value directly with Mathematica gives us
undetermined.
In[4]:= Integrate[Tan[x],{x,0,Pi/2}]
Indeterminate
The third integral presents a unique singular point at x = 0, which is within the range
(-1.8).
In[5]:= Limit[Integrate[1/(x)^(1/3),{x,-1,-b}],b->0]+
Limit[Integrate[1/(x)^(1/3),{x,c,8}],c->0]
2/3
3 (-1)
6 - –––
2
The above value is 9.2, which is integral convergent.
In[6]:= Integrate[1/(x)^(1/3),{x,-1,8}]
2/3
3 (-1)
6 - –––
2
The last integral has two singular points x = -1 and x = 1 that are internal to the interval
(-2.2)
In[7]:= Limit[Integrate[2x/(x^2-1),{x,-2,-1-b}],b->0]+
Limit[Limit[Integrate[2x/(x^2-1),{x,-1+c,1-d}],c->0],d->0]+
Limit[Integrate[2x/(x^2-1),{x,1+e,2}],e->0]
Indeterminate
In[8]:= Integrate[2x/(x^2-1),{x,-2,2}]
Indeterminate
Integrals dependent on a parameter can also be improper, and in addition the limits of
integration may also depend on a parameter.
provided and are defined on the interval and have continuous derivatives
and , and the curves and are contained in the rectangle .
i.e. integration under the integral sign is valid, and the order in which the integrals are
evaluated can be reversed without affecting the result.
Exercise 3-20. Solve the following integrals using a parameter :
For the first integral, we will start by integrating the derivative of the integrand with
respect to the parameter , which will be easier to integrate. Once this integral is found we
integrate with respect to to find the original integral.
Now we integrate this function with respect to the variable a, to find the original integral:
In[2]:= Integrate[%,a]
Pi Log[1 + a]
––––-
2
As in the first integral, we differentiate the integrand with respect to the parameter , find
the integral, and then integrate with respect to . The desired integral is then given by setting
.
In[1]:= Integrate[D[(1-E^(-a x^2))/x^2,a],{x,0,Infinity}]
Sqrt[Pi]
–––
2 Sqrt[a]
In[2]:= s[a_]=Integrate[%,a]
Sqrt[a] Sqrt[Pi]
In[3]:= s[1]
Sqrt[Pi]
3.9 THE RIEMANN INTEGRAL
The definite integral of a function gives the total area between the graph of the
function and the x-axis contained between the two limits of integration, where areas under
the x-axis are regarded as negative.
Riemann defined this integral as a certain limit. Given a partition of the domain of the
function into intervals one can define two associated values, the upper sum and the lower
sum. The upper sum is the sum of the areas of the rectangles with bases the intervals of the
partition and heights given by the maximum of the function in the interval. The lower sum
is similarly defined except the height of the rectangle is determined by the minimum of the
function in the interval. Thus there are infinitely many upper sums and infinitely many
lower sums. One can define the upper integral to be the infimum of the upper sums and the
lower integral to be the supremum of the lower sums. If these two values are equal, then the
function is Riemann integrable and the common value is its integral.
By dividing the domain into a large number of intervals with small widths one can
approximate the integral. This method can be used when the integrand cannot be integrated
algebraically.
If the function y = f (x) we have to integrate is positive and continuous on the interval [a,
b] of integration, the upper and lower sums can be calculated with the following
Mathematica expressions:
If the function we have to integrate is negative in certain subsets of its domain, we take the
modulus of the function for the purpose of computing the integral and finding areas. One can
apply this directly to the function without having to determine the subintervals on which it is
negative.
In[1]:= Plot[1/Log[x],{x,1.1,5}]
Figura 3-13
We approximate the integral value using 100 rectangles and considering the upper and
lower amounts.
In[2]:= <<Algebra`SymbolicSum`
In[3]:= f[x_]=1/Log[x]
1
––
Log[x]
In[1]:= Plot[Sin[x^2],{x,0,4}]
Figure 3-14
Since there are areas above and below the x axis must be considered in the function
module to subtract areas not to try to add them. We estimate 1,000 rectangles and the upper
and lower Riemann sums. The actual value of the integral will be a number between these
two sums. Keep in mind that the function has positive and negative areas, so it is necessary
to consider its module.
In[2]:= <<Algebra`SymbolicSum`
In[3]:= f[x_]=Sin[x^2];
1.75381
In[5]:= (4-0)/1000 Sum[Abs[f[0+(4-0)k/1000]],{k,0,999}]//N
1.75152
Then the exact value of the integral problem will be a number between 1.75381 and
1.75152.
The exact value can be calculated considering n intervals and finding the limit when n
has infinite sums upper and lower amounts. Should match this limit, the integral value is to
limit calculated precisely.
5 2
ò (1 + 12 x - x )
In[1]:= f[x_]=1+12x-x^2;
We have now calculated the exact value of the integral, as the limit of the upper and
lower amounts of Rieman agree limit value.
Chapter 4.
Computes
Integrate[f[x,y],{x,a,b},{y,c,d}}]
NIntegrate[f[x,y],{x,a,b},{y,c,d}}]
Computes, using approximate methods, the definite double integral
Integrate[f[x,y,…, z],{x,a,b},{y,c,d},…,{z,e,f}]
Computes
NIntegrate[f[x,y,…, z],{x,a,b},{y,c,d},…,{z,e,f}]
If we consider an enclosure S, we can find its area through the use of double integrals.
If the area S is determined by boundary curves whose equations are given in cartesian
coordinates, its area A is given by the formula:
If the region S is determined by curves whose equations are given in polar coordinates
with radius vector and angle , its area is given by the formula:
Exercise 4-1. Calculate the area of the region above the OX axis bounded by the OX
axis, the parabola y2 = 4 x and the line x + y = 3.
In[1]:= Plot[{y=0,y=Sqrt[4x],y=3-x},{x,-1,4}]
Figure 4-1
We see that in the enclosed region y is limited between 0 and 2 (0 < y < 2) and x is
limited between the curves x =( y ^ 2)/4 and x = 3 - y. We calculate the requested area as
follows:
Abs[Integrate[1 Integrate[1,{x,y^2/4,3-y}],{y,0,2}]]
10
—
3
Exercise 4-2. Calculate the area in the first quadrant bounded between the semicubical
parabola y2 = x3 and the bisector of the first quadrant.
In[1]:= Plot[{y=Sqrt[x^3],y=x},{x,-1/4,5/4}]
Figure 4-2
Inspecting the graph we see that the enclosed area is defined for x ranging between 0
and 1 (0 <x< 1) and y ranging between the curves y = x and y = x ^(3/2). Therefore the
area of the requested region can be calculated as:
1
—
10
Exercise 4-3. Calculate the area outside the circle with polar equation r = 2, and inside
the cardioid with polar equation r = 2 (1 + cos (a)).
In[1]:= <<Graphics`Graphics`
In[2]:= PolarPlot[{2,2(1+Cos[a])},{a,0,2Pi}]
Figure 4-3
Inspecting the graph, we see that, by symmetry, we can calculate half of the required
area by varying a between 0 and Pi/2 (0 <a<Pi/2) and r between the curves r = 2 and r =
2 (1 + cos (a)):
Exercise 4-4. Calculate the area of the surface of the cone x2 + y2 = z2, limited above
the OXY plane and cut by the cylinder x2 + y2 = b y.
The projection of the surface onto the OXY plane is the disc bounded by the circle with
equation x2+ y2= by. Then we can find the surface area via the first formula above as
follows:
In[1]:= Clear[x,y,z]
In[2]:= z[x_,y_]=Sqrt[(x^2+y^2)/a];
In[3]:= Integrate[Integrate[Sqrt[1+D[z[x,y],x]^2+D[z[x,y],y]^2],
{x,-Sqrt[b y-y^2],Sqrt[b y-y^2]}],{y,0,b}]
1
(1 + -) a b
3/2 a
(1 + a) b Sqrt[–––—] Pi
1+a
–––––––––––
1
4 Sqrt[1 + -] a
a
In[4]:= Simplify[%]
2
(1 + a) b Pi
–––––
1
4 Sqrt[1 + -] a
a
Exercise 4-5. Calculate the area of the paraboloid x2 + y2 = 2 z limited below the plane
z = 2.
In[1]:= Clear[x,y,z]
In[2]:= z[x_,y_]=(x^2+y^2)/2;
In[3]:= Sqrt[1+D[z[x,y],x]^2+D[z[x,y],y]^2]
2 2
Sqrt[1 + x + y ]
This expression is suitable for a transformation to polar coordinates:
2
Sqrt[1 + r ]
The requested integral is calculated as 4 times the integral delimited by the first
quadrant of the circle r = 2 :
(-1 + 5 Sqrt[5]) Pi
––––––-
3
The result of the integral will be 4 times the previous value, i.e.
2(5Ö5-1)p/3.
In[1]:= Plot3D[f[x,y]=(x^2+y^2)/2,{x,-3,3},{y,-3,3}] ;
In[2]:= Plot3D[f[x,y]=2,{x,-3,3},{y,-3,3}]
In[3]:= Show[%,%%]
Figure 4-4
4.3 VOLUME CALCULATION BY DOUBLE INTEGRALS
The volume of a cylindroid limited in its upper part by the surface with equation
, its lower part by the OXY plane and laterally by the straight cylindrical
surface which cuts the OXY plane bordering a region , is:
The volume of a cylindroid limited in its upper part by the surface with equation
, its lower part by the OYZ plane and laterally by the straight cylindrical
surface which cuts the OYZ plane bordering a region , is:
The volume of a cylindroid limited in its upper part by the surface with equation
, its lower part by the OXZ plane and laterally by the straight cylindrical surface
which cuts the OXZ plane bordering a region , is:
Exercise 4-6. Calculate the volume in the first octant bounded between the OXY plane,
the plane z = x + y + 2 and the cylinder x2 + y2 = 16.
We begin by graphically representing the problem (see Figure 4-5) with the the plane in
cartesian coordinates and parameterizing the cylinder:
In[2]:= Plot3D[f[x,y]=x+y+2,{x,-4,4},{y,-4,4}];
Now we represent the two surfaces on the same graph:
Figure 4-5
In[1]:= Integrate[Integrate[x+y+2,{y,0,Sqrt[16-x^2]}],{x,0,4}]
128
– + 8 Pi
3
In[2]:= Plot3D[f[x,y]=x^2+4y^2,{x,-1/2,1/2},{y,-1/4,1/4}]
In[3]:= Show[%,%%,%%%]
Figure 4-6
In[4]:= Integrate[Integrate[x^2+4y^2,{y,x^2,Sqrt[x]}],{x,0,1}]
3
-
7
4.4 VOLUME CALCULATION AND TRIPLE INTEGRALS
Exercise 4-8. Calculate the volume bounded by the paraboloid x2 + y2 = z and the
cylinder with equation z = a2 - y2.
In[1]:=4Integrate[Integrate[Integrate[1,{z,ax^2+y^2,a^2-y^2}],
{y,0,Sqrt[(a^2-a x^2)/2]}],{x,0,Sqrt[a]}]
17/4
a Pi
––––––—
3/2
2 Sqrt[2] Sqrt[a ]
In[2]:=PowerExpand[%]
7/2
a Pi
–––
2 Sqrt[2]
In[1]:= Plot3D[f[x,y]=x^2,{x,-2,2},{y,-2,2}]
In[3]:= Show[%,%%]
Figure 4-7
In[1]:=4Integrate[Integrate[Integrate[1,{z,x^2,4-y^2}],
{y,0,Sqrt[4-x^2]}], {x,0,2}]
8 Pi
Exercise 4-10. Calculate the volume enclosed by the cylinder r = 4cos(a), the plane z =
0 and the sphere with equation r2 + z2 = 16.
In[1]:= Integrate[Integrate[r*Integrate[1,{z,0,Sqrt[16-r^2]}],
{r,0,4Cos[a]}],{a,0,Pi}]
128 64 (-2 + 3 Pi)
-(–) + ––––—
9 9
In[2]:= Simplify[%]
64 (-4 + 3 Pi)
––––—
9
Exercise 4-11. Calculate the volume enclosed by the cone b = π/4 and the sphere with
equation r = 2 k cos (b).
The volume of the enclosure, given in spherical coordinates, will be four times the
result of the following integral:
Let C be a simple closed planar curve, and R the region consisting of C and its interior.
If f and g are continuous functions with continuous first partial derivatives in an open set
D containing R, then:
The two parabolas intersect at the points (0,0) and (1,1). We graphically represent
the problem (see Figure 4-8) and calculate the integral:
In[1]:= Plot[{x^2,Sqrt[x]},{x,0,1.2}]
Figura 7-8
In[2]:= m[x_,y_]=x+E^Sqrt[y];
In[3]:= n[x_,y_]=2y+Cos[x];
In[4]:=Integrate[Integrate[D[n[x,y],x]-D[m[x,y],y],{y,x^2,Sqrt[x]}],
{x,0,1}]
Pi 2
-26 + 8 E + 2 Cos[1] - Sqrt[—] FresnelC[Sqrt[—]] +
2 Pi
2
Sqrt[x ]
Integrate[E , {x, 0, 1}] + 2 Sin[1]
» numeric(int(int(‘diff(n(x,y),x)-diff(m(x,y),y)’,‘y’,‘x^2’,‘sqrt(x)’),‘x’,0,1))
In[4]:=NIntegrate[NIntegrate[D[n[x,y],x]-D[m[x,y],y],{y,x^2,Sqrt[x]}],
{x,0,1}]
-0.6764
4.6 THE DIVERGENCE THEOREM
Let Q be a domain with the property that each straight line passing through a point
inside a domain cuts its border at exactly two points. In addition, suppose the boundary S
of the domain Q is a closed oriented surface with exterior normal vector n. If f is a vector
field that has continuous partial derivatives on Q, then:
The left-hand side of this equality is called the outflow of the vector field f across the
surface S.
Exercise 4-13. Use the divergence theorem to find the outflow of the vector field f =
(xy+x2 , yz+xy2 , z z, xz+xyz2 ) across the surface of the cube in the first octant bounded
by the planes x = 2, y = 2 and z = 2.
In[1]:=<<Calculus`VectorAnalysis`
In[3]:= divf[x_,y_,z_]=Div[f[x,y,z],Cartesian]
x+y+z+6xyz
In[4]:=Integrate[divf[x,y,z],{x,0,2},{y,0,2},{z,0,2}]
72
4.7 STOKES’ THEOREM
Let S be an oriented surface of finite area defined by a function f(x,y), having boundary
C with unit normal n. Suppose F is a continuous vector field defined on S such that the
component functions of F have continuous partial derivatives at each non-boundary point
of S. Then:
where C is the intersection of the cylinder x2 + y2 = 1 and the plane x + y + z = 1, and the
orientation of C corresponds to counterclockwise rotation of the OXY plane.
The curve C bounds the surface S defined by z = 1-x-y = f(x,y) for (x, y) in the domain
D = {(x,y) / x2 + y2= 1}.
We put F= - y 3 i + x 3 j - z 3 k.
In[1]:= F={-y^3,x^3,z^3}
3 3 3
{-y , x , z }
In[2]:= Curl[F]
2 2
{0, 0, 3 x + 3 y }
Therefore, we have to calculate the integral òD (3 x2+ 3y2 ) dx dy. Changing to polar
coordinates, we obtain:
3 Pi
–-
2
Chapter 5.
Solving differential equations in partial derivatives is also possible with the program.
The packages are Calculus`PDSolve` and Calculus`PDSolve1`. The first increases the
number of equations to be solved directly with the DSolve function, and the second
implemeta functions to solve partial differential equations..
The most important Mathematica commands that solve differential equations are the
following:
DSsolve(equation, y[x], x) Solves the given differential equation for y[x]. This
returns only explicit solutions.
In[1]:= DSolve[2y’[x]+2y[x]==1,y[x],x]
1 C[1]
{{y[x] -> - + –-}}
2 x
E
In[2]:= DSolve[{y[x]==-z’[x],z[x]==-y’[x]},{y,z},x]//Simplify
2x 2x
C[1] + E C[1] + C[2] - E C[2]
–––––––––––—],
x
2E
z -> Function[x,
2x 2x
C[1] - E C[1] + C[2] + E C[2]
–––––––––––—]}}
x
2E
In[3]:= DSolve[{3y’[x]==2y[x],y[0]==1},y[x],x]
(2 x)/3
{{y[x] -> E }}
5.1 SEPARATION OF VARIABLES
A differential equation is said to have separable variables if it can be written in the form f(x)
dx = g(y) dy.
If Mathematica cannot directly solve a differential equation with the function DSolve, then
we can try to express it in the above form and solve the given integrals algebraically, which
does not present particular difficulties for the program, given its versatility in symbolic
computation.
First of all we try to solve it directly. The equation can be written in the form:
Solve::tdep:
The equations appear to involve transcendental
functions of the variables in an essentially
non-algebraic way.
Solve::tdep:
The equations appear to involve transcendental
functions of the variables in an essentially
non-algebraic way.
2
y[x]
Solve[Log[y[x]] - Sin[x] + –— == C[1], y[x]]
2
Thus the differential equation appears not to be solvable with DSolve. However, in this
case, the variables are separable, so we can solve the equation as follows:
In[1]:= solucion=Integrate[Cos[x],x]==Integrate[(1+y^2)/y,y]+c
2
y
Sin[x] == c + — + Log[y]
2
1
0 == - + c
2
In[3]:= Solve[constante]
1
{{c -> -(-)}}
2
2
1 y
Sin[x] == - + — + Log[y]
2 2
5.2 HOMOGENEOUS DIFFERENTIAL EQUATIONS
Consider a general differential equation of first degree and first order of the form
For this type of equation, we can transform the initial differential equation (with
variables x and y), via the change of variable x = vy, into another (separable) equation
(with variables v and y). The new equation is solved by separation of variables and then
the solution of the original equation is found by reversing the change of variable.
(x 2 - y2) dx + x y dy = 0.
In[2]:=DSolve[ecuacion,y[x],x]
2 2
DSolve[x y y’[x] == -x + y[x] , y[x], x]
The DSolve function not offers solution, so let’s solve the equation by ordinary
algebraic method.
First we check if the equation is homogeneous:
In[3]:= m[x_,y_]=x^2-y^2;
n[x_,y_]=x y;
2
t (x - y) (x + y)
2
t xy
In[6]:= expresion1=m[x,y]Dt[x]+n[x,y]Dt[y]
2 2 2 2
v y Dt[y] + (-y + v y ) (y Dt[v] + v Dt[y])
In[7]:= x=v y;
In[8]:= expresion2=expresion1//ExpandAll
3 2 3 3 2
-(y Dt[v]) + v y Dt[v] + v y Dt[y]
In[9]:= expresion3=Collect[expresion2,{Dt[v],Dt[y]}]
3 2 3 3 2
(-y + v y ) Dt[v] + v y Dt[y]
If we divide the previous expression by v3y3, and group the terms in d(v) and d(y), we
already have an equation in separated variables.
2
(-1 + v ) Dt[v] Dt[y]
––––– + –—
3 y
v
In[11]:=expresion5=Integrate[(v^2-1)/v^3,v]+Integrate[1/y,y]
1
–- + Log[v] + Log[y]
2
2v
In[12]:= Clear[x,v,y]
2
y
–- + Log[x]
2
2x
In[14]:= solucion=expresion6==c
2
y
–- + Log[x] == c
2
2x
Now we can represent the solutions of this differential equation graphically. To do this
we graph the solutions with parameter C, which is equivalent to the following contour plot
of the function defined by the left-hand side of the above general solution (see Figure 5-1):
Figure 5-1
5.3 EXACT DIFFERENTIAL EQUATIONS
M(x,y) dx+N(x,y) dy = 0
is said to be exact if ¶N/¶x = ¶M/¶y. If the equation is exact, then there exists a function F
such that its total differential dF coincides with the left-hand side of the above equation,
i.e.:
dF = M(x,y) dx+N(x,y) dy
The exercise below follows the usual steps of an algebraic solution to this type of
equation.
Solve::tdep:
The equations appear to involve transcendental
functions of the variables in an essentially
non-algebraic way.
Solve::tdep:
The equations appear to involve transcendental
functions of the variables in an essentially
non-algebraic way.
x y[x]
Solve[E - x + Sin[x y[x]] + y[x] == C[1], y[x]]
Thus the function DSolve does not give a solution to the proposed equation. We are
going to try to solve the equation using the classical algebraic method.
In[4]:= D[m[x,y],y]==D[n[x,y],x]
True
Since the equation is exact, we can find the solution in the following way:
In[5]:= solucion1=Integrate[m[x,y],x]+g[y]
xy
E - x + g[y] + Sin[x y]
In[6]:= expresion1=D[solucion1,y]
xy
E x + x Cos[x y] + g’[y]
In[7]:=Solve[expresion1==n[x,y],g’[y]]
xy
E - x + y + Sin[x y]
To graphically represent the family of solutions, we draw the following contour plot of
the above expression (Figure 5-2):
In[9]:=ContourPlot[solucion,{x,-Pi,Pi},{y,-Pi,Pi},
PlotRange->{-10,10},ContourShading->False]
Figure 5-2
In the following section we will see how any reducible differential equation can be
transformed to an exact equation using an integrating factor.
5.4 LINEAR DIFFERENTIAL EQUATIONS
.
Mathematica implements these solutions of linear differential equations, and offers
them whenever the integral appearing in the integrating factor can be found.
x dy/dx + 3 y = x sin(x).
Make the graphs of the solutions to the following constant values: -6,-4,-2,0,2,4,6
C[1]
{{y[x] -> –- +
3
x
3 2
6 x Cos[x] - x Cos[x] - 6 Sin[x] + 3 x Sin[x]
–––––––––––––––—}}
3
x
In[2]=Plot[Evaluate[Table[solucion[[1,1,2]] /.C[1]->
{i,-6,6,2,-2,4,-4,0}]], {x,0.01,3Pi/2}, PlotRange->{-5,5}]
Figure 5-3
5.5 NUMERICAL SOLUTIONS TO DIFFERENTIAL
EQUATIONS OF THE FIRST ORDER
Exercise 5-5. Solve and graph the solution of the differential equation:
In[1]:= ecuation=y’[x]==Sin[2x-y[x]];
In[2]:= solution=NDSolve[{ecuation,y[0]==0.5},y[x],{x,0,15}]
Figure 5-4
Chapter 6.
An ordinary linear differential equation of order n has the following general form:
The equation:
is linearly independent.
x x x 2
{E , E x, E x }
In[2]:= matriz={funciones,D[funciones,x],D[funciones,{x,2}]}
x x x 2 x x x x x 2
{{E , E x, E x }, {E , E + E x, 2 E x + E x },
x x x x x x 2
{E , 2 E + E x, 2 E + 4 E x + E x }}
In[3]:= Det[matriz]
3x
2E
This gives us the value of the Wronskian, which is obviously always non-zero.
Therefore the set of functions is linearly independent.
In[4]:= TableForm[matriz]
x x x 2
E E x E x
x x x x x 2
E E +E x 2E x+E x
x x x x x x 2
E 2E +E x 2E +4E x+E x
6.2 HIGHER-ORDER LINEAR HOMOGENEOUS
EQUATIONS WITH CONSTANT COEFFICIENTS
The equation:
is called the characteristic equation of the above differential equation. The solutions
of this characteristic equation determine the general solution of the
associated differential equation.
If the characteristic equation has a complex root mj=a+bi , then its complex conjugate
mj+1=a-bi is also a root. These two roots determine a pair of terms in the general solution
of the homogeneous equation:
Mathematica directly applies this method to obtain the solutions of homogeneous linear
equations with constant coefficients, using the command DSolve.
In[1]:= solucion=DSolve[3y”[x]+2y’[x]-5y[x]==0,y[x],x]
C[1] x
{{y[x] -> ––— + E C[2]}}
(5 x)/3
E
In[2]:=solucion=DSolve[{2y”[x]+5y’[x]+5y[x]==0,y[0]==0,
y’[0]==1/2},y[x],x]
Sqrt[15] x
2 Sin[–––-]
4
–––––—
(5 x)/4
Sqrt[15] E
In[1]=solucion=DSolve[9D[y[x],{x,4}]-6D[y[x],{x,3}]+46y’’+37y==0,
y[x],x}
x/3
{{y[x] -> C[2] Cos[x] + E C[4] Cos[2 x] -
x/3
C[1] Sin[x] - E C[3] Sin[2 x]}}
Looking at the solution, it is evident that the characteristic equation has two pairs of
complex conjugate solutions.
In[2]:= Solve[9x^4-6x^3+46x^2-6x+37==0]
1 1
{{x -> -I}, {x -> I}, {x -> - - 2 I}, {x -> - + 2 I}}
3 3
6.3 NON-HOMOGENEOUS EQUATIONS WITH CONSTANT
COEFFICIENTS. VARIATION OF PARAMETERS
We will follow the algebraic method of variation of parameters to solve the first
equation. We first consider the characteristic equation of the homogeneous equation to
obtain a set of linearly independent solutions.
In[2]:= Solve[m^2+4m+13==0]
In[3]:= y1[x_]=Exp[-2x]Cos[3x];
In[4]:=y2[x_]=Exp[-2x]Sin[3x];
In[5]:= wronskiano=Det[{{y1[x],y2[x]},D[{y1[x],y2[x]},x]}]//Simplify
3
–-
4x
E
We see that the Wronskian is non-zero, indicating that the functions are linearly
independent. Now we calculate the functions
In[6]:= W1[x_]=Det[{{0,y2[x]},{1,D[y2[x],x]}}]//Simplify
Sin[3 x]
-(––—)
2x
E
In[7]:=W2[x_]=Det[{{y1[x],0},{D[y1[x],x],1}}]//Simplify
Cos[3 x]
––—
2x
E
2x
(E (-86700 Cos[3 x] + 281775 x Cos[3 x] -
6084 Cos[9 x] + 129285 x Cos[9 x] -
36125 Sin[3 x] - 187850 x Sin[3 x] -
13013 Sin[9 x] - 28730 x Sin[9 x])) / 14652300
2x
(E (108375 Cos[3 x] + 563550 x Cos[3 x] +
13013 Cos[9 x] + 28730 x Cos[9 x] -
260100 Sin[3 x] + 845325 x Sin[3 x] -
6084 Sin[9 x] + 129285 x Sin[9 x])) / 14652300
c1 Cos[3 x] c2 Sin[3 x]
–––— + –––— +
2x 2x
E E
Now we graphically represent a set of solutions, for certain values of c1 and c2 (see
Figure 6-1)
In[12]:= Plot[Evaluate[Table[y[x],{c1,-5,5,5},{c2,-4,4,2}]],{x,-1,1},
PlotRange->{-20,20}, PlotPoints->100]
Figure 6-1
For the second differential equation we directly apply DSolve, obtaining the solution.
First, we find the general solution of the homogeneous equation:
x 2
-3 E x x x
{{y[x] -> ––— + E C[1] + E x C[2] +
4
x 2
E x Log[x]
––––}}
2
6.4 NON-HOMOGENEOUS LINEAR EQUATIONS WITH
VARIABLE COEFFICIENTS. CAUCHY-EULER
EQUATIONS
Mathematica solves this type of equation directly with the command DSolve.
Exercise 6-5. Solve the following differential equation:
The Laplace transform and its inverse are used to solve certain differential equations.
The method is to calculate the Laplace transform of each term of the equation to obtain a
new differential equation, which we then solve. Finally, we find the solution of the
original equation by applying the inverse Laplace transform to the solutions just found.
In[1]:= <<Calculus`LaplaceTransform`
First, we calculate the Laplace transform of each side of the differential equation,
and we apply the initial conditions.
In[2]:=paso1=LaplaceTransform[y”[x]+2y’[x]+4y[x],x,s]
1
- + 4 LaplaceTransform[y[x], x, s] +
2
2
s LaplaceTransform[y[x], x, s] +
1
- - s + 4 LaplaceTransform[y[x], x, s] +
2
2
s LaplaceTransform[y[x], x, s] +
General::intinit:
Loading integration packages — please wait.
{{LaplaceTransform[y[x], x, s] ->
2 3 4
-(2 - 2 s + 5 s - s - 2 s )
–––––––––—}}
2 2
2 (-1 + s) s (4 + 2 s + s )
This gives the solution of the Laplace transformed equation. To calculate the solution of
the original equation we calculate the inverse Laplace transform of the solution obtained
in the previous step.
In[5]:= paso4=paso3[[1,1,2]]
2 3 4
-(2 - 2 s + 5 s - s - 2 s )
–––––––––—
2 2
2 (-1 + s) s (4 + 2 s + s )
In[6]:= solucion=InverseLaplaceTransform[paso4,s,x]
x
1 E x 36 Sqrt[3] Sin[Sqrt[3] x]
-(-) - — + - + (––––––––- +
8 7 4 x
E
Cos[Sqrt[3] x] Sin[Sqrt[3] x]
71 (––––— - ––––—)) / 56
x x
E Sqrt[3] E
6.6 SYSTEMS OF LINEAR HOMOGENEOUS EQUATIONS
WITH CONSTANT COEFFICIENTS
where:
-2 3 -3 3
{{x[t] -> (–- + –-) C[1] + (–- + –-) C[2],
8t 7t 8t 7t
E E E E
2 2 3 2
y[t] -> (–- - –-) C[1] + (–- - –-) C[2]}}
8t 7t 8t 7t
E E E E
6.7 SYSTEMS OF LINEAR NON-HOMOGENEOUS
EQUATIONS WITH CONSTANT COEFFICIENTS
Mathematica can solve such systems of equations directly with the command DSolve,
provided the integrals that appear in the solution can be evaluated.
In[1]:=DSolve[{x’[t]-y’[t]==E^(-t),y’[t]+5x[t]+2y[t]==Sin[3t],
y[0]==y0,x[0]==x0},{x[t],y[t]},t]
41 5 x0 2 y0
-(–) + –- + –-
-1 609 7 7
{{x[t] -> –- + ––––––— -
t 7t
6E E
41 5 x0 2 y0
-(–) + –- + –-
5 609 7 7
y[t] -> –- + ––––––— +
t 7t
6E E
The numerical approach yields a solution in the form of a finite set of solution
points, to which a curve can be fitted by various algebraic methods (interpolation,
regression,…). This curve will be an approximate solution of the differential equation.
Among the most common numerical methods is the Runge–Kutta method.
Approximation methods are most commonly employed to find the solution of equations
and systems of differential equations of order and degree greater than one, where the exact
solution cannot be obtained by other methods.
7.2 THE EULER METHOD
This method provides a set of points on the solution of the differential equation of
the type dy / dx = f (x, y), y(x0) = y0.
The set of points is defined by the recurrence relation:
x(n) = n h
y(n+1) = y(n) + f(x(n),y(n))h
for initial values: (x0, y0)
A more sophisticated variant of Euler, which defines the solution by the following
recurrence relation:
X(n) = n h
y(n+1) = y(n) +1/2 [f(x(n),y(n)) + f(x(n+1), y(n) + f(x(n),y(n))h) ]h
Exercise 7-1. Solve the following two equations by the Euler method:
In[1]:= Clear[f,x,y,h]
In[2]:= f[x_,y_]=x y;
In[6]:=puntos=Table[{x[i-1],Table[y[i],{i,0,10}][[i]]},
{i,1,Length[Table[y[i],{i,0,10}]]}]
{{0, 1}, {0.1, 1}, {0.2, 1.01}, {0.3, 1.0302},
{0.4, 1.06111}, {0.5, 1.10355}, {0.6, 1.15873},
{0.7, 1.22825}, {0.8, 1.31423}, {0.9, 1.41937},
{1., 1.54711}}
.
Figure 7-1
In[1]:= Clear[x,y,h]
In[2]:= f[x_,y_]=x y;
In[4]:= x[n_]=n h;
Figure 7-2
7.3 THE RUNGE–KUTTA METHOD
The Runge–Kutta method gives a set of data points to which you can fit a curve,
approximating the solution of a differential equation. Initially used the method known as
order 2 used, but now often used the order 4. In this case the iterative algorithm used is as
follows:
x(n+1) = nh
y(n+1) = y(n) + h/6 (k1+2k2+2k3+k4)
Exercise 7-2. Solve the following two equations by the Euler method:
In[1]:= x[n_]:=n h
In[2]:= f[x_,y_]=x y;
In[4]:= h=0.1;
In[5]:= rk4[0]=1;
In[6]:=rk4[n_]:=Module[{k1,k2,k3,k4},
k1=f[x[n-1],rk4[n-1]];
k2=f[x[n-1]+h/2,rk4[n-1]+(h k1)/2];
k3=f[x[n-1]+h/2,rk4[n-1]+(h k2)/2];
k4=f[x[n],rk4[n-1]+h k3];
rk4[n]=rk4[n-1]+(h/6)(k1+2k2+2k3+k4)]
In[7]:= solucion=Table[{x[i-1],Table[rk4[i],{i,0,10}][[i]]},
{i,1,Length[Table[rk4[i],{i,0,10}]]}]
We have a set of points of the solution can be adjusted to a curve through any known
method. This curve is an approximation to the solution of the differential equation.
7.4 DIFFERENTIAL EQUATIONS SYSTEMS BY
APPROXIMATE METHODS
dx/dt=f(t,x,y)
dy/dt=g(t,x,y)
x(t0)=x0, y(t0)=y0
Euler considered the following law of recurrence for a set of points {(xn,yn)} the
approximate solution:
x(n+1)=x(n)+hf(t(n),x(n),y(n))
y(n+1)=y(n)+hg(t(n),x(n),y(n))
where t(n)=t0+nh
donde k1=f(t(n),x(n),y(n)),
k2=f(t(n)+h/2,x(n)+hk1/2,y(n)+hm1/2),
k3=f(t(n)+h/2,x(n)+hk2/2,y(n+hm2/2)
k4=f(t(n)+h,x(n)+hk3,y(n)+hm3)
m1=g(t(n),x(n),y(n)),
m2=g(t(n)+h/2,x(n)+hk1/2,y(n)+hm1/2),
m3=g(t(n)+h/2,x(n)+hk2/2,y(n)+hm2/2)
m4=g(t(n)+h,x(n)+hk3,y(n)+hm3)
the value of h is given.
Exercice 7-3. Solve the following system of differential equations by Euler method and
Runge-Kutta:
In[1]:=f[t_,x_,y_]:=x-y+1;
In[2]:=g[t_,x_,y_]:=x+3y+E^(-t);
In[3]:=h=0.1;
In[4]:=x[0]=0;
In[5]:=y[0]=1;
In[6]:=t[n_]=h n;
In[7]:=x[n_]:=x[n]=x[n-1]+h(f[t[n-1],x[n-1],y[n-1]])
In[8]:=y[n_]:=y[n]=y[n-1]+h(g[t[n-1],x[n-1],y[n-1]])
In[9]:=solucion=Table[{t[i],x[i],y[i]},{i,0,10}]
In[1]:=f[t_,x_,y_]:=x-y+1;
In[2]:=g[t_,x_,y_]:=x+3y+E^(-t);
In[3]:=h=0.1;
In[4]:=xrk[0]=0;
In[5]:=yrk[0]=1;
In[6]:=t[n_]=h n;
In[7]:=xrk[n_]:= Module[{k1,k2,k3,k4},
k1=f[t[n-1],xrk[n-1],yrk[n-1]];
m1=g[t[n-1],xrk[n-1],yrk[n-1]];
k2=f[t[n-1]+h/2,xrk[n-1]+(h k1)/2,yrk[n-1]+(h m1)/2];
m2=g[t[n-1]+h/2,xrk[n-1]+(h k1)/2,yrk[n-1]+(h m1)/2];
k3=f[t[n-1]+h/2,xrk[n-1]+(h k2)/2,yrk[n-1]+(h m2)/2];
m3=g[t[n-1]+h/2,xrk[n-1]+(h k2)/2,yrk[n-1]+(h m2)/2];
k4=f[t[n-1]+h/2,xrk[n-1]+(h k3),yrk[n-1]+(h m3)];
m4=g[t[n-1]+h/2,xrk[n-1]+(h k3),yrk[n-1]+(h m3)];
xrk[n]=xrk[n-1]+(h/6)(k1+2k2+2k3+k4)]
In[8]:=yrk[n_]:= Module[{k1,k2,k3,k4},
k1=f[t[n-1],xrk[n-1],yrk[n-1]];
m1=g[t[n-1],xrk[n-1],yrk[n-1]];
k2=f[t[n-1]+h/2,xrk[n-1]+(h k1)/2,yrk[n-1]+(h m1)/2];
m2=g[t[n-1]+h/2,xrk[n-1]+(h k1)/2,yrk[n-1]+(h m1)/2];
k3=f[t[n-1]+h/2,xrk[n-1]+(h k2)/2,yrk[n-1]+(h m2)/2];
m3=g[t[n-1]+h/2,xrk[n-1]+(h k2)/2,yrk[n-1]+(h m2)/2];
k4=f[t[n-1]+h/2,xrk[n-1]+(h k3),yrk[n-1]+(h m3)];
m4=g[t[n-1]+h/2,xrk[n-1]+(h k3),yrk[n-1]+(h m3)];
yrk[n]=yrk[n-1]+(h/6)(m1+2m2+2m3+m4)]
In[9]:=solution=Table[{t[i],xrk[i],yrk[i]},{i,0,10}]
called quasilinear equation, most of the time it will be possible to find a solution directly.
Otherwise it is used with Mathematica classical algebraic method of characteristics to
solve this type of equations. Likewise you can use the classic metods for other types
dieferenciales partial differential equations.
FirsIntegrals[{ec1,ec2,…},{u,v,..},x]
It gives a list of first integrals for the given system
in terms of u, v, ….. y x
-(-x-y+u)2 + x2ux - uy = 0
In[1]:=<<Calculus`PDSolve1`
In[2]:=DSolve[-(-x-y+u[x,y])^2+x^2*(D[u[x,y],x]-D[u[x,y],y])==0,
u[x,y],{x,y}]
{{u[x, y] ->
2
2 x + y - x C[1][x + y] - x y C[1][x + y]
-(––––––––––––––)}}
-1 + x C[1][x + y]
Now we will integrate the second partial differential equation:
{{w[x, y, z] ->
2 x
x y z Log[-]
y
-(–––––––—) +
2
-(x y) + y + x z - y z
2 x
x y z Log[-]
z 1 1 1 1
–––––––— + C[1][- - -, - - -]}}
2 x y x z
-(x y) + x z + y z - z
(x2 ux2 )/ y + u = uy
-u + (2+y) uy + x ux + 3 ux2 = 0
-c + b uy + a y ux + ux2 = 0
In[1]:=CompleteIntegral[Derivative[0,1][u][x,y]==(u[x,y]+
x^2*Derivative[1,0][u][x,y]^2)/y,u[x,y],{x,y}]
2 2
-B[1] B[1] Log[x] Log[x]
{{u[x, y] -> –– + y B[2] - –––— - ––-}}
4 2 4
CompleteIntegral[-u[x,y]+(2+y)*Derivative[0,1][u][x,y]+
x*Derivative[1,0][u][x,y]+3*Derivative[1,0][u][x,y]^2==0,
u[x,y],{x,y},IntegralConstants->F]
2
x F[1] F[1]
{{u[x, y] -> –– + –— + 2 F[2] + y F[2]}}
6 12
In attempting to solve the third equation with DSolve automatically it invokes the
function CompleteIntegral.
In[1]= DSolve[-c+b*Derivative[0,1][u][x,y]+
a*y*Derivative[1,0][u][x,y]+
Derivative[1,0][u][x,y]^2==0,u[x,y],{x,y}]
DSolve::nlpde:
This is a nonlinear partial differential equation.
General solution is not available. Trying to
build a complete integral instead.
{{u[x, y] ->
2 2
cy a y B[1] y B[1]
– + x B[1] - ––– - ––- + B[2] + y B[2]}}
b 2b b
Exercice 7-6. Calculating the first integral to the following system of differential
equations:
u’ = - u(u+v)
v’ = v(u+v)
u
{-Sqrt[u v] x + ArcTan[–––], u v}
Sqrt[u v]
7.6 ORTHOGONAL POLYNOMIALS
Two different functions f (x) and g (x) are said to be orthogonal on an interval [a, b] if
their inner product is 0, ie if
SphericalHarmonicY[l,m,n,a,b]
Let us now his relationship with the differential equations. Precisely this relationship
is what allows us to find solutions of certain nonlinear equations of higher order.
Chebyshev polynomials of the first specie are the solutions of the differential equation:
Legendre polynomials
Hermite polynomials
Laguerre polynomials
Jacobi polynomials
α, β > -1
Gegenbauer polynomials
3 5 7
-7 x + 56 x - 112 x + 64 x
In[2]:= LegendreP[6,x]
2 4 6
-5 + 105 x - 315 x + 231 x
–––––––––—
16
In[3]:= HermiteH[5,x]
3 5
120 x - 160 x + 32 x
In[4]:= LaguerreL[5,x]
2 3 4 5
120 - 600 x + 600 x - 200 x + 25 x - x
––––––––––––––
120
7.7 AIRY AND BESSEL FUNCTIONS
Are called Bessel functions linearly independent solutions of the differential equation:
2 2 2
y’’ + y’/x + (k - n /x ) y = 0 (Bessel equation)
Are called modified Bessel functions of the linearly independent solutions of the
differential equation:
2 2 2
y’’ + y’/x - (k + n /x ) y = 0 (modified Bessel equation)
AiryAiPrime[z] y AiryBiPrime[z]
BesselJ[n,z] y BesselY[n,z]
This is the Bessel differential equation for n = 1/2. We obtain two linearly
independent solutions as follows:
In[1]:= BesselJ[1/2,x]
1
Sqrt[2] Sqrt[–-] Sin[x]
Pi x
In[2]:= BesselY[1/2,x]
1
-(Sqrt[2] Sqrt[–-] Cos[x])
Pi x
APPENDIX I. NUMBERS, OPERATIONS AND
MOST COMMON FUNCTIONS
8.1 ARITHMETIC OPERATIONS IN MATHEMATICA
x+y sum
x-y difference
x * y or x y product
x/y division
x^y power
As examples we have:
In [1]: = 53 + 78
Out [1] = 131
In [4]: = 34 ^ 56
Out [4] = 5791877320528712784204425412617959985284096849205
6164062843692360166371779746690236416
1) -6 a b + 3 a ^ 2 + 2ab
2) 6 a ^ 2 + 2 a – b ^ 2 + 3 a-5 a ^ 2 + b ^ 2
(3) 6 a b - 2 a ^2 -4 a b + 3 a ^ 2
We asked Mathematica previous operations in the following way:
In[6] := ‑6 a b + 3 a^2 + 2 a b
2
Out[6]= - 4 a b + 3 a
2
Out[8]= a + 2 a b
Exercise 3. If H = 3 a ^ 2 - 2 a + 7, F = 6 a ^ 3 - 5 a + 2 y
G = 5 a ^ 2 + 4 a - 3; Calculate:
1) H + F + G
(2) H F + G
(3) F G H
In[10] := H ‑ F + G
2 3
Out[10]= 2 + 7 a + 8 a ‑ 6 a
In[11] := H ‑ F – G
2 3
Out [11] = 8 - a - 2 a - 6 a
8.2 PREDEFINED FUNCTIONS OF INTEGER
ARGUMENT
Mathematica incorporates the most typical functions with integer argument, among
which we mention the following:
Function meaning
––––––––––––––––––––––––––––––
FactorInteger[n] list of prime factors of n
Out[15] = 886163135
In[16]:= Binomial[n, 3]
(-2 + n) (-1 + n) n
Out[16]= –––––––––
6
In[17]:= Multinomial[7, 6]
Exercise 5. Find the rest of the division of 2 ^ 13451 between 3. Also find the
integer k such that k (3 ^(-1)) º 1 mod 7.
Exercise 6. Decomposed into prime factors the number 18.900, find all your
dividers and the number of prime numbers that are smaller than he. Also find the prime
number that is 18.900 place.
In[16] := FactorInteger[18900]
Out[16] = {{2, 2}, {3, 3}, {5, 2}, {7, 1}}
In[17] := Divisors[18900]
Out[17] = {1, 2, 3, 4, 5, 6, 7, 9, 10, 12, 14, 15, 18, 20, 21, 25, 27, 28,
30, 35, 36, 42, 45, 50, 54, 60, 63, 70, 75, 84, 90, 100, 105, 108, 126,
135, 140, 150, 175, 180, 189, 210, 225, 252, 270, 300, 315, 350, 378, 420, 450, 525, 540,
630, 675, 700, 756, 900, 945, 1050, 1260, 1350, 1575, 1890, 2100, 2700, 3150, 3780, 4725,
6300, 9450, 18900}
In[20] := PrimePi[18900]
Out[20] = 2150
In[21] := Prime[18900]
Out [21] = 211093
Exercise 7. Two books have 840 and 384 pages respectively. If they are formed by
booklets of equal number of leaves, and more than 18 leaves, calculate the number of
leaves of a booklet.
Exercise 8. To find the number N, than to divide it by 16, 24, 30 and 32 gives rest
5.
N-5 will be a multiple of 16, 24, 30 and 32, and as you are asked to calculate the lowest
number, then N-5 will be the minimum common multiple of 16, 24, 30 and 32
Mathematica work with different numbering systems have the following syntax:
RealDigits [x, b] returns the decimal digits places of the real number x in base b
In[1]:=BaseForm[2342424, 2]
Out[1]=1000111011111000011000
2
Exercise 10. Express in base-10 the number 100101 in base 2 and express in base
10 the hexadecimal number ffffaa00. Find in base 10, the result of the operation of
hexadecimal numbers fffaa2 + ff - 1
In[1]:= 2^^100101
Out[1]=37
In[2]:= 16^^ffffaa00
Out[2] = 4294945280
In [3]: = 16 ^ ^ fffaa2 + 16 ^ ^ ff - 1
Out [3] = 16776096
In[2]:= BaseForm[%, 5]
Out [2] = 1132403221320
5
282803061001316876
Out[1]= –––––––––––-
1027
In[2]:=BaseForm[%, 13]
56a96112b7a32757
13
Out[2]=–––––––––––-
610
13
In[3]:=N[%1]
14
Out[3]=2.75368 10
12
Out[4]:=b.a861 13
13
Exercise 13. Put in base 3 the decimal number 125,7864 and put in base 16 the
result of next operation: 8796,43 + 0 6789-4,25
In[1]:= BaseForm[125.7864, 3]
Out[1]=11122.2100200212
3
In[2]:= BaseForm[8796.43+0.6789-4.25, 16]
Out [2] = 2258. e
16
Exercise 14. Put in base 12 the result of the operation:
In[1]:= (4^^13.3-9^^888.7777)(16^^aaff10-12.3)/(15^^aabbc01-1)//N
Out[1]= -66.187
Exercise 15. A list of the decimal digits of the numbers in base 16,
145673214567005, and 145673214567005,241
In[1]:=IntegerDigits[145673214567005, 16]
Out[1]={8, 4, 7, 13, 3, 0, 3, 5, 2, 2, 5, 13}
In[2]:=RealDigits[145673214567005.241, 16]
Out [2] = {{8, 4, 7, 13, 3, 0, 3, 5, 2, 2, 5, 13, 3, 14}, 12}
8.4 RATIONAL NUMBERS
Mathematica can normally work with rational numbers according to the rules of Algebra.
Below are exercises that illustrate the operations with rational numbers.
In[1]:=3/5+2/5+7/5
Out[1]:=12/5
In[2]:=1/2+1/3+1/4+1/5+1/6
29
Out[2]:=–-
20
In[3]:=1/2-1/3+1/4-1/5+1/6
23
Out[3]:= –
60
In[4]:=(2/3-1/6)-(4/5+2+1/3)+(4-5/7)
137
Out[4]:= –-
210
In[5]:=(1/5*4/7/(4/3-2/5))/(6-(5/9-1/7)*5+1/4)
216
Out[5]:= ––
7385
In[6]:=((-1)^4/(2/5)^3)^(-2)/((-3/7)^2-(2/5)^(-1))
6272
Out[6]:= -(––––-)
3546875
In[7]:=(2-1/5)^2/(3-2/9)^(-1)/(6/7 5/4-(2/7)/(1/2))^3/(1/2-1/3)
Out[{7]:=432
8.5 IRRATIONAL NUMBERS
Mathematica can work normally with irrational numbers according to the rules of
Algebra. Below are exercises that illustrate the operations with irrational numbers.
Sqrt[2]
b) Sqrt[2] + 3 Sqrt[2] - –––—
2
1/2 1/3
(f) a a
1/5
(h) Sqrt [a a ]
a)
In[1]:= 3Sqrt[a]+2Sqrt[a]-5Sqrt[a]+7Sqrt[a]
Out[1]= 7 Sqrt[a]
b)
In[2]:= Sqrt[2]+3Sqrt[2]-(1/2)Sqrt[2]
1
Out[2]= -(–––—) + 4 Sqrt[2]
Sqrt[2]
c)
In[3]:= 4a^(1/3)-3b^(1/3)-5a^(1/3)-2b^(1/3)+ma^(1/3)
d)
In[4]:= Sqrt[432]+Sqrt[75]-Sqrt[363]-Sqrt[108]
Out[4]= 0
e)
f)
In[6]:=a^(1/2) a^(1/3)
5/6
Out[6]= a
g)
h)
In[8]:= Sqrt[a (a^(1/5))]
6/5
Out[8] = Sqrt[a ]
2
a) ––-
Sqrt[2]
Sqrt[2]
b) ––-
1/3
2
2
c) –-
1/3
4
3
d) ––-
SQRT [3]
(e) 2 /(Sqrt[2])
a)
In[1]:= 2/Sqrt[2]
Out[1] = Sqrt[2]
b)
In[2]:= Sqrt[2]/2^(1/3)
1/6
Out[2]:=2
c)
In[3]:= 2/4^(1/3)
1/3
Out[3]= 2
d)
In[4]:= 3/Sqrt[3]
Out [4] = Sqrt [3]
8.6 COMPLEX NUMBERS. MORE COMMON
FUNCTIONS
Mathematica incorporates functions for working with complex numbers. We then see
some examples:
In [12]: = Re [3 + 4 I]
Out [12] = 3
In [13]: = Im [3 + 4 I]
Out [13] = 4
In [14]: = Conjugate [3 + 4 I]
Out [14] = 3-4 I
In addition to these specific functions of a complex variable, there are many other
functions that can be applied to complex numbers.
For example, the Round[Z] function rounds both the real part and the imaginary of Z.
In [17]: = Round [2.7 - 8.5 I]
Out [17] = 3-8 I
There are many more features in Mathematica working with complex numbers, including
free, Cos, Ex and Log, etc. Some of the most important are presented in the following table:
Trigonometric functions
Function Inverse
–––––––––––––––
Sin [z] ArcSin [z]
COS [z] ArcCos [z]
Tan [z] ArcTan [z]
CSC [z] ArcCsc [z]
SEC [z] ArcSec [z]
Cot [z] ArcCot [z]
Hyperbolic functions
Function Inverse
–––––––––––––––—
Sinh [z] ArcSinh [z]
Cosh [z] ArcCosh [z]
Tanh [z] ArcTanh [z]
Csch[z] ArcCsch[z]
Sech[z] ArcSech[z]
Coth [z] ArcCoth [z]
Function
––––––––––––––-
ABS [z] module
ARG [z] argument
Conjugate [z] conjugate
Re [z] real part
IM [z] imaginary part
It should be noted that, as the real numbers are a special case of the complexes (zero
imaginary part), all functions defined above also are valid for real variable.
Exercise 19. Given the complex numbers: X = 3+2i, Y = 3-2i and Z = i^307,
calculate X+Y+Z, X * Y * Z, X/Y and Y/Z
In[3]:= (3+2I)/(3-2I)
5 12 I
Out[3]:= — + –-
13 13
In[1]:=(-3-3Sqrt[3] I)^3//N
-14
Out[1]:=216. - 3.15928 10 I
In[2]:=(2+2I)^2/(-3-3Sqrt[3]I)^90//N
-84 -70
Out[2]:= -6.77367 10 + 7.40419 10 I
i ^ 8 - i ^(-8)
––––- + i
2I-1
i
i 3+i i 1-i
i , i , i and (1+Ö3 i)
In[1]:= Abs[I^I]
-Pi/2
Out[1] = E
In[2] = Arg[I^I]
Out[2] = 0
In[3]:= Abs[I^(3+I)]
-Pi/2
Out[3]= E
In[4]:= Arg[I^(3+I)]
-Pi
Out[4]= –
2
In[5]:= Abs[(I^I)^I]
Out[5]:=1
In[6]:= Arg[(I^I)^I]
-Pi
Out[6]=–-
2
In[31]:= Rationalize[3.1415926536, 0]
Out [31] = 3926990817 / 1250000000
8.7 ROUNDING AND APPROACH FUNCTIONS
In[32]:= Round[2.574]
Out[32] =3
In[32]:= Round[2.41]
Out [32] = 2
In[33]:= Round[Sqrt[17]]
Out[33] = Round[Sqrt[17]]
In[34]:= Round[N[Sqrt[17]]]
Out[34] = 4
Chop [0.00000000002]
Out [35] = 0
Mathematica allows to represent constants more used < adas in practice. We have:
The example calls a distributed random integer uniformly in the range [0, 100] including
the ends.
Out [54] = 50
In[56]:= Needs[“Statistics`ContinuousDistributions`”]
In[57]:= Random[NormalDistributions[5]]
Out [57] = 4.05222
8.10 NUMBER THEORY PACKAGE
Mathematica enables a specific package of expansion of the theme.
· x = y = a, assigns the value “a” to the variables x and at the same time.
· {x, y} = {a1, a2}, assigns different values to x and y at the same time.
For example, we can define the variable x and assign it the value 6 in the following way:
In [1]: = x = 6
Out [1] = 6
Mathematica offers the possibility to define custom functions using the following syntax:
In [1]: = f [x_]: = x ^ 3
In[2]:= f[a+1]
3
Out[2]=(1+a)
In[3]:= f[4]
Out[3]= 64
In[4]:= f[3x+x^2]
23
Out[4]:= (3 x + x )
9.3 RECURSIVE FUNCTIONS
Recursive definitions of functions you can use in Mathematica. For example, to define
the function factorial of as follows:
In[18]:= fact[1]=1;
In[19]:= fact[n_]:=n*fact[n-1]
Also is you can set recursive functions on the basis of n values above and according to n
initial conditions. For example:
In [3]: = f [5]
Out [3] = 8
9.4 PIECEWISE FUNCTIONS
When the condition is true evaluates expression1, and when false expression2 is
evaluated.
As an application example we define the function:
In[12]:= Delta[x_] := If [x==0, 1, 0]
This function takes the value 1 for all x greater than 0, and takes the value 0 for all x less
than or equal to 0.
When it is necessary to control the function, rather than across a single condition, but of
several, is available the operator condicuional Wich with the following syntax:
If the conditioni is true the expressioni is evaluated (i=1, 2, …,n). Putting True
as the last condition, gets evaluate the last expression if none of the previous conditions
have been certain.
The function g to pieces at intervals is defined (-¥, - 3) (- 3, - 2), (- 2.2), (2.3), (3,¥).
Also we can graphically represent the function for the function g as follows:
We will now define a function, called rect, which is set to 1 in the interval [-1/3, 1/3] and
which is worth $ 0 in the rest of the real line. We will use the notation /, which can be
interpreted as “such that” or “whenever”.
In[22] := Clear[rect];
In[23] := rect[x_ /. (-1/3 <= x && x >= 1/3)] := 1
In[24] := rect[x_ /. Abs[x] > 1/3] := 0
9.5 OPERATIONS WITH FUNCTIONS
In[1]:= InverseFunction[ArcSin]
Out[1]= Sin
In[4]:= Composition[f,g][x]
Out[4:= 1+x^3+(1+x^3)^2
In[5]:= Composition[g,f][x-1]
Out[5]= 1+(-1+(-1+x)^2+x)^3
In[6]:= Composition[f,Sin][x]
Out [5] = [x] + Sin [x] ^ 2
In[9]:= Clear[f,g]
In[1]:=Nest[f, x, 4]
Out[1]= f[f[f[f[x]]]]
In[2]:= NestList[f, x, 4]
Out[2]= {x, f[x], f[f[x]], f[f[f[x]]], f[f[f[f[x]]]]}
In[20]:= Operate[p, t]
Out[20]= p[(1 + a) (1 + b)][x]
9.6 DATA TYPES USED IN THE DEFINITION OF THE
FUNCTIONS
We already know that in Mathematica there is no need to declare the type of the variables
or functions, as in C or FORTRAN. Mathematica manipulates data more as the APL
programming language: deducing the type of data based on how you are using. Each
expression that defines a function is composed of objects in one or more of the following
atomic types: Integer, Real, Rational, Complex, Symbol, and String. The following table
gives an example of each of these indivisible types and their descriptions
Exercise 1. Define the functions f (x) = x ^ 2, g (x) = x ^(1/2) and h (x) = x + Sin
(x). Calculate f(2), g(4), h(pi/2), f(a-b^2) and ((x+h) - f (x) f) / h
In[1]:= clear[f, g, h]
2
Out[1]= x
In[2]:= g[x_]= Sqrt[x]
Out[2]= Sqrt[x]
In[4]:= f[2]
Out[4]= 4
In[5]:= g[4]
Out[5]= 2
In[6]:= h[Pi/2]
Out[6]= 1 + Pi/2
In[7]:= f[a-b^2]
22
Out[7]= (a- b )
In[8]:= (f[x+h]-f[x])/h
2 2
-x + (h + x)
Out[8]=–––––––––
h
2 2 2 2
Out [2] = (Cos [x - y]-Sin [x - y])
In[3]:= h[1,2]
Out[3]= (Cos[3], Sin[3])
In[4]:= h[-Pi,Pi]
Out[4]= (1, 0)
In[1]:= Clear[f,g,h]
In[1]:= Nest[f,x,3]
2 2 2 2 22 2
Out[1]:= x + x + (x + x ) + (x + x + (x + x ) )
In[1]:= Clear[f]
In[2]:= f[x_] := f[x] = f[x - 1] + f[x -2];
In [3]: = f [0] = f [1] = 1
Out [3] = 1
Exercise 6. Define a function f (x), which is set to 1 in the interval [-1/2, 1/2] and
that is set to 0 if x is greater than 1/2, and that it is set to - 1 if x is less than - 1/2.
Represent it graphically.
We will use the notation /, which can be interpreted as such that, or whenever (some
versions of the program have this option obsolete).
In[1]:= Clear[f]
Exercise 7. Calculating the compose function of f (x) = [x+Cox [x]] and its inverse.
Verify that the result is correct.
In[1]:= Clear[f]
In[2]:= f[x_]:= Sin[x+Cox[x]];
In[3]:= g[x_]:=InverseFunction[f][x]
In[4]:= Composition[f, g][x]
Out [4] = x