0% found this document useful (0 votes)
2 views

Lab04.guide

The document is a lab guide for Applied Calculus for IT at Ton Duc Thang University, focusing on the use of the SymPy library for symbolic mathematics in Python. It includes setup instructions, practice examples, and exercises related to plotting functions, evaluating expressions, and calculating limits. Additionally, it provides references for further learning about Python programming.

Uploaded by

dinhhien150706
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab04.guide

The document is a lab guide for Applied Calculus for IT at Ton Duc Thang University, focusing on the use of the SymPy library for symbolic mathematics in Python. It includes setup instructions, practice examples, and exercises related to plotting functions, evaluating expressions, and calculating limits. Additionally, it provides references for further learning about Python programming.

Uploaded by

dinhhien150706
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ton Duc Thang University

Faculty of Information Technology

Applied Calculus for IT - 501031


Lab 04

1. Sympy
SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer
algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible
and easily extensible.
1.1 Setup:
If you have Python and PIP already installed on a system, then install it using this command:
python -m pip install sympy
1.2 Introduction
Symbolic computation systems such as SymPy are capable of computing symbolic expressions
with variables. Let us define a symbolic expression, representing the mathematical
expression x+2y.

Note that we wrote just as we would if x and y were ordinary Python variables. But in
this case, instead of evaluating to something, the expression remains as just . Now let us
play around with it:

https://docs.sympy.org/latest/tutorials/intro-tutorial/intro.html
1.3 Practice examples:
Solve .
import sympy as sp

Trinh Hung Cuong - Applied Calculus for IT - 501031 1/5


Ton Duc Thang University
Faculty of Information Technology
x = sp.symbols('x')
root = sp.solve(x**2 + 4*x + 4)
print(root)

Plot functions:
import sympy as sp
import math

x = sp.symbols('x')

f5a = abs(x)**(1/2)
sp.plot(f5a, (x, -10, 10), line_color='red') #tuple

f5d = math.e**x
sp.plot(f5d, (x, -10, 10), line_color='blue')

f5e = sp.log(x)
sp.plot(f5e, (x, -10, 10))

Evaluate a certain expression:


import sympy as sp

x = sp.symbols('x')
f = x*x - x + 1

print( f.subs(x, 2) ) # f(2)

Exercise 0
Write a Python program to plot the following functions on a graph, and mark the intersection point of
f1 and f2:

Hint:
import sympy as sp
import matplotlib.pyplot as plt
import numpy as np

# ve f1 va f2
Trinh Hung Cuong - Applied Calculus for IT - 501031 2/5
Ton Duc Thang University
Faculty of Information Technology
x = np.arange(...)
f1 = lambda x: ...
f2 = lambda x: ...

y1 = ...
y2 = ...

plt.plot(x, y1)
plt.plot(x, y2)

# ve diem giao nhau


x = sp.symbols('x')
f1 = -x + 5
f2 = ...

x_root = sp.solve(...)
y_root = f1.subs(...) #f1(x_root[0]) = f1(2)

plt.plot(..., y_root, 'ro')


plt.title('Find intersection point of f1(x) = -x + 5 and f2(x) = x/2 + 2')
plt.grid(linestyle='--')
plt.show()

Exercises 1, 3, 4, 5, 6, 7, 10 in the PDF file “Lab04.ex.pdf”


Hint:
Limit of a function:
import sympy as sp
import math
x = sp.symbols('x')
#1c
f1c = math.e ** (1/x)
lm = sp.limit(f1c, x, 1)
print("1c - The limit of f(x) at x = 1: " + str(lm) )
#1i
f = (2*x*x) / ( 3 - 3*sp.cos(x) )
lm = sp.limit(f, x, 0)
print("1i - The limit of f(x) at x = 0: " + str(lm) )

#infinity: sp.oo

#factorial: sp.factorial(...)

Trinh Hung Cuong - Applied Calculus for IT - 501031 3/5


Ton Duc Thang University
Faculty of Information Technology
Right/Left limit:

import sympy as sp
import math

#3.1
x = sp.symbols('x')
f3_1 = 1/( 1 + 2**(1/x) )
lmRight = sp.limit(f3_1, x, 0, '+')
print ("Right limit = " , lmRight )
lmLeft = sp.limit(f3_1, x, 0, '-')
print ("Left limit = " , lmLeft )

Continuity

Trinh Hung Cuong - Applied Calculus for IT - 501031 4/5


Ton Duc Thang University
Faculty of Information Technology
Ex6:
import numpy as np
import sympy as sp

x = sp.symbols('x')
f6a = ...

#At point x = 0
lm_x_0 = sp.limit(f6a, x, 0)
#Compare lm_x_0 and f(0)
...

#Other points x != 0
for c in np.arange(-100, 100, 1):
if c != 0:
lm_x_c = sp.limit(f6a, x, c)
#Compare lm_x_c and f(c)

#f(x) is continuous for all x # 0, 3

Homework:
Exercises 8 in the PDF file “Lab04.ex.pdf”

2. References
- Python Tutorial on the W3schools website: https://www.w3schools.com/python/default.asp
- Python Tutorial on the Tutorials Point website:
https://www.tutorialspoint.com/python/index.htm

-- THE END --

Trinh Hung Cuong - Applied Calculus for IT - 501031 5/5

You might also like