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. μ»΄ν¨ν°μ κ°λ λ° μ€μ΅
© Copyright 2024