-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex4.1.py
More file actions
114 lines (102 loc) · 2.53 KB
/
ex4.1.py
File metadata and controls
114 lines (102 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 27 15:11:48 2019
@author: astro
"""
import numpy as np
import math as mt
ecc=float(input('Enter the value of the eccentricity (0.1, 0.7, 0.9):'))
F1= mt.pi #result for pi
F2= mt.pi/3 #result for pi/3
#F=x-ecc*np.sin(x), x=E
tot=1e-6
ival=int(input('Choose the method: relaxation(1), bisection (2), Newton-Rapshon (3):'))
#relaxation method
if(ival==1):
x3=1. #guess
x=1.
xold=10.
while(abs(x3-xold)>tot): #iteration
xold=x3
x3=F1+ecc*np.sin(x3)
print('For F=F1 Converged to E=',x3)
while(abs(x-xold)>tot): #iteration
xold=x
x=F2+ecc*np.sin(x)
print('For F=F2 converged to E=',x)
#bisection method
if(ival==2):
x1=0
x2=1.
xmid=0
xmid1=0
while(5>0):
f1=x1-ecc*np.sin(x1)-F1
f2=x2-ecc*np.sin(x2)-F1
if(f1*f2<0):
break
x2 += 1
print(x1,x2)
while(abs(x2-x1)>tot): #iteration
xmid=0.5*(x2+x1)
fmid=xmid-ecc*np.sin(xmid)-F1
f1=x1-ecc*np.sin(x1)-F1
if(f1<0 and fmid>0):
x1=x1
x2=xmid
else:
x1=xmid
x2=x2
print('For F1, it converges at E=',xmid)
#F=F2
x1=0. #riassegnazione
x2=1.
while(5>0):
f1=x1-ecc*np.sin(x1)-F2
f2=x2-ecc*np.sin(x2)-F2
if(f1*f2<0):
break
x2 += 1
print(x1,x2)
while(abs(x2-x1)>tot): #iteration
xmid1=0.5*(x2+x1)
fmid=xmid1-ecc*np.sin(xmid1)-F2
f1=x1-ecc*np.sin(x1)-F2
if(f1<0 and fmid>0):
x1=x1
x2=xmid1
else:
x1=xmid1
x2=x2
print('For F2, it converges at E=',xmid1)
#1) x-ecc*np.sin(x)-F=0
#2) choose the interval x1, x2
#3) calculate f(x1) and f(x2)
#4) compare them to continue the research
#5) choose when to stop iterate:abs(x2-x1)>1e-6
#Newton-Rapshon method
if(ival==3): #f'(x)= 1 - ecc*cos(x)
x=1.
xnew=0.
while(5>0):
f=x-ecc*np.sin(x)-F1
f1=1-ecc*np.cos(x) #real derivative
deltax=f/f1
xnew=x-deltax
if(abs(xnew-x)<=tot):
break
x=xnew #reassignment
print('For F1, it converges at E=', x)
#F=F2
x=1.
xnew=0.
while(5>0):
f=x-ecc*np.sin(x)-F2
f1=1-ecc*np.cos(x) #real derivative
deltax=f/f1
xnew=x-deltax
if(abs(xnew-x)<=tot):
break
x=xnew #reassignment
print('For F2, it converges at E=', x)