Computer exercise n. 2

5 downloads 136 Views 38KB Size Report
Computer exercise n. 2. Theme: Polynomials and fractional rational function. Syllabus of exercise: 1. Operation with polynomials. 2. Fractional rational function .
Computer exercise n. 2 Theme: Polynomials and fractional rational function Syllabus of exercise:

1. Operation with polynomials 2. Fractional rational function

2.1 Operation with polynomials 2.1.1 Calculation of the functional value of the polynomial Task n. 1: Calculate of the functional value of the polynomial p = x 5 − 7 x 4 + 8 x 3 − 5 x 2 + 14 x − 31 for x = 3; x = 7; x = −3; x = −11 . Solution: p=[1,-7,8,-5,14,-31] polyval(p,3) polyval(p,7) polyval(p,-3) polyval(p,-11)

-142 2566 -1144 -274976

Result: This polynomial has for pro x = 3 functional value –142, for x = 7 functional value 2 566, for x = −3 functional value –1 144, for x = −11 functional value –274 976. Task n. 2: Modification Calculate of the functional value of the polynomial p = x 5 − 7 x 4 + 8 x 3 − 5 x 2 + 14 x − 31 for all values which take the values from 1 to 20 with equidistant step 1 and save those values into the file vysl.txt. cyklus.m p=[1,-7,8,-5,14,-31]; for i=1:20, x(i)=polyval(p,i); end;

We enter the commands to MATLAB: cyklus; dlmwrite('vysl.txt',x);

Result: We find all calculated values in the file vysl.txt in the directory work. 2.1.2 Addition of the polynomials Task n. 3: There are polynomials p = x 7 − 5 x 4 + 7 x 3 − 14 x − 31 , q = 5 x 4 − 5 x 2 + 14 x − 11 and r = −5 x 7 + 11x 4 + 4 x 3 − 5 x 2 + 1 . Find the addition of these polynomials. Solution: Attention! All polynomials must have the same degree ⇒ complete in front with zeros. p=[1,0,0,-5,7,0,-14,-31] q=[0,0,0,5,0,-5,14,-11] r=[-5,0,0,11,4,-5,0,1] p+q+r ans = -4 0 0 11

11

-10

0

-41

Result: The addition of these polynomials is the polynomial − 4 x 7 + 11x 4 + 11x 3 − 10 x 2 − 41 .

2.1.3 Multiplication of the polynomials Annotation. We must not complete with zeros to be the same degree of the polynomials. Task n. 4: There are polynomials p = 13 x 3 − 14 x − 3 , q = −7 x 2 + 4 x − 9 and r = 8 x 2 + x . Find the multiplication of these polynomials. Solution: p=[13,0,-14,-3] q=[-7,4,-9] r=[8,1,0] s=conv(p,q) s=conv(s,r)

s = -91 s = -728

52 325

-19 -100

-35 -299

114 877

s = 0 -91 s = 0 0

52 -728

-19 325

-35 -100

114 -299

27 330

27

0

Or p=[13,0,-14,-3] q=[0,-7,4,-9] r=[0,8,1,0] s=conv(p,q) s=conv(s,r)

27 877

330

27

0

Result: The multiplication of these polynomials is the polynomial − 728 x 7 + 325 x 6 − 100 x 5 − 299 x 4 + 877 x 3 + 330 x 2 + 27 x . 2.1.4 Division of the polynomials Attention! It is impossible to divide by zero ⇒ it is impossible to use the notation as with the addition of the polynomials i.e. complete in front with zeros. The first number also must be nonzero. Task n. 5: There are polynomials p = 6 x 4 + x 3 − 10 x 2 + 19 x + 24 , q = 2 x 2 + 5 x + 3 . p Find the division s = of these polynomials. q Solution: p=[6,1,-10,19,24] q=[2,5,3] deconv(p,q)

ans = 3

-7

8

Result: The division of these polynomials is the polynomial 3 x 2 − 7 x + 8 . Attention! If we do not know that the polynomial q divides the polynomial p (the remainder is 0), so we must use the command deconv different way. Mechanical using the command deconv (as the task n. 4) we do not know if the remainder after division exists or does not exist. Task n. 6: There are polynomials p = 6 x 4 + x 3 − 10 x 2 + 19 x + 24 , q = x 2 + x + 3 . Determine if the polynomial q divide the polynomial p, determine the quotient and the remainder after the division of these polynomials. Solution: p=[6,1,-10,19,24] q=[1,1,3] [s,r]=deconv(p,q)

s = 6 r = 0

-5 0

-23 0

57

93

Result: The polynomial q does not divide the polynomial p without the remainder. The quotient is s = 6 x 2 − 5 x − 23 and the remainder is r = 57 x + 93 . (6 x 4 + x 3 − 10 x 2 + 19 x + 24) : (x 2 + x + 3) = 6 x 2 − 5 x − 23 + 572 x + 93 . x + x+3

Proof: k=conv(s,q)+r

k = 6

1

-10

19

24

Really the polynomial k is the same as the polynomial p.

2.1.5 Finding of the roots of the polynomial of the random degree Task n. 7: Find the roots of the polynomial p = 3 x 3 − 27 x 2 + 78 x − 72 and decompose into simple factors. Solution: p=[3,-27,78,-72] roots(p) ans = 4.0000 3.0000 2.0000

Result: Polynomial p has three different real roots x1 = 4; x 2 = 3; x3 = 2 and decomposition into simple factors is 3 x 3 − 27 x 2 + 78 x − 72 = 3( x − 4 )(x − 3)( x − 2 ) .

2.1.6 Derivative and integration of the polynomial of the random degree Task n. 8: Calculate the derivative and primitive function to polynomial function f : y = 3 x 8 − 7 x 7 + 14 x 3 + 5 x 2 + 2 x − 7 . Solution: p=[3,-7,0,0,0,14,5,2,-7] k=polyder(p) k = 24 -49 0 0 l=polyint(p) l = 0.3333 -0.8750 0 0

0.3333 = 0. 3 =

0 0

42

3.5000

10 1.6667

2 1.0000

-7.0000

0

3 1 = 9 3

875 175 35 7 =− =− =− 1000 200 40 8 6 2 5 1.6667 = 1. 6 = 1 + = 1 + = 9 3 3 Result: The derivative this function is y ′ = 24 x 7 − 49 x 6 + 42 x 2 + 10 x + 2 and 1 7 7 5 the primitive function is Y = x 9 − x 8 + x 4 + x 3 + x 2 − 7 x + C . 3 8 2 3 Annotation – How to convert periodic decimal to common fraction. − 0.8750 = −0.875 = −

We can purely periodic decimal expansion a (0 < a ≤ 1) to write as common fraction which numerator is the period and denominator is positive number with decimal notation which contain exactly as many of the nine as the period has digits. 592 16 0.592 = = 999 27 We can periodic decimal expansion which is not purely a (0 < a ≤ 1) to write as common fraction. Its numerator is the difference. Its minuend is positive integer number in decimal notation which contains exactly numbers of the before-period and of the period in the same order as in the number notation. Its subtrahend is before-period. The denominator is the number in decimal notation which contains the first exactly as many of the nine as period has digits 64 096 − 640 63 456 2 644 and then exactly as many of the zeros follows as before-period has digits. 0.64096 = = = . 99 000 99 000 4 125

Or f=sym('3*x^8-7*x^7+14*x^3+5*x*x+2*x-7') diff(f) ans = 24*x^7-49*x^6+42*x^2+10*x+2 int(f) ans = 1/3*x^9-7/8*x^8+7/2*x^4+5/3*x^3+x^2-7*x

Result: The derivative this function is y ′ = 24 x 7 − 49 x 6 + 42 x 2 + 10 x + 2 and 1 7 7 5 the primitive function is Y = x 9 − x 8 + x 4 + x 3 + x 2 − 7 x + C . 3 8 2 3

2.2 Fractional rational function 2.2.1 Decomposition of the purely fractional rational function into partial fractions Task n. 9: 5x + 1 Express purely fractional rational function 2 in partial fractions. x + x−2 Annotation – Using this decomposition in chemistry. In reaction kinetics when we solve kinetic equations reaction the second order we have the case of the irreversible reaction of the one molecule initial matter A with one molecule initial matter B when the product is created (identification A + B → product). The reaction speed is directly proportional to concentration of the initial matters. In our case is v = k ⋅ c A ⋅ c B , where c A , c B are concentrations of the initial matters A, B. If we indicate of the reaction quantum x, then we can write: x = c A0 − c A a x = c B0 − c B , whereas c A0 , c B0 are initial concentrations of the initial matters A, B. Because x

∫ (c 0

v=

dx , so dt

(

)(

)

dx = k cA − x cB − x ⇒ dt 0

0

dx = k ⋅ dt and we solve integral c A − x cB − x

(

0

)(

0

)

t

A0

dx = − x c B0 − x

)(

)

∫ k ⋅ dt . The first we must decompose the purely fractional rational function on the left side into 0

partial fraction to find the primitive function to it and then integrate it.

Solution: The first we find the roots of the denominator p=[1,1,-2] roots(p) ans = -2 1

Then we decompose the denominator into simple factors ( x + 2 )( x − 1) 5x + 1 A B Decomposition is 2 = + ⇒ 5 x + 1 = A( x − 1) + B ( x + 2 ) x + x − 2 x + 2 x −1 Substitution method: x = 1 ⇒ 6 = 3B ⇒ B = 2 x = −2 ⇒ −9 = −3 A ⇒ A = 3 Comparison of the coefficients: A + B = 5 -A + 2B = 1 a=[1,1;-1,2]; b=[5;1] a\b ans = 3 2

Decomposition into partial fractions

5x + 1 3 2 = + . x + x − 2 x + 2 x −1 2

Or When we indicate the purely fractional rational function

B( x ) and we enter: A( x )

A=[1,1,-2] B=[5,1] [r,p,k]=residue(B,A) r = 3 2 p = -2 1 k = []

5x + 1 3 2 = + . x + x − 2 x + 2 x −1 Result: So

2

Decomposition into partial fractions is

5x + 1 3 2 = + . x + x − 2 x + 2 x −1 2

Task n. 10: Express purely fractional rational function

− x 3 − 3 x 2 + 25 x + 5 in partial fractions. x 4 − 4x3 + 2x 2 + x + 6

Solution: p=[1,-4,2,1,6] roots(p) ans = 3.0000 2.0000 -0.5000 + 0.8660i -0.5000 - 0.8660i q=[1,-3] s=deconv(p,q) s = 1 -1 -1 -2 r=[1,-2] s=deconv(s,r) s = 1 1 1

(

)

Decomposition of the denominator into simple factors ( x − 3)( x − 2 ) x 2 + x + 1

− x − 3 x + 25 x + 5 A B Cx + D = + + 2 . 4 3 2 x − 4x + 2x + x + 6 x − 3 x − 2 x + x + 1 A( x − 2 ) x 2 + x + 1 + B ( x − 3) x 2 + x + 1 + (Cx + D ) x 2 − 5 x + 6 = − x 3 − 3 x 2 + 25 x + 5 3

2

Decomposition into partial fractions

(

)

conv(r,s) ans = 1 conv(q,s) ans = 1

(

(

-1

-1

-2

-2

) (

)

(

)

-2 -3

) (

)

(

)

A x − x − x − 2 + B x − 2 x − 2 x − 3 + C x 3 − 5x 2 + 6 x + D x 2 − 5x + 6 = 3

2

3

2

− x − 3 x + 25 x + 5 3

2

A + B + C = -1 -A – 2B - 5C + D = -3 -A – 2B + 6C - 5D = 25 -2A – 3B + 6D = 5 a=[1,1,1,0;-1,-2,-5,1;-1,-2,6,-5;-2,-3,0,6] a = 1 1 1 0 -1 -2 -5 1 -1 -2 6 -5 -2 -3 0 6 b=[-1;-3;25;5];

a\b ans = 2.0000 -5.0000 2.0000 -1.0000

− x 3 − 3 x 2 + 25 x + 5 2 5 2x − 1 = − + 2 . Decomposition into partial fractions is 4 3 2 x − 4x + 2x + x + 6 x − 3 x − 2 x + x + 1 Or A=[1,-4,2,1,6]; B=[-1,-3,25,5]; [r,p,k]=residue(B,A) r = 2.0000 -5.0000 1.0000 + 1.1547i 1.0000 - 1.1547i p = 3.0000 2.0000 -0.5000 + 0.8660i -0.5000 - 0.8660i k = []

So

2 5 1 + 1,154 7 i 1 − 1,154 7 i − + + x − 3 x − 2 x − (− 0,5 + 0,866i ) x − (− 0,5 − 0,866 i )

p=[1,0.5+0.866i]; q=[1,0.5-0.866i]; conv(p,q) ans = 1.0000 1.0000 1.0000 a=1+1.1547i; b=1-1.1547i; s=a*p s = 1.0000 + 1.1547i -0.5000 + 1.4434i r=b*q r = 1.0000 - 1.1547i -0.5000 - 1.4434i s+r ans = 2.0000 -0.9999

When we add the two last partial fractions we get asked decomposition: 2 5 2x − 1 − + 2 x − 3 x − 2 x + x +1 Attention! If we do not know the complex numbers exactly the result must not be also integer number (s. the second coefficient). Result: − x 3 − 3 x 2 + 25 x + 5 2 5 2x − 1 Decomposition into partial fractions is 4 = − + 2 . 3 2 x − 4x + 2x + x + 6 x − 3 x − 2 x + x + 1