You are on page 1of 48

Computational

Laboratory Work
using Python
Praktikum Komputasi
Minggu I
Semester II – Tahun Ajaran 2021/2022
Tujuan Instruksional Umum
• Menyusun program komputer untuk menghitung kasus-kasus teknik kimia.
Mampu menggunakan metode data processing

• Bahasa pemrograman yang dipakai: Python


Tujuan Instruksional Khusus
Setelah menyelesaikan mata kuliah ini, mahasiswa mampu
1. Mengembangkan program komputer untuk penyelesaian persamaan
matematis (linear dan non-linear), persamaan diferensial ordiner dan parsial
secara numeris.
2. Mengembangkan program komputer untuk optimisasi secara langsung.
3. Menjalankan perangkat lunak komputer untuk perhitungan kasus teknik
kimia.
4. Mengembangkan persamaan empiris dan mengevaluasi nilai parameter
berdasarkan data yang tersedia.
Jadwal kuliah (tentatif) …
Minggu Kelas Laboratorium
ke-
1 Pengenalan python.
2 Pengenalan Python.
3 Pengenalan Python.
Pengantar Tugas 1 diferensiasi numerik.
4 Teori & coding integrasi numeris. Praktikum Tugas 1
Pengantar Tugas 2 integrasi numeric.
5 Teori & coding penyelesaian persamaan non- Praktikum Tugas 2
linier satu variabel.
Pengantar Tugas 3 persamaan non-linier satu
variabel
Jadwal kuliah (tentatif) …
Minggu Kelas Laboratorium
ke-
6 Teori & coding penyelesaian persamaan linier Tugas 3
simultan.
Pengantar Tugas 4 penyelesaian persamaan linier
simultan.
7 Teori & coding penyelesaian optimisasi satu Tugas 4
variable.
Pengantar Tugas 5 golden section.
8–9 Ujian Tengah Semester

10 Teori & coding penyelesaian PD ordiner metode Tugas 5


Runge-Kutta.
Pengantar Tugas 6 PD ordiner RK order 4.
Jadwal kuliah (tentatif) …
Minggu Kelas Laboratorium
ke-
11 Tugas 6
Teori & coding penyelesaian PD ordiner metode
shooting
Pengantar Tugas 7 PD ordiner shooting

12 Teori & coding penyelesaian PD Parsial metode dg Tugas 7


FDA
Pengantar Tugas 8 PDP FDA
13 Teori & coding penyelesaian PD Parsial dg metode Tugas 8
method of lines
Pengantar Tugas 9 PDP MOL
14 Tugas 9
Teori & coding penyelesaian optimisasi
multivariable
Pengantar Tugas 10 Optimisasi multivariabel
Jadwal kuliah (tentatif).
Minggu Kelas Laboratorium
ke-

15 Tugass 10
Teori & coding penyelesaian persamaan non
linier multi variabel
Pengantar Tugas 11 persamaan non linier
multivariabel

16 Penutup Tugas 11

17
Ujian Akhir Semester
Penilaian
• Praktikum pemrograman: 11 tugas
• UTS dan UAS
• Presensi kuliah 75% (ketentuan UGM)
Our python is not a snake, but …
What is Python?

• Python is an interpreted, high-level, general-purpose


programming language.
• Python was conceived in the late 1980s by Guido van
Rossum at Centrum Wiskunde & Informatica (CWI) in
the Netherlands.
• A mature language
• Python 0.9.0 was released in 1991
• Python 2.0 was released in 2000
• Python 3.0 was released in 2008 Guido Van Rossum:
• Author of Python
language
• Dropbox founder
Interpreter vs. compiler …
Interpreter vs. compiler.

Interpreter Compiler

Translate program one statement at a Scans the entire program and translates it
time. as a whole into machine code.

It takes less amount to analyze the source It takes large amount of time to analyze
code but the overall execution time is the source code but the overall execution
slower. time is comparatively faster.

Matlab, Python, JavaScript, … Fortran, C, C++, Visual Basic, …


Why Python?
• Open source and very active community
• High-level language  fast prototyping
• Suited to both small and huge projects (>100k lines of codes or parallel
computing)
• Easy transfer from Matlab to Python
• Support the basic scientific programming (optimization, signal processing,
imaging, linear algebra, storage, databases, etc.)
Why Python?
TIOBE Index for February 2020 (Popularity index)

Feb 2020 Feb 2019 Programming Language Ratings


1 1 Java 17.358%
2 2 C 16.766%
3 3 Python 9.345%
4 4 C++ 6.164%
5 7 C# 5.927%
6 5 Visual Basic .NET 5.862%
7 6 JavaScript 2.060%
8 8 PHP 2.018%
9 9 SQL 1.526%
10 20 Swift 1.460%

16 12 MATLAB 0.794%
Who use Python?
• Large organizations that use Python include CERN (the European Organization
for Nuclear Research), NASA, Wikipedia, Google, Yahoo!, Facebook, Amazon,
Instagram, Spotify.
• The social news networking site Reddit is written entirely in Python.
The Python platform: Spyder on Anaconda
(‘Hello World!’)
Python installation …
• Sebagai perangkat lunak berbasis OPEN SOURCE, tersedia berbagai alternatif
aplikasi untuk menjalankan bahasa pemrograman Python.
• Untuk keperluan komputasi ilmiah, aplikasi yang direkomendasikan adalah
ANACONDA.
• ANACONDA merupakan paket aplikasi lengkap, di mana berbagai library Python
untuk komputasi ilmiah sudah tersedia dalam satu wadah.
Python on Anaconda installation …
• Unduh installer dari Anaconda Distribution (The World's Most Popular Python/R
Data Science Platform)
• Buka website: anaconda.com

Klik bagian Products


Pilih: Individual Edition
Anaconda installation …

• Klik Download
Pilih 64-Bit atau 32-Bit tergantung jenis komputernya. Komputer baru biasanya 64 bit, namun
kalau tidak yakin, pilih saja 32-Bit (Anaconda 32 bit bisa jalan di komputer 32/64 bit namun
Anaconda 64 bit hanya bisa jalan di komputer 64 bit).
Anaconda installation …
• Install Anaconda.
• Selesai instalasi, jalankan Anaconda Navigator
Spyder launching …

Untuk menulis dan menjalankan program Python,


dipakai aplikasi Spyder.
Spyder interface.

Editor:
Tempat menuliskan kode
pemrograman

Console:
Tempat menampilkan output program.
Spyder Editor

• Bagian awal kode yang


dibuat otomatis oleh
Spyder.
• Boleh diubah atau dihapus.

• Bagian di antara dua tanda “”” bisa ditulisi


komentar apapun dengan bebas.
• Biasanya tempat menuliskan keterangan
program yang dibuat dan identitas
pembuatnya.
Our first program.

1. Tuliskan program di atas.


2. Save ke hardisk.
3. Jalankan dengan klik icon ini
Some basic components:
• Mathematical operators & functions
• Variables
• Data format: scalar, list, tuple, array
• Flow controls:
• If
• For
• While
• Print formatting: formatted number, table, etc.
• Data plotting
Introduction to Python
Mathematical operators & functions
Variables
Mathematical operators
Operator Function Example (Spyder Console)
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder (modulus)
// Quotient
** Exponentiation

Remainder : bilangan bulat sisa operasi pembagian


Quotient = pembulatan ke bawah hasil operasi pembagian
Mathematical functions
• Untuk mempergunakan fungsi trigonometri, logaritmik, pembulatan, dll perlu
import dari NumPy.
• NumPy is a library for the Python programming language, adding support for
large, multi-dimensional arrays and matrices, along with a large collection of high-
level mathematical functions to operate on these arrays.
• Contoh: hitung
Mengimpor library NumPy
dengan nama alias np

Tanda # untuk
komentar

np.pi =
np.sin( ) =
Trigonometric functions
Numpy Function Trigonometric function
sin( ) Trigonometric sine
cos( ) Trigonometric cosine
tan( ) Trigonometric tangent
arcsin( ) Inverse trigonometric sine
arccos( ) Inverse trigonometric cosine
arctan( ) Inverse trigonometric tangent
degrees( ) Convert angles from radians to degrees
radians( ) Convert angles from degrees to radians

pi 3.141592653589793
Exponents and logarithms
Numpy Function Exp & log function
exp( ) Calculate the exponential
log( ) Natural logarithm
log10( ) Return the base 10 logarithm
log2( ) Base-2 logarithm

e 2.718281828459045
Miscellaneous functions
Numpy Function Function
sqrt( ) Return the positive square-root
absolute( ) Calculate the absolute value
square( ) Return the square value
round( ) Round an array to the given number of decimals
fix( ) Round to nearest integer towards zero
floor( ) Return the floor of the input (rounded downwards)
ceil( ) Return the ceiling of the input (rounded upwards)
Variables

• Variables are assigned values using the =


operator
• Variables refer to a value stored in memory
and are created when first assigned
• Variable names:
• Must begin with a letter (a - z, A - B) or
underscore _
• Other characters can be letters, numbers or
_
• Are case sensitive: capitalization counts!
• Can be any reasonable length
Variable Data Types
• Available basic types:
• Numbers: Integers and floating point (64-bit)
• Complex numbers: x = complex(3,1) or x = 3+1j
• Strings, using double or single quotes: "cat" 'dog'
• Boolean: True and False
• Lists, dictionaries, and tuples
• These hold collections of variables
• Specialty types: files, network connections, objects
Variable assignment
• Assignment can be done en masse:
x=y=z=1
• Multiple assignments can be done on one
line:
x, y, z = 1, 2.39, 'cat'
Variable modifying operators
Operator Effect Equivalent to…

x += y Add the value of y to x x = x + y


x -= y Subtract the value of y from x x = x - y
x *= y Multiply the value of x by y x = x * y
x /= y Divide the value of x by y x = x / y
Latihan

1. Download dan install anaconda di computer anda.


2. Hitunglah menggunakan Spyder:

a. ingat

Kunci:
a. 0.42261826174069944; b. 0.6745085168424266
c. -0.15051499783199057; d. 1.3671101706955464
Contoh coding 01
• Buatlah program untuk menghitung konversi suhu 40F ke Celcius!
Contoh coding 02
• Seorang mahasiswa pergi ke kampus dengan sepeda motor berkecepatan rata-rata
39 km/j. Jarak dari rumahnya ke kampus 21 km. Jika ia berangkat dari rumah
pukul 06:27, hitunglah pukul berapa ia sampai di kampus. Hitung sampai dua
angka desimal untuk detiknya.
Matplotlib
Plotting in Python
Plotting with matplotlib
• Matplotlib is probably the most popular Python plotting library
• If you are familiar with Matlab plotting then matplotlib is very easy to learn!
• Plots can be made from lists, tuples, numpy arrays, etc.
Contoh coding 03: Simple plot
• Buatlah grafik untuk
Contoh coding 04
• Buatlah grafik dan untuk dalam satu gambar.
Contoh coding 05
• Buatlah grafik dan untuk dalam gambar terpisah.
Matplotlib is a very powerfull plotting tool!
Complete tutorial : https://matplotlib.org/
Referensi

• Gregor, B, “Introduction to Python – Part 1”, Boston University


• Gregor, B, “Introduction to Python – Part 2”, Boston University
• Renaud ,N. & Grozema, F., 2016, “Numerical Techniques for Chemical Engineers
Using Python 2”, TU Delft

You might also like