Professional Documents
Culture Documents
Laborator 5
Laborator 5
November 4, 2019
1 Laboratorul 5 - Aplicatii
In cursul acestui laborator vom implementa diverse probleme specifice (de genul celor pe care le
veti primi la partial). Incercati rezolvarea problemelor individual, in laborator si cereti ajutorul
acolo unde intampinati dificultati. Scopul acestui laborator este sa va familiarizeze cu subiectele.
1.1 Problema 1 - Identificarea limitei unui sir definit printr-o relatie recurenta
Fie sirul definit prin relatia de recurenta $ a_n = atan(-3*a_{n-1}+10)$. Scrieti o secventa de cod
care sa preia de la tastatura termenul $ a_0 $ si verificati daca numarul real este cuprins intre 0 si
π/2. Estimati limita acestui sir folosind pentru testul de atingere a limitei expresia
unde
ϵ = 10−7
. Afisati la consola limita estimata si numarul iteratiei - n - atins in acel moment.
[1]: from math import pi, atan
while True:
try:
a0 = float(input('a0:'))
if a0 > 0 and a0<pi/2:
break
print('Invalid input, a0 must be greater than 0 and smaller than pi/2')
except:
print('Invalid input, a0 must be a real number')
def a(n):
if n == 0:
return a0
return atan(-3*a(n-1)+10)
n = 1
while abs(a(n)-a(n-1)) > 1E-7:
n += 1
print('limita: %.4f - %.4f = %.4f, iteratia %i' % (a(n),a(n-1),␣
,→abs(a(n)-a(n-1)),n))
1
a0:1.1
limita: 1.4001 - 1.4001 = 0.0000, iteratia 8
∞
∑ −1
e=3+
k!(k − 1)k
k=2
creati un script care sa permita estimarea numarului e folosind aceasta formula si care sa foloseasca
testul de atingere a limitei folosind expresia
|math.e − e| < ϵ
, unde
ϵ = 10−6
.
[2]: from math import factorial as fact
from math import e
eps = 1E-6
n = 3
def fn(n):
return 3+sum([(-1)/(fact(k)*(k-1)*k) for k in range(2,n)])
while abs(e-fn(n)) > eps:
n += 1
print('estimated: %.6f, exact: %.6f, diff: %.6f, iter: %i' % (fn(n),e,␣
,→abs(fn(n)-e),n))
Folosind formula
∞
∑
√ (−3)−k
π= 12
2k + 1
k=0
creati un script care sa permita estimarea numarului π folosind formula de mai sus si care sa
foloseasca testul de atingere a limitei
2
[3]: from math import sqrt, pi
eps = 1E-6
fn = lambda n: sqrt(12)*sum([((-3)**(-k))/(2*k+1) for k in range(n)])
n = 1
while abs(fn(n)-pi) > eps:
n += 1
print('estimated: %.6f, absolute: %.6f, iteration: %i' % (fn(n),pi,n))
Scrieti un script care sa preia de la tastatura un numar real $ m > 0 $ si un numar intreg n.
Verificati daca m este cuprins in intervalul (0.7, 3.5) si $ n >= 4 $. Calculati expresia:
log8 m m−1
n
(n − 2)!
m:.8
n:8
Value: -0.000098330247
Scrieti un script care sa citeasca de la tastatura un numar real e care trebuie sa fie cuprins in
intervalul (0, 10−2 ). Estimati radacinile functiei
f (x) = atan(x) + x − 1
in intervalul
x ∈ [0, 5]
folosind testul de apropiere de radacina conditia
|f (x)| < e
3
. Afisati la consola numarul de pasi necesari pentru a atinge aceste aproximatii.
[5]: from math import atan
while True:
try:
e = float(input('e:'))
if e>0 and e<1E-2:
break
print('e must be in (0, 1E-2)')
except:
print('Invalid input, e must be a real number')
f = lambda x: atan(x)+x-1
def getint(a, b):
hi = (a+b)/2
if f(a)*f(hi) < 0:
return a,hi
return hi,b
a, b = 0, 5
while abs(a - b) > 2*e:
a, b = getint(a,b)
print('root: %.8f' % ((a+b)/2))
e:1e-8
root: 0.52026899
Scriteti un script care sa citeasca un fisier text ce contine 2 coloane despartite cu spatiu si contin
cate un numar real in fiecare coloana. Pentru fiecare linie din fisierul respectiv se va afisa numarul
complex z cu $ Re(z) = a si Im(z)=b $. Stiind ca numarul $ c = 1-1j$ verificati conditia ca $ | c-z
| <= 3 $.
[7]: try:
c = 1-1j
with open('data.dat','r') as f:
for line in f.readlines():
ls = line.split(' ')
if len(ls) == 2:
z = complex(float(ls[0]), float(ls[1]))
if abs(c-z) <= 3:
4
print('%s satisfies the condition' % z)
else:
print('%s' % z)
except Exception as e:
print('Could not open the data.dat file.')
(1.68894406+2.86469603j)
(1.79435227+1.1180126j) satisfies the condition
(1.57827647+2.65820806j)
(2.01802231+1.94754524j)
(2.87839653+1.74405228j)
(1.20499415+1.00001243j) satisfies the condition
(1.84407868+1.12210581j) satisfies the condition
(2.22964982+1.36365545j) satisfies the condition
(1.57112443+1.56063128j) satisfies the condition
(2.02975616+1.00801764j) satisfies the condition
(2.2514934+2.25882377j)
(1.33097559+2.49187994j)
(2.85257105+2.03324967j)
(2.72330796+2.30850687j)
(1.86870852+1.7789032j) satisfies the condition
(2.83914139+1.46711845j)
(2.33740599+2.38918385j)
(1.96314464+2.05129212j)
(2.02618414+2.89816498j)
(1.08962459+1.69525178j) satisfies the condition
(2.73196233+1.12786513j) satisfies the condition
(2.2492072+2.60928606j)
(1.93689884+2.09413295j)
(1.93745332+2.722481j)
(2.6544594+2.92477522j)
(1.25305535+1.36072336j) satisfies the condition
(1.2363914+2.91209376j)
(2.12044953+1.71227458j) satisfies the condition
(2.10513615+1.94913936j)
(2.08951604+2.67240527j)
(1.29197228+2.34238578j)
(1.45502192+2.58456396j)
(1.23338466+2.5710435j)
(1.47319139+1.42692499j) satisfies the condition
(1.10751905+2.11617428j)
(2.90266183+1.02481621j) satisfies the condition
(2.78335611+1.00824207j) satisfies the condition
(1.65513236+1.39761331j) satisfies the condition
(2.45692245+2.31332583j)
(1.74928829+2.62232173j)
(2.60208232+2.29139089j)
5
(1.79745596+1.27276836j) satisfies the condition
(1.48264232+2.24322447j)
(1.19457053+1.18079227j) satisfies the condition
(2.70595418+2.58074157j)
(2.741288+2.38600935j)
(2.13089456+1.14230258j) satisfies the condition
(2.31408486+2.38555801j)
(2.06282541+1.73008921j) satisfies the condition
(2.18810675+2.05793264j)
(2.08556843+1.91089428j)
(1.39930976+2.62891539j)
(2.39480913+1.84068854j)
(1.68380101+1.39665948j) satisfies the condition
(2.40983909+1.4224625j) satisfies the condition
(1.86098523+2.36526403j)
(2.88565133+2.04418864j)
(2.57226065+1.99815406j)
(1.70324931+1.60379432j) satisfies the condition
(1.72718947+1.66589506j) satisfies the condition
(1.16313746+1.26914693j) satisfies the condition
(2.96132623+1.18105744j) satisfies the condition
(2.71233844+2.00407855j)
(2.84072862+2.76693724j)
(1.82154495+2.63451017j)
(1.55267817+2.79002265j)
(1.23030324+1.07655873j) satisfies the condition
(2.25793851+1.46524232j) satisfies the condition
(1.97938957+2.99182152j)
(1.18788544+2.42168071j)
(1.92634165+2.79530462j)
(2.578442+1.08007965j) satisfies the condition
(1.47800191+2.60199032j)
(1.87539027+2.24048838j)
(2.47130749+1.1373235j) satisfies the condition
(2.36842357+2.2721732j)
(2.91077677+2.8556606j)
(2.78335511+1.30696098j) satisfies the condition
(2.18290723+1.05437421j) satisfies the condition
(1.47051683+1.26709779j) satisfies the condition
(1.09159822+1.86175907j) satisfies the condition
(2.41707812+2.03130433j)
(2.3880248+2.93155117j)
(1.27793446+1.81268305j) satisfies the condition
(1.15352276+2.54399271j)
(1.11994311+1.99620547j) satisfies the condition
(2.73618661+2.71735018j)
(2.56862223+2.00522324j)
(1.89281742+1.40257201j) satisfies the condition
6
(2.13614774+2.33795391j)
(2.36080444+1.12822154j) satisfies the condition
(2.25543764+2.33580663j)
(2.76385145+1.76506017j)
(2.77790324+1.88508542j)
(2.0687127+1.66496936j) satisfies the condition
(1.81902616+2.44585259j)
(2.52700905+1.00011796j) satisfies the condition
(2.52019847+1.68498108j)
(2.1382298+2.21742249j)
(2.22917183+2.36252136j)