You are on page 1of 10

Mathematics for Computer Science

Module 1

Prepared by

Dalos D. Miguel

Faculty

School of Accountancy, Management, Computing and Information Studies

Saint Louis University

Baguio City

I. Graphical Visualization
1. The Cartesian Coordinate System

The Cartesian coordinate system or Cartesian plane is formed by a horizontal axis ( the x axis)
and the vertical axis ( the y axis) that are drawn on a plane. The two axes divide the plane
into four quadrants. Recall that the intersection of the x-axis and the y-axis corresponds to the
point with abscissa 0 and ordinate 0 [i.e. (0,0)]. Every point in the lane is corresponding to a pair
of an abscissa and an ordinate. The abscissa is conventionally identified as x coordinate while
the ordinate is conventionally identified as the y coordinate. Any (x,y) pair correspond to a
point in the coordinate system such that a pair of a positive abscissa and a positive ordinate is a
point in the first quadrant, a pair of negative abscissa and a positive ordinate is a point in the
second quadrant, a point with a negative abscissa and a negative ordinate is a point in the
third quadrant and a point with a positive abscissa and a negative ordinate is a point in the
fourth quadrant. The abscissa of a point is a measure of the distance of the point from the verti-
cal axis while the ordinate of a point is the distance of the point from the horizontal axis. The ab-
scissa of a point at the right of the vertical axis is a positive number while the abscissa of a point
at the left of the vertical axis is a negative number. The ordinate of a point above the horizontal
axis is positive while the ordinate of a point below the horizontal axis is negative. One unit of dis-
tance is arbitrary and is set by any user of the Cartesian plane.

2. y = f(x)

The relationship between a value of x and a value of y is conventionally given by the equation
y=f(x) which is read as y is a function of x. Usually, x and y are numbers such that when a value
for x is given, the corresponding value for y is computed by applying f(x).

For example. Suppose y=f(x)=2x+5, the following table shows possible pairs of x and y.

x 1 3 5

y=f(x)=2x+5 7 11 15

The table shows the following cases.


When x=1, y=2(1)+5=7.
When x=3, y=2(3)+5=11.
When x=5, y=2(5)+5=15.

1! | Page
It must be noted that y=f(x) enforces the technical definition of a function in that only one val-
ue of y is paired to a value of x. It is the case that a function is defined from a first set(called
Domain) to second set (called Range) such that only one element from the second set(such
element is called image) is paired to an element of the first set(such element is called pre-im-
age).

The above table implies that the function y=2x+5 is from the set of integers(Z) to the set of inte-
gers(Z). In the absence of a specification, however, it can be generalized that the function
y=2x+5 is a function from the set of Real Numbers(R) to the set of Real Numbers(R).

3. Visualization of a function in a coordinate system using the Python programming language


(matplotlb and pyplot)

3.1. Guide to Using the Python programming Language through colab.research.google.com

NOTES:
a. Through this course, we will use the Python programming language to create pictorial repre-
sentations of functions.

b. Since this is not a course in programming, you need not install an Integrated Development
Environment(IDE) for Python Programming. Instead, you may just use the free online platform
courtesy of colab.research.google.com.

c. colab.research.google.com offers an online IDE that will suffice for beginners in Python pro-
gramming. As an alternative, if you want to work with Python programming while offline, you
need to install Python and its supporting environment. Helpful guides in this regard are surely
available online.

You may follow this procedure in order to use colab.research.google.com


1. Login to gmail.com using your gmail account.
2. Once logged-in, go to colab.research.google.com
3. You will then see an IDE for Python Programming.
4. To write a program, click on +code then type the codes on the provided code window
5. To execute the program, click on the run icon (arrowhead at the left of the code window).
Error messages appear when the code has problems. Otherwise, the output of the program is
shown.
6. Debug your code as needed and redo the process as needed. Take note that indention is
a part of the programming rule in Python. Indentions are means to specify blocks of statements.
Statements under the first block should not be indented. Fortunately, the Python IDE usually pro-
vides automatic indentions where needed.

3.2. Python Code for Plotting/Graphing Given Points

Problem:
Create a program using Python Programming for drawing the curve in a rectangular coordinate
system that passes through the points (1,189), (2,256), (3,280), (4,270), and (5,286).

2! | Page
(Output)

3! | Page
(B) Graph with an assigned color and markers for the points
(Program)
from matplotlib import pyplot as plt
x=[1,2,3,4,5]
y=[189,256,280,270,286]
plt.plot(x,y, color=“red”) # color parameter sets the color of the curve
plt.scatter(x,y,color=“blue”)
# scatter creates a scatter diagram ( shows the points)
plt.title(“Curve Passing through Given Points”);
plt.ylabel(“Values of y”)
plt.xlabel(“Values of x”)

(Output)

4! | Page
Exercise IA

(Philippine Population Curve)


Use Python programming to create a graphical representation for the following population
data for the Philippines. Provide appropriate labels(i.e. let the x-axis be for the years, y-axis be
for the population). Show the program and the output of the program in a file. Filename:
<YourName>Exercise1A (e.g. DelacruzJuanExercise1A)

Year 1960 1970 1975 1980 1990 1995 2000 2007 2010 2015

Popu- 27.09 36.68 42.07 48.09 60.70 68.62 76.51 88.57 92.34 100.98
lation
(In
Mil-
lion)

3.3. Python Code for Plotting/Graphing a given y=f(x)

Suppose you are to graph the function y=2x2-5.


You should determine arbitrary points that are found and in curve for the function. You can then
plot the curve.

(A) Graph of y=2x2-5 using Python

import matplotlib.pyplot as plt #alternative way to import peplos from matplotlib


# define a function that implements y=2x2-5
def function1(x): # This is the syntax for declaring a function in Python
return 2*x*x-5 # Note that the body of the function should be forward indented
# Unlike Java or C, no delimiters are used for the block of codes,
#Instead, forward indention is the way to define the block of statements
# Specify arbitrary values of x in a list
x= [1,2,3,4,5]. # x[0]=1, x[1]=2, x[2]=3, x[3]=4, x[4]=5
#create the list of the values of y corresponding to the values of x
y=[function1(x[0]), function1(x[1]), function1(x[2]), function1(x[3]), function1(x[4])]
plt.plot(x,y)

5! | Page
Another
example
of a code
in Python
for the
graphical
visualiza-
tion of
y=f(x):

# define a
function
that im-
plements
y=2x2-5
def func-
tion1(x):
return 2*x*x-5
# Specify arbitrary values of x in a list
x= [1,2,3,4,5]. # x[0]=1, x[1]=2, x[2]=3, x[3]=4, x[4]=5
#Use a for loop to create the list of the values of y corresponding to the values of x
y=[]
for element in x:
y.append( function1(element))
plt.plot(x,y)
plt.scatter(x,y,color=“green”)

Another example of a code in Python for the graphical visualization of y=f(x):


import matplotlib.pyplot as pls
def functionA(w):
return (w*w + 2*w + 1) # y=x^2+2x+1

6! | Page
!
x=[1,2,3,4,5]
y=[]
for element in x:
y.append(functionA(element))
plt.plot(x,y)
plt.scatter(x,y,color="red")
plt.xlabel(“x”)
plt.ylabel(“y=x^2 + 2x + 1”)

Exercise IB ( Exponential Function Curve )

Write a Python program for showing the graph of the Sigmoid function y = 1/(1+e-x). Show the
program and a snapshot of the program’s output in a file. Filename: <YourName>Exercise1B
(e.g. DelaCruzJuanExercise1B)

7! | Page
Additional Notes:
Some functions are involved in processing data in the context of numerical-based data classifi-
cation and/or machine learning.

One kind of function that is used in data classification is the threshold function.

For example, the threshold function may be

1 If netInput >= 0.5


f(netIput) =
0 if netInput < 0.5

A threshold function is used to determine a class to which a computed value belongs. In the
given example, each of 1 and 0 represents a class. netInput is a valued that is computed out of
some parameters of the application. 1 or 0 is then mapped to netInput. Hence, the function f is
a function from the set of all possible netInputs to the set {0,1}. As the name implies, a threshold
value is involved in the classification such that if the netInput is at least equal to the threshold,
the corresponding classification is 1. If the netInput is below the threshold, the corresponding
classification is 0.

The threshold function is used when the classes are represented by discrete values (e.g. 0 or 1).
If the classifications are not discrete, say, a class is a certain real number, a continuous classifica-
tion function is applied. An example of a continuous classification function is the function f(net-
Input) = e-netInput (i.e. f(x) = e-x).

Application.
In a lending company, a borrower is classified as a good borrower or a bad bor-
rower. A good borrower is one who pays a loan on time and a bad borrower is
one who is delinquent in paying a loan. An machine learning-based analysis of
the data about the borrowers during a period revealed that there are 3 factors
that determine the classification of a borrower. The 3 factors are gender,
monthly income bracket and other credit membership or subscription. The 3
factors are dichotomously denoted numerically as follows.

8! | Page
X1 represents gender where X1=1 means the borrower is male and X2=0 means
the borrower is female.

X2 represents monthly income bracket where X2=1 means the borrower has a
monthly income that is greater or equal to PhP20,000.00 and X2=0 means the
borrower has a monthly income that is below 20,000.00.

X3 represents other credit membership where X3=1 means the borrower has oth-
er existing credit membership and X3=0 means the borrower has no existing
credit card membership.

Furthermore, the classification of the borrower is represented as 1 or 0 where 1 is


to good borrower and 0 is to bad borrower.

The machine learning-based analysis of the data led to determining a weight


corresponding to each of the factors that affect the payment behaviour of a
borrower. The weight of X1 may be denoted as w1, the weight of X2 may be de-
noted as w2, and the weight of X3 may be denoted as w3.

Essentially, the classification of the borrower is given by a threshold function. The


threshold function is summarised as follows.

1 if (sum of Xi * wi) >= 0.5


Borrower classification = f =
0 if (sum of Xi * wi) < 0.5

Suppose the machine learning-based analysis of the data on borrowing resulted


to the case that the X1 has a weight of 0.10, X2 has a weight of 0.50 and X3 has a
weight of 0.10. It can be forecasted that a male who has a monthly income of
PhP25,000.00 who does not have an existing credit membership can be a good
borrower.

I.e.
1(0.10)+ 1(0.5) + 0(0.1) = 0.6
Since, 0.6 >= 0.5, the classification is 1=good borrower.

9! | Page
References:

Banks, J., et al. (1996). Discrete Event System Simulation. 2 Edition. Prentice Hall, Inc. New Jersey. U.S.A.
nd
Gosset, E. (2003). Discrete Mathematics with Proof. Pearson Education Inc. USA.

Hillier, F. and M. Hillier. (2011). Introduction to Management Science A Modeling and Case Studies Approach with
Spreadsheets. 4 Edition. McGraw-Hill Education. Singapore.
th

Freeman, J. And D. Skapura.(1991). Neural Networks Algorithms, Applications, and Programming Techniques.
Addison-Wesley Publishing Company. New York.

Janacek,G and M. Lemmon. (2011). Mathematics for Computer Science. Gareth J. Janacek, Mark Lemmon Close &
Ventus Publishing

Love, C. & E. Rainville. (1981). Differential and Integral Calculus. . Macmillan Publishing Co., Inc. USA.

Rainville, E. & P. Bedient. (1974). Elementary Differential equations. Macmillan Publishing Co., Inc. USA.

Rosen, K. (2001). Discrete Mathematics and Its Applications. Mc-Graw Hill Book Co. Singapore.

Walpole, R.(1997). Introduction to Statistics. 3 Edition. Prentice Hall International, Inc. Singapore.
rd

! | Page
10

You might also like