You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 52 Next »


Here, we will aim to provide you with a number of exercises and solutions with a varying degree of difficulty. There will also occur exercises that requires you to use functions that has not yet been described in Symbolic mathematics.

If you have a suggestion on a topic you would like to see some more exercises (or tutorials) on, please send us a mail or leave a post on our Forum. We are more than happy to recieve feedback.

Page content

Expand

Exercise 1

Expand the expression

Solution
from sympy import *

x, y = symbols('x y')
f = (x + y)*(x - y)
ans = expand(f)
print(ans)


# Output:
x**2 - y**2

Exercise 2

Expand the expression

Solution
from sympy import *

x, y = symbols('x y')
f = (x**2 + y**2 + cos(x))*(4*x*y - 8*x + 10)
ans = expand(f)
print(ans)


# Output:
4*x**3*y - 8*x**3 + 10*x**2 + 4*x*y**3 - 8*x*y**2 + 4*x*y*cos(x) - 8*x*cos(x) + 10*y**2 + 10*cos(x)

Integration

Exercise 1: Single definite integral

This example is taken from the 2018 exam in TMA4100 - Calculus 1, check out the exam and the solution for hand calculations (only in norwegain). This is task 2.

Compute the intregral

Solution
from sympy import *

# First we define our variable
x = Symbol('x')
# We then compute the answer to our given interval
ans = integrate(x*log(x**2), (x, 1, 4))     # log() without specifying the base equals ln()
# Finally, we print the answer
print(ans)
print(float(ans))   # Since the answer includes a constant, we have to specify if we want it as a float


# Output:
-15/2 + 8*log(16)	# log() is here the natural logarithm
14.68070977791825

Exercise 2: Double definite integral

Compute the integral

Solution
from sympy import *

# First we define our variables
x, y = symbols('x y')

# To make our code more readable, we will define the function we will integrate first.
f = x**3*exp(y)

# The integrate function is able to take multiple integrations as parameteres.
# The different integrations are listed according to the integration order: dy -> dx
ans = integrate(f, (y, x**2, 9), (x, 0, 3))

print(ans)
print(float(ans))   # Since the answer includes a constant, we have to specify if we want it as a float


# Output:
-1/2 + 65*exp(9)/4
131674.6138231

Exercise 3: Triple definite integral

Compute the integral

Solution
from sympy import *

# First we define our variables
x, y, z = symbols('x y z')

# The integrate function is able to take multiple integrations as parameteres.
# The different integrations are listed according to the integration order: dz -> dx -> dy.
ans = integrate(1, (z, 24*x, 1), (x, -sqrt(1-y**2), sqrt(1-y**2)), (y, -1, 1))

print(ans)
print(float(ans))   # Since the answer (in this case) is a constant, we have to specify if we want it as a float


# Output:
pi
3.141592653589793

Exercise 4: Single indefinite integral

Evaluate

Solution
from sympy import *
 
# First we define our variable
t = Symbol('t')
# We then compute the answer to our given interval
ans = integrate((t**2 - 1)*(4 + 3*t))
# Finally, we print the answer
print(ans)


# Output:
3*t**4/4 + 4*t**3/3 - 3*t**2/2 - 4*t

Remember, when evaluating indefinite integrals, we have to add a general constant to our answer. Python does not include this when integrating, thus we should evaluate the output as the answer:

Exercise 5: Double indefinite integral

Compute the integral

Solution
from sympy import *

x, y = symbols('x y')

# If we want to define our function for later use, or it's simply to big to put directly into the integrate function,
# we can define it first using the pre-defined symbols
f = (x**2 + y**2)**2

ans = integrate(f, x, y)
print(ans)


# Output:
x**5*y/5 + 2*x**3*y**3/9 + x*y**5/5

Exercise 6: Triple indefinite integral

Compute the integral


Solution
from sympy import *

r, omega, t = symbols('r omega t')

f = r*cos(omega + t)
ans = integrate(f, r, omega, t)
print(ans)


# Output:
-r**2*cos(omega + t)/2

Simplify

Click here to read SymPys tutorial on simplification.

Exercise 1

Simplify the expression

Solution
from sympy import *

x = Symbol('x')
ans = simplify(2*x + 15*x - 8*x)
print(ans)


# Output:
9*x

Exercise 2

Reduce the expressionto factors.


Solution
from sympy import *

x, y = symbols('x y')
f = x**2 + 2*x*y + y**2

# In this case, the function "simplify" does not do the trick, as it is a combination of all simplifications,
	# thus not sure which simplification form to choose. To simplify the expression as wanted,
	# we will therefore use the function "factor".
ans = factor(f)
print(ans)


# Output:
(x + y)**2

Documentation about

Exercise 3

Reduce the expression
to factors.

Solution
from sympy import *

x, y = symbols('x y')
f = x**6 + 3*x**4*y**2 + 3*x**2*y**4 + y**6
ans = factor(f)
print(ans)


# Output:
(x**2 + y**2)**3


  • No labels