0% found this document useful (0 votes)
2 views4 pages

Introduction To Python

This document provides an introduction to using Python for numerical analysis, specifically through Jupyter notebooks. It covers basic operations, the use of loops, and how to work with functions and plotting using libraries like numpy and matplotlib. Exercises are included to reinforce the concepts learned, emphasizing the importance of understanding the code rather than memorizing it.

Uploaded by

arrogantaldo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Introduction To Python

This document provides an introduction to using Python for numerical analysis, specifically through Jupyter notebooks. It covers basic operations, the use of loops, and how to work with functions and plotting using libraries like numpy and matplotlib. Exercises are included to reinforce the concepts learned, emphasizing the importance of understanding the code rather than memorizing it.

Uploaded by

arrogantaldo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

University of Pretoria

Dept of Mathematics and Applied Mathematics


WTW 123 - Numerical Analysis

Introduction to Python

We use Jupyter notebook for programming in Python. To open a new notebook, go to Anaconda
Navigator (if you don’t have it yet, please download it). After it has launched, look for “Jupyter
notebook” and click on the “Launch” button. On the next screen you will see buttons for
“Upload” and “New” on the top right. Click on “New” and then “Python 3” on the dropdown
list that appears.
You will now have an open workbook and see “In [ ]:”. You can type your commands here.
You can type more than one command at a time (on separate lines) and then click on “Run”
or use the keyboard shortcut shift-enter to execute the commands.

Known functions in Python:

The following basic operations are built into Python:

Normal notation Python notation


a+b a+b
a−b a−b
ab a∗b
ab a∗∗ b
a
b a/b

For the next operations you need to import the math module first, type: import math and run
the command. You only have to do this once in a workbook. The following can now be used:

Normal notation Python notation



a [Link](a)
sin a [Link](a)
cos b [Link](b)
tan a [Link](a)
ex [Link](x)
ln x [Link](x)
π [Link]

Exercise: √
Play around with using Python as a calculator. For example, find the values of 2 + 3 6 and
35 − cos(10). Now assign these to variables a and b (using a=... and b=...) and add the two.
You can use print(a) for example if you want to see the value stored in the variable a.
Note that you don’t have to re-type the expressions. You can just add things to cells that you
already ran.

Remark: The above functions are used when it is applied to a single number a. When these
operations need to be applied to a list (or vector) of numbers, you have to use numpy. instead
of math. . (See “Important notes” in section 1.2 of this document.)

1
1.1 The ‘for’ loop
To learn how a ‘for’ loop works, enter the following lines and run the code.
sum = 0
k = 41
sum = sum + k**2
k = 42
sum = sum + k**2
k = 43
sum = sum + k**2
print(sum)
43
X
The code above calculates k 2 = 412 + 422 + 432 . The following is a much better way of
k=41
calculating it, without so much repetition of commands:
sum = 0
for k in [41,42,43]:
sum = sum + k**2
print(sum)
The following will do the exact same thing:
sum = 0
for k in range(41,44):
sum = sum + k**2
print(sum)
Important notes:

ˆ for tells Python that you are going to use a loop (i.e. something that will happen more
than once). The notation for this is to start with the word for, then give a variable
that will change every time the loop is executed (in this case k), then the values that the
variable should take on, and then finally the command is ended with a :.

ˆ The indented commands after the : are then the things inside the loop and in this case
executed 3 times, once for each integer value of k between 41 and 43.

ˆ Note that range can be used instead of manually typing all the options (if we want a loop
to run many times). The function range needs 2 input values, the first is the number
where it should start counting and then the last is the number AFTER the last number
you want, so in this case it is 44 (43 + 1).

ˆ In Python the indentation indicates the end of the for loop. Therefore the print(sum)
command is only executed once, after the loop is done.

Exercise
10
X
Use Python to calculate i3 . You should get 3025. Ask your tutor/lecturer if you are not
i=1
able to get this answer.

1.2 x– and y–values


Consider the function f (x) = x3 − 3x2 + 3. We want to work out the function values between
certain x-values and print the values as a table (this will be used for interval division).
In order to do this in Python we need to import another package, type: import numpy and
run the command. This allows us to set up a range that does not just contain integer values.
Type the following:

2
x=[Link](−1,3.1,0.1)
y= x**3 − 3*x**2 + 3
for xp, yp in zip(x,y):
xprint=format(xp,'.2f')
yprint=format(yp,'.2f')
print(xprint, '\t', yprint)
Important notes:
ˆ To construct the x-values we use [Link](−1,3.1,0.1). This sets up values
from −1 to 3 with stepsize 0.1. Note that we use 3.1 instead of 3 for the endpoint, since
Python does not use the last value (look at how range worked again).
ˆ The for loop prints the values of x and y as a vertical list. The format commands are
used since we don’t want to display too many decimals in our list. The 't' is just used
to add some space between the x and y values in the printed list.
ˆ When you assign values to y using the whole list of values in x as above, you cannot use
the commands [Link](x) etc as in the table. You have to use [Link](x) etc
in order to apply the operation to all the values in the variable x.
ˆ This is not the only way to set up the vectors x and y, nor the only way to print a table.
You may use other commands, or the tabulate environment:

from tabulate import tabulate


x=[Link](−1,3.1,0.1)
y= x**3 − 3*x**2 + 3
Data=[Link]((x,y))
print(tabulate(Data,headers=('x','f(x)')))

Can you see that x is a vector that contain all the endpoints in the subinterval? What is the
meaning of y?

1.3 Plotting the graph


The aim is to get a graph of the function f with f (x) = x3 − 3x2 + 3 on the interval [−1, 3],
(see Example 1, Section 1.2). The program above already calculated the function values (f ) for
the different x-values.
In order to plot, we have to import another package. Type the following and run it before
attempting the program below: import [Link] as plt. Note that “plt” is just
a short name for the package so that we don’t have to type out the whole name every time we
use it.
x=[Link](−1,3.1,0.1)
y= x**3 − 3*x**2 + 3
[Link](x,y)
Important notes
ˆ It was not necessary to run the first 2 lines again. Python still remembers what x and y
are from the previous commands.
ˆ [Link](x,y) plots the points (x, y) against each other and connect these points with
straight lines. This gives us an idea of the graph. Although this graph appears smooth
on the screen, it is not the case. If we plot vectors (of the same length) with few entries
against each other, we get graphs that do not appear smooth, but have a polygonal form.
Make sure to use enough points so that you can properly see the shape of the graph, i.e.
polynomials should at least look smooth.

3
ˆ You may want to use [Link]() to insert a grid on your graph - it makes it easier to
see where the graph cuts the axis.

Final remarks

ˆ When you get an error and you want to fix your mistake, it is not necessary to retype (or
copy) everything. You can just fix the mistake where it is and run the code again.

ˆ You will not be expected to know all the code/commands on this document, but rather
understand it. In the class tests incomplete programs or programs with errors will be
given, so you should be able to complete it and/or fix it.

ˆ The code in this document is not the only (or necessarily the best) way of programming
these problems. If you have another way of doing it you can use that, as long as you don’t
use other packages and you still understand the code given on this document.

You might also like