Добавил:
Lobov4ever
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
import math
import matplotlib.pyplot as plt
import numpy as np
x_list = []
y_list = []
def f(x):
return x*math.tan(x)-(1/3)
def ff(x):
return math.tan(x)+(x/(math.cos(x)**2))
def fff(x):
return ((2*x*math.sin(x))+(2*math.cos(x)))/(math.cos(x)**3)
def fi(x):
return 1/(3*math.tan(x))
def delenie_e(a,b,E):
x=(a+b)/2
n=0
while abs(a-b)>E:
if n<5:
x_list.append(x)
y_list.append(f(x))
if f(a)*f(x)<0:
b=x
x=(b+a)/2
else:
a=x
x=(a+b)/2
n+=1
return x, n
def delenie_n(a,b,N):
x=(a+b)/2
for i in range(N+1):
if (i<5):
x_list.append(x)
y_list.append(f(x))
if f(a)*f(x)<0:
b=x
x=(b+a)/2
else:
a=x
x=(a+b)/2
e=abs(a-b)
return x, e
def newton_e(a,b,E):
if f(a)*fff(a)>0:
x=a
else:
x=b
if ff(x)!=0:
x1=x-f(x)/ff(x)
else:
return x1, 1
n=1
while abs(x1-x)>E and ff(x)!=0:
if n<5:
x_list.append(x)
y_list.append(f(x))
x=x1
x1=x-f(x)/ff(x)
n+=1
return x1, n
def newton_n(a,b,N):
if f(a)*fff(a)>0:
x=a
else:
x=b
if ff(x)!=0:
x1=x-f(x)/ff(x)
else:
return x1, 1
for i in range (N):
if (i<5):
x_list.append(x)
y_list.append(f(x))
if ff(x)!=0:
x=x1
x1=x-f(x)/ff(x)
e=abs(x-x1)
return x1, e
def pribl_e(x0,E):
x=x0
x1=fi(x)
n=1
while abs(x1-x)>E:
if n<5:
x_list.append(x)
y_list.append(f(x))
x=x1
x1=fi(x)
n+=1
return x1, n
def pribl_n(x0,N):
x=x0
x1=fi(x)
for i in range(N):
if (i<5):
x_list.append(x)
y_list.append(f(x))
x=x1
x1=fi(x)
e=abs(x1-x)
return x1, e
vibor=int(input('Выберите вариант вычисления и введите его номер:\n1. Метод деления отрезка пополам с заданным E\n2. Метод деления отрезка пополам с заданным N\n3. Метод Ньютона с заданным E\n4. Метод Ньютона с заданным N\n5. Метод последовательных приближений с заданным E\n6. Метод последовательных приближений с заданным N\nВведите ваш выбор: '))
if vibor==1:
a=float(input('Введите a: '))
b=float(input('Введите b: '))
E=float(input('Введите Эпсилон: '))
x,n=delenie_e(a,b,E)
print('x=', x, '; Число итераций - ', n )
if vibor==2:
a=float(input('Введите a: '))
b=float(input('Введите b: '))
N=int(input('Введите число итераций: '))
x,e=delenie_n(a,b,N)
print('x=', x, '; Точность - ', e )
if vibor==3:
a=float(input('Введите a: '))
b=float(input('Введите b: '))
E=float(input('Введите Эпсилон: '))
x,n=newton_e(a,b,E)
print('x=', x, '; Число итераций - ', n )
if vibor==4:
a=float(input('Введите a: '))
b=float(input('Введите b: '))
N=int(input('Введите число итераций: '))
x,e=newton_n(a,b,N)
print('x=', x, '; Точность - ', e )
if vibor==5:
x0=float(input('Введите x0: '))
E=float(input('Введите Эпсилон: '))
x,n=pribl_e(x0,E)
print('x=', x, '; Число итераций - ', n )
if vibor==6:
x0=float(input('Введите x0: '))
N=int(input('Введите число итераций: '))
x,e=pribl_n(x0,N)
print('x=', x, '; Точность - ', e )
if vibor<5:
x = np.arange(a, b, 0.01)
plt.plot(x, x*np.tan(x)-(1/3))
plt.plot(x_list, y_list)
plt.grid(True)
plt.show()
else:
x = np.arange(x0-0.5, x0+0.5, 0.01)
plt.plot(x, x * np.tan(x) - (1 / 3))
plt.plot(x_list, y_list)
plt.grid(True)
plt.show()
#По условию нужно ввести a=0.2 , b=1 , e=0.0001 , n = 16 :)
Соседние файлы в папке лаб 7
