Computing With Numbers Objects and Graphics

Computing With Numbers
Objects and Graphics
Computer Concepts and Practice
Intelligent Data Systems Lab.
Seoul National University
Objectives
To understand Newton-Raphson Method for your project
To solve 4 + 𝜢 Practice Questions
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
κ³Όμ • μ˜ˆμ‹œ
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
3
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
4
κ³Όμ • μ˜ˆμ‹œ
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
5
κ³Όμ • μ˜ˆμ‹œ
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
6
κ³Όμ • μ˜ˆμ‹œ
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
7
κ³Όμ • μ˜ˆμ‹œ
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
8
κ³Όμ • μ˜ˆμ‹œ
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
9
κ³Όμ • μ˜ˆμ‹œ
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
10
Notes
Assume that the input function is always first order
differentiable
Function is given as a lambda expression
You don’t need to know anything about it yet
Just assume that you can get f(x) by using it like a function call.
x = 3.3
func = eval(input("Please input a function f(x):"))
#f(3.3)λ₯Ό κ³„μ‚°ν•œ 값이 ν”„λ¦°νŠΈλœλ‹€.
print("f(3.3) is", func(x))
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
Implmentation Hints
You can APPROXIMATE f’(x) by using the formula
As long as this error is less than the tolerated error margin,
above approximation can guarantees a working algorithm
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
Graphics.py
Download graphics.py from http://ids.snu.ac.kr
Copy it to C:\Python34\Lib\site-packages
Or put it in the same directory as the source files
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
Practice 1
Let’s write a program that can find the solutions to a quadratic equation:
π’‚π’™πŸ + 𝒃𝒙 + 𝒄 = 𝟎.
Equation has two solutions for the value of 𝒙 given by the quadratic formula:
βˆ’π‘ ± 𝑏 2 βˆ’ 4π‘Žπ‘
π‘₯=
2π‘Ž
Program Execution)
This program finds the real solutions to a quadratic
Please enter the coefficients (a, b, c): 3, 4, -2
The solutions are 0.39 and -1.72
* Cautions
1.
This program makes use of the square root function sqrt from the math
library. Add import math at the top of the program.
2.
Round to second decimal place by using function round
1. round(3.141592, 2)  3.14
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
Practice 2
Let’s write a program that will compute the factorial of a number entered by
the user.
In mathematics, factorial is denoted with an exclamation (!).
And the factorial of a whole number 𝒏 is defined as 𝒏! = 𝒏 𝒏 βˆ’ 𝟏 𝒏 βˆ’ 𝟐 … 𝟏
For example, we compute πŸ”! = πŸ” πŸ” βˆ’ 𝟏 πŸ” βˆ’ 𝟐 πŸ” βˆ’ πŸ‘ πŸ” βˆ’ πŸ’ πŸ” βˆ’ πŸ“ = πŸ•πŸπŸŽ
Program Execution)
Please enter a whole number: 6
The factorial of 6 is 720
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
Practice 3
Let’s write a program that determines the molecular weight of a hydrocarbon
based on the number of carbon(C), hydrogen(H), and oxygen(O) atoms.
Here are the weights of atoms.
Atom
Weight
(grams / mole)
carbon (C)
12.011
hydrogen (H)
1.0079
oxygen(O)
15.9994
Program Execution)
Please enter the number of each atom
carbon: 2
hydrogen: 6
oxygen: 1
The molecular weight of C 2 H 6 O 1 is 46.07
* Cautions
1.
Round to second decimal place by using function round
1. round(46.0688, 2)  46.07
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅
Practice 4
β€’ κ·Έλž˜ν”½μŠ€ 창의 벽에 λΆ€λ”ͺ히면 λ°˜μ‚¬λ˜λŠ”
곡을 κ΅¬ν˜„ν•˜μ‹œμ˜€
β€’ μˆ˜μ—… μ‹œκ°„μ— 배운 .move(x, y)λ₯Ό ν™œμš©
β€’ μ™Όμͺ½κ³Ό 같은 물리 법칙을 λ§Œμ‘±ν•΄μ•Όν•¨
β€’ μž…λ ₯ κ°’: 초기 속도, 초기 각도, 곡의 반경
β€’ μ’Œν‘œ κ³„λŠ” (0,0)λΆ€ν„° (10, 10)κΉŒμ§€
β€’ μž…λ ₯ 예:
Enter initial speed of the ball: 10
Enter initial angle of the ball: 250
Enter radius of the ball: 1.1
IDS Lab.
μ»΄ν“¨ν„°μ˜ κ°œλ… 및 μ‹€μŠ΅