Sie sind auf Seite 1von 12

Notes from the lecture of August 30, 2001 - CEE 6510 - Numerical and statistical methods in Civil

Engineering
Notes on polynomial operations
First, we define polynomias pol1 and pol2:
> pol1:=-x^5+9*x^4-18*x^3+x^2+19*x-10;
pol1 := −x5 + 9 x4 − 18 x3 + x2 + 19 x − 10
> pol2:=x-2;
pol2 := x − 2
To factor polynomial pol1 use:
> factor(pol1);
−( x − 1 ) ( x − 2 ) ( x + 1 ) ( x2 − 7 x + 5 )
Form a fraction with polynomial pol1 as numerator and polynomial pol2 as denominator:
> pol1/pol2;
−x5 + 9 x4 − 18 x3 + x2 + 19 x − 10
x−2
Function simplify can be used to simplify algebraic equations like the fraction formed above
> simplify(%);#Recall that % stands for "the last result"
− x4 + 7 x 3 − 4 x2 − 7 x + 5
Using function factor you can see that the factor (x-2) has been eliminated from the numerator
> factor(%);
−( x − 1 ) ( x + 1 ) ( x2 − 7 x + 5 )
Function divide can be used to determine if pol1 divides exactly by pol2. If that is the case, the result is true
and the quotient,pol1/pol2, gets stored in variable q:
> divide(pol1,pol2,'q');
true
To see the contents of q use:
> q;
− x4 + 7 x 3 − 4 x2 − 7 x + 5
Now, define pol3 and check if pol1 divides exactly into pol3. The results in this case is false, and the quotient
is not stored into q1
> pol3:=x-3;
pol3 := x − 3
> divide(pol1,pol3,'q1');
false
> q1;
q1
An attempt to simplify pol1/pol3 produces the following fraction:
> simplify(pol1/pol3);
x5 − 9 x4 + 18 x3 − x2 − 19 x + 10

x−3
Factoring out the fraction resulting from pol1/pol2 indicates that there are no common factors between the
numerator and denominator:
> factor(pol1/pol3);
( x − 1 ) ( x − 2 ) ( x + 1 ) ( x2 − 7 x + 5 )

x−3
Division of a polynomial p1 by another polynomial p2, when the degree of p1 is larger than that of p2, can be
written as:
p1 r
=q+ ,
p2 p2
where q is the quotient (a polynomial) and r is the residual (another polynomial).
To obtain an expansion involving quotient and residual as shown above, use the function convert as follows:
> F:=convert(pol1/pol3,parfrac,x);
56
F := −x4 + 6 x3 + x + 22 +
x−3
In this call to function convert we use three arguments: the fraction to be simplified (in this case, pol1/pol3),
the option parfrac -- which stands for partial fractions --, and the independent variable for the polynomials (in
this case, x).
A second example of expanding a fraction using convert and parfrac is follow next:
> p1:=x^4+y^3+x*y; p2:=x-3;
p1 := x4 + y3 + x y
p2 := x − 3
> convert(p1/p2,parfrac,x);
y3 + 3 y + 81
x3 + 3 x2 + 9 x + y + 27 +
x−3
To find more information about function convert use:
> ?convert

Taylor series expansions


One of the examples in the help for convert uses the function series which is used to obtain Taylor series
expansions of functions. The Taylor series expansion of a function f(x) about the point x=a is defined as:

∞ [n]
( x − a )n

f
f( x ) =
n=0
n!

[n] dn [0]
where f stands for n
= f( x ).
f( x ) evaluated at x=a, with f
dx
A Taylor series is an expression containing an infinite number of components. For practical calculations we can
write
 m f[ n ] ( x − a )n 
f( x ) =  ∑  + R( x ),

n=0 n! 
where R(x) is referred to as the residual or error in the expansion. The residual or error is typically expressed
by an order of magnitude, so that we write
 m f[ n ] ( x − a )n 
f( x ) = ∑  O( x( m + 1 ) ) .
 +
n=0 n! 
The summation corresponding to the first term in the right-hand side of this equation is referred to as a Taylor
polynomial, so that f(x) is approximated by this polynomial of order m, i.e.,
[n]
( x − a )n
m


f
f( x ) ~ p( x ) =
n=0
n!

(m + 1) (m + 1)
The term O( x ) indicates that the error in the approximation f(x)~p(x) is of the order of x .

Taylor series expansions in Maple


To obtain the Taylor series expansion of a function f(x) in Maple we use the function series. This function
requires three arguments: the function to expand (i.e., f(x)), the independent variable (i.e., x) and expansion
point (i.e., a) combined into the expression x=a, and the order of the error in the expansion (i.e., m+1). For
example, a Taylor series expansion of a generic function f(x) about x=0 with error of order 5 is obtained by
using:
> restart:series(f(x),x=0,5);
1 (2) 1 (3) 1 (4)
f( 0 ) + D( f )( 0 ) x + (D ) ( f ) ( 0 ) x2 + ( D )( f )( 0 ) x 3 + ( D ) ( f ) ( 0 ) x 4 + O( x 5 )
2 6 24
The Taylor series expansion of the function exp(x) about x=0 with a residual of order 10 is given by:
> series(exp(x),x,10);#Note:If a=0 you can simply use x as the second argument in the function call.
1 1 1 4 1 5 1 6 1 7 1 1
1+x+ x2 + x3 + x + x + x + x + x8 + x9 + O( x10 )
2 6 24 120 720 5040 40320 362880
Taylor series expansions can also be found by using the function taylor, e.g.,
> taylor(tan(x),x=Pi,5);
1
x−π+
( x − π ) 3 + O( ( x − π ) 5 )
3
Notice that the arguments of the function taylor are the same as in the function series. To find more
information about the function taylor use:
> ?taylor
In the following example we use only two arguments in the call to function taylor. The result is a polynomial or
order 5 and a residual of ordr 6.
> taylor(ln(x),x=1);
1 1 1 1
x−1− ( x − 1 )2 + ( x − 1 ) 5 + O( ( x − 1 ) 6 )
( x − 1 )3 − ( x − 1 )4 +
2 3 4 5
In this case, the order of the residual (m+1) is determined by the value of the environment variable Order. To
find out what the default value of Order is, use:
> Order;#Note: Environment variables typically start with an upper-case letter
6
The default value of Order is 6. You can change the value of Order by assigning a new value to it, e.g.,
> Order:=10;
Order := 10
With an Order value of 10, the Taylor series expansion obtained earlier is now:
> taylor(ln(x),x=1);
1 1 1 1 1 1 1 1
x−1− ( x − 1 )2 + ( x − 1 )3 − ( x − 1 )4 + ( x − 1 )5 − ( x − 1 )6 + ( x − 1 )7 − ( x − 1 )8 + ( x − 1 )9 +
2 3 4 5 6 7 8 9
O( ( x − 1 )10 )
Using the command restart will reset the value of the environment variable Order to 6, e.g.,
> restart:taylor(ln(x),x=1);
1 1 1 1
x−1− ( x − 1 )2 + ( x − 1 )3 − ( x − 1 )4 + ( x − 1 ) 5 + O( ( x − 1 ) 6 )
2 3 4 5

Some operations with matrices


The following exercises were used to illustrate differences between Maple and SCILAB. The following
statement defines a 3x3 matrix, with the matrix elements listed by rows:
> A:=matrix(3,3,[1,2,3,4,5,2,-1,0,1]);
1 2 3
 
A :=  4 5 2
 
-1 0 1
To perform operations on matrices we need to load the linear algebra (linalgI) package:
> with(linalg);
[ BlockDiagonal , GramSchmidt , JordanBlock , LUdecomp, QRdecomp , Wronskian, addcol, addrow, adj, adjoint,
angle, augment , backsub , band, basis, bezout, blockmatrix, charmat, charpoly , cholesky, col, coldim, colspace,
colspan, companion , concat, cond, copyinto , crossprod, curl, definite, delcols, delrows, det, diag, diverge, dotprod,
eigenvals, eigenvalues , eigenvectors, eigenvects, entermatrix, equal, exponential , extend, ffgausselim, fibonacci,
forwardsub, frobenius, gausselim, gaussjord, geneqns , genmatrix, grad, hadamard , hermite, hessian, hilbert,
htranspose , ihermite, indexfunc , innerprod , intbasis, inverse, ismith, issimilar, iszero, jacobian , jordan, kernel,
laplacian , leastsqrs, linsolve, matadd, matrix, minor, minpoly, mulcol, mulrow, multiply, norm, normalize, nullspace,
orthog, permanent , pivot, potential, randmatrix, randvector, rank, ratform, row, rowdim, rowspace, rowspan, rref,
scalarmul, singularvals, smith, stackmatrix, submatrix, subvector, sumbasis, swapcol, swaprow, sylvester, toeplitz,
trace, transpose, vandermonde , vecpotent , vectdim, vector, wronskian ]
One of the functions available in the linalg package is the inverse function that calculates the inverse of a
matrix:
> inverse(A);
 5 -1 -11 
 
 8 4 8 

 5 
 -3 1
 
 4 2 4 
 
 5 -1 -3 
 
 8 4 8 

Complex numbers
There was a question related to complex numbers at this point in the lecture. The unit imaginary number in
Maple is given by the upper-case letter I:
> I;
I
Here is an example of a complex number in Maple:
> z1:=3+5*I;
z1 := 3 + 5 I
Functions of complex numbers are available in Maple, howver, being a symbolic environment, symbolic
solutions -- like the next -- are more likely:
> log(z1);
ln( 3 + 5 I )
To obtain the value of the function use evalf (evaluate as a floating point number):
> evalf(%);
1.763180262 + 1.030376826 I

Differential equations in Maple


There was a question regarding a comparison between Maple and SCILAB in the class. I indicated that Maple is
more powerful for solving symbolic problems such as obtaining the symbolic (closed-fom) solution to an
ordinary differential equation (ODE). I refer you to the example in the handout on page 14:
* First, define the differential equation [diff indicates a derivative, x$2 indicates the second derivative with
respect to x]:
> eq1:=diff(y(x),x$2)+y(x)=0;
 ∂2 
eq1 :=  2 y( x )  + y( x ) = 0
 ∂x 
Use function dsolve to obtain a general solution to the ODE
> sol1:=dsolve(eq1,y(x));
sol1 := y( x ) = _C1 sin( x ) + _C2 cos( x )
We can include some boundary conditions in the solution, e.g.,:
> bc1s:=y(0)=1,y(1)=0;
bc1s := y( 0 ) = 1, y( 1 ) = 0
> sol2:=dsolve({eq1,bc1s},y(x));
cos( 1 ) sin( x )
sol2 := y( x ) = − + cos( x )
sin( 1 )
Because of the symbolic nature of Maple, the result is given in terms of cos(1) and sin(1). To obtain floating
point values use:
> evalf(sol2);
y( x ) = −.6420926160 sin( x ) + cos( x )

Functions of two variables


We return to page 10 in the handout and show the example for plotting the function f(x,y) = x sin y + y sin x:
> restart;
> f:=(x,y) -> x*sin(y)+y*sin(x); #Use Maple's arrow function
f := ( x, y ) → x sin( y ) + y sin( x )
> f(x,y);#This is the expression of the function
x sin( y ) + y sin( x )
> f(3,2);evalf(%);#Evaluating the function at point x = 3, y = 2, in two steps.
3 sin( 2 ) + 2 sin( 3 )
3.010132296
> evalf(f(3,2));#Evaluating the function at point x = 3, y = 2, in a single step.
3.010132296
> plot3d(f(x,y),x=-2*Pi..2*Pi,y=-2*Pi..2*Pi); #A plot of the function

Projectile motion example


There was a question in class regarding the solution of projectile motion equations shown in pages 8 and 9.
Here is that example:
> #page 8 - projectile motion example
> restart;
First, define the equations.
> ePX:=x=x0+v0*cos(theta)*t;
ePX := x = x0 + v0 cos( θ ) t
> ePY:=y=y0+v0*sin(theta)*t-(1/2)*g*t^2;
1
ePY := y = y0 + v0 sin( θ ) t − g t2
2
Provide values of the known parameters:
> v0:=25; theta:=45*Pi/180;x0:=10;y0:=15;g:=32.2;t:=23;
v0 := 25
1
θ := π
4
x0 := 10
y0 := 15
g := 32.2
t := 23
Let's see how the equations look after defining these parameters, and obtain a solution for x and y:
> ePX;ePY;
575
x = 10 + 2
2
575
y = −8501.900000 + 2
2
> solve({ePX,ePY},{x,y});
{ x = 416.5863992 , y = -8095.313601 }
Now, re-define the equations and solve a different problem:
> restart;
> ePX:=x=x0+v0*cos(theta)*t;ePY:=y=y0+v0*sin(theta)*t-(1/2)*g*t^2;
ePX := x = x0 + v0 cos( θ ) t
1
ePY := y = y0 + v0 sin( θ ) t − g t2
2
> v0:=25; x0:=10;y0:=15;g:=32.2;x:=300;y:=-2500;
v0 := 25
x0 := 10
y0 := 15
g := 32.2
x := 300
y := -2500
After defining the known parameters the equations to solve are:
> ePX;ePY;
300 = 10 + 25 cos( θ ) t
-2500 = 15 + 25 sin( θ ) t − 16.10000000 t2
> solve({ePX,ePY},{t,theta});
{ t = -12.83518367 , θ = -2.699283745 }, { θ = 2.814084890 , t = -12.25118741 },
{ θ = -.3275077634 , t = 12.25118741 }, { θ = .4423089085 , t = 12.83518367 }
A numerical solution is also possible. The function to use is fsolve which requires that the user provide ranges
of values of the unknowns.
> fsolve({ePX,ePY},{t,theta},0..10,0..20);
{ θ = .4423089085 , t = 12.83518367 }
Changing the ranges for the unknowns may produce different solutions:
> fsolve({ePX,ePY},{t,theta},0.5..20,5.0..20.0);
{ θ = 5.955677544 , t = 12.25118741 }
To find more information about the function fsolve use:
> ?fsolve
> fsolve({ePX,ePY},{t,theta},0..10,0..20);#Repeat first solution
{ θ = .4423089085 , t = 12.83518367 }
> subs(%,{ePX,ePY});
{ 300 = 10 + 320.8795918 cos( .4423089085 ), -2500 = −2637.345231 + 320.8795918 sin( .4423089085 ) }
> evalf(%);
{ 300. = 300.0000001 , -2500. = -2500.000000 }
> Digits;#Environment variable that determines the number of digits in a result -- the default value is:
10
> Digits:=20;#Change Digits to 20
Digits := 20
> restart; #Using restart will reset Digits to its default value of 10
> Digits;#default value = 10
10

Plotting more than one function


> #back to page 10
> #Plotting more than one function
> restart;
> f:=x->x^2-5*x+2;
f := x → x2 − 5 x + 2
> g:=x->2*x-3;
g := x → 2 x − 3
> plot({f(x),g(x)},x=0..20);
Fancier plot:
> plot({f(x),g(x)},x=0..10,style=point,symbol=cross,axes=boxed,color=RED,labels=["x(m)","y(m)"],title="Exer
cise 1");
> #Producing and storing plots separately
> plot({f(x),g(x)},x=0..10,style=point,symbol=cross,axes=boxed,color=RED,labels=["x(m)","y(m)"],title="Exer
cise 1");

> plot1:=%:#Store previous plot into variable plot1


> plot(g(x),x=-10..10,style=line,linestyle=4);
> plot2:=%:#Store previous plot into variable plot2
> plots[display]({plot1,plot2},labels=["x(ft)","y(miles)"]); #Display both plots in the same set of axes

Plotting two bi-variate functions in the same set of 3D axes


> restart;
> f:=(x,y)->x^2+y^2-25;g:=(x,y)->x^2-y^2-5;
f := ( x, y ) → x2 + y2 − 25
g := ( x, y ) → x2 − y2 − 5
> plot3d({f(x,y),g(x,y)},x=-10..10,y=-10..10,axes=boxed);

Simultaneous solution of f(x,y) = 0 and g(x,y) = 0 to find points of intersection -- Note that the solution is given
in terms of the function RootOf. To get actual solutions out of RootOf use the function allvalues:
> solve({f(x,y)=0,g(x,y)=0},{x,y});
{ x = RootOf( _Z2 − 15 ), y = RootOf( −10 + _Z2 ) }
> allvalues(%);

{y = 10 , x = 15 }, { y = − 10 , x = 15 }, { y = 10 , x = − 15 }, { y = − 10 , x = − 15 }

Das könnte Ihnen auch gefallen