-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConics with matplotlib.py
275 lines (242 loc) · 11.2 KB
/
Conics with matplotlib.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# Importing all the required Libraries
#Importing matplotlib
import matplotlib.pyplot as mpl
#Impoting numpy
import numpy as np
#Defining values for x and y
x = np.linspace(-50, 51, 200)
y = np.linspace(-50, 51, 200)
#meshgrid makes an array, this is useful in defining functions
x, y = np.meshgrid(x, y)
#Axes() as a function, this will highlight our origin
def axes():
mpl.grid()
mpl.axhline(0, alpha= .2, linewidth= 2, color='k' )
#Printing Horizontal line, X axis
mpl.axvline(0, alpha= .2, linewidth= 2, color='k' )
#Printing Vertical line, Y axis
#mpl.axis('equal')
#For taking valid inputs as values, defining a f(n)
def options(prompt):
while True:
try:
#Ensuring the input to be integer
number = int(input(prompt))
break
except ValueError :
#Printing Output for correction, Giving user another chance to enter value
print('Please enter a Valid input ')
pass
return number
def delta(a,b,c,f,g,h):
if a*(b)*c +2*f*g*h -a*f^2 -b*g^2 -c*h^2 != 0:
return True
else :
return False
while True :
print('Conic section Graphs')
print('Menu \n 1. Parabola \n 2. Ellipse and circles \n 3. Hyperbolas \n 4.EXIT')
opts = options(' Please enter any one of the options ')
if opts == 1:
while True:
print('PARABOLA')
print("MENU \n 1. Parabola in standard equation \n 2. Parabola in Expanded equation \n 3.Return to Menu")
ch = int(input('Choose an option'))
if ch == 1:
print('1. Parabola in standard equation')
a = float(input('Please enter the value of \'a\' ' ))
parabola = (y**2 - 4*a*x)
print(' Parabolas in standard eqn are of the form \n y^2 = 4ax ')
print('Do you Want to see directrix and focus as well ? ')
duff = input (' y for YES and n for NO ')
if duff == 'y':
axes()
# Printing Focus as a DOT.
mpl.plot(a, 0, '.')
# Printing Directrix, using a vertical line axvline.
mpl.axvline(-a)
mpl.contour(x, y, parabola, [0], colors='r' )
mpl.show()
else :
axes()
mpl.contour(x, y, parabola, [0], colors='k')
mpl.show()
elif ch == 2:
while True:
print('2. Parabola in Expanded equation')
print('Parabolas in non-standard eqn are of the form :')
print('Ax^2+2Hxy+By^2+2Gx+2Fy+C=0, \n where H^2−4AB=0')
a = options(' Please enter the Value of A ')
b = options(' Please enter the Value of B ')
c = options(' Please enter the Value of C ')
f = options(' Please enter the Value of 2F ')
g = options(' Please enter the Value of 2G ')
h = options(' Please enter the Value of 2H ')
if delta(a,b,c,f,g,h) and (h)**2 - 4*a*b == 0 :
print('Your Equation is : ')
print('\t',a,'^2x + ',h,'xy + ',b,'y^2 + ',g,'x + ',f,'y + ',c,' = 0 ' )
axes()
mpl.contour(x, y,(a*x**2 + h*x*y + b*y**2 + g*x + f*y + c), [0], colors='k')
mpl.show()
else:
print('Condition : h**2 - 4*a*b = 0 \t NOT SATISFIED \n Try again :( ')
continue
elif ch == 3 :
break
else:
continue
elif opts == 2:
while True:
print('ELLIPSE AND CIRCLES')
print("MENU \n 1. Circle \n 2. Ellipse in Standard equation \n 3. Ellipse in Expanded equation \n 4.Return to Menu")
ch = int(input('Choose an option'))
if ch ==1 :
print('1. Circle')
r= int(input('Please enter radius of circle '))
h = int(input('Please enter x coordinate of centre '))
k = int(input('Please enter y coordinate of centre '))
print('Radius of circle ',r, 'with centre ','(',h,',',k,')')
t = np.linspace(0,2*np.pi, 1000)
x = r*np.cos(t) + h
y = r*np.sin(t) + k
mpl.axis('equal')
mpl.grid()
mpl.plot(x,y)
mpl.show()
if ch==2:
print('2. Ellipse in Standard equation')
print('x^2/A^2 + y^2/B^2 = 1')
a = options(' Please enter the Value of A ')
b = options(' Please enter the Value of B ')
print('Do you Want to see directrix and focus as well ? ')
duff = input (' y for YES and n for NO ')
while True:
if duff == 'y':
axes()
h = int(input('Please enter x coordinate of centre '))
k = int(input('Please enter y coordinate of centre '))
t = np.linspace(0,2*np.pi, 1000)
p = h + a*np.cos(t)
q = k + b*np.sin(t)
mpl.axis('equal')
mpl.plot(p,q)
# Defining Eccentricity.
e = np.sqrt(1 - b**2/a**2)
# Printing Foci as DOTs.
mpl.plot(a*e, 0, '.', -a*e, 0, '.')
# Printing Directrix, using a vertical lines axvline
mpl.axvline(a/e)
mpl.axvline(-a/e)
mpl.show()
break
elif duff == 'n' :
axes()
h = int(input('Please enter x coordinate of centre '))
k = int(input('Please enter y coordinate of centre '))
t = np.linspace(0,2*np.pi, 1000)
p = h + a*np.cos(t)
q = k + b*np.sin(t)
mpl.axis('equal')
mpl.grid()
mpl.plot(p,q)
mpl.show()
break
else :
continue
if ch ==3:
while True:
print('3. Ellipse in Expanded equation')
print('For ellipses, the eccentricity,e is 0<e<1 \n It is defined as e**2 = 1 - B**2/A**2 ')
print('To define a ellipse in a non-standard position is has to follow the form :')
print('Ax^2+2Hxy+By^2+2Gx+2Fy+C=0, \n where H^2−4AB<0')
a = options(' Please enter the Value of A ')
b = options(' Please enter the Value of B ')
c = options(' Please enter the Value of C ')
f = options(' Please enter the Value of 2F ')
g = options(' Please enter the Value of 2G ')
h = options(' Please enter the Value of 2H ')
if delta(a,b,c,f,g,h) and (h)**2 - 4*a*b < 0:
print('Your Equation is : ')
print('\t',a,'^2x + ',h,'xy + ',b,'y^2 + ',g,'x + ',f,'y + ',c,' = 0 ' )
axes()
mpl.contour(x, y,(a*x**2 + h*x*y + b*y**2 + g*x + f*y + c), [0], colors='k')
mpl.axis('equal')
mpl.show()
break
else :
print('Condition : 2h**2 - 4*a*b < 0 \t NOT SATISFIED \n Try again :( ')
continue
elif ch==4:
break
else:
continue
elif opts == 3:
q = np.linspace(-100, 101, 200)
w = np.linspace(-100, 101, 200)
q, w = np.meshgrid(q, w)
while True:
print('HYPERBOLA')
print('*** PRO TIP : Users are requested to use values less than 50, for best results :) ***')
print("MENU \n 1. Hyperbola in standard equation \n 2. Hyperbola in Expanded equation \n 3.Return to Menu")
ch = int(input('Choose an option'))
if ch == 1:
print('1. Hyperbolas in standard equation are of the form')
print('x^2/A^2 - y^2/B^2 = 1 ')
a = options(' Please enter the Value of A ')
b = options(' Please enter the Value of B ')
hyperbola = (q**2/a**2 - w**2/b**2)
print('Do you Want to see directrix and focus as well ? ')
duff = input (' y for YES and n for NO ')
while True:
if duff == 'y':
axes()
mpl.contour(q, w,hyperbola, [1], colors='k')
# Defining Eccentricity.
e = np.sqrt(1 + b**2/a**2)
# Printing Foci as DOTs.
mpl.plot(a*e, 0, '.', -a*e, 0, '.')
# Printing Directrix, using a vertical line axvline
mpl.axvline(a/e)
mpl.axvline(-a/e)
mpl.show()
break
elif duff =='n' :
axes()
mpl.contour(q, w,hyperbola, [1], colors='k')
mpl.show()
break
else:
continue
elif ch == 2:
while True:
print('2. Hperbola in Expanded equation')
print('Hyperbolas in non-standard eqn are of the form ::')
print('Ax^2+2Hxy+By^2+2Gx+2Fy+C=0, \n where H^2−4AB=0')
a = options(' Please enter the Value of A ')
b = options(' Please enter the Value of B ')
c = options(' Please enter the Value of C ')
f = options(' Please enter the Value of 2F ')
g = options(' Please enter the Value of 2G ')
h = options(' Please enter the Value of 2H ')
hyperx = (a*x**2 + h*x*y + b*y**2 + g*x + f*y + c)
if delta(a,b,c,f,g,h) and h**2 - 4*a*b > 0 :
print('Your Equation is : ')
print('\t',a,'^2x + ',h,'xy + ',b,'y^2 + ',g,'x + ',f,'y + ',c,' = 0 ' )
axes()
mpl.contour(x, y,hyperx, [0], colors='k')
mpl.show()
break
else:
print('Condition : h**2 - 4*a*b > 0 \t NOT SATISFIED \n Try again :( ')
continue
elif ch == 3 :
break
else:
continue
if opts == 4:
break
print('EOP')
# In[ ]: