-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectile_ODE.py
1118 lines (1010 loc) · 52.1 KB
/
Projectile_ODE.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Author: Praveenkumar Hiremath (Lund University 9/23/2019.)
from __future__ import division
import numpy as np
import matplotlib
from matplotlib import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import math
from math import *
import io
import os
from pylab import *
from scipy.optimize import leastsq
from matplotlib.pyplot import figure
figure(num=None, figsize=(7, 5), dpi=80, facecolor='w', edgecolor='k')
rcParams['legend.fontsize'] = 10
'''
Solve the "How hard can I throw the ball"-Project 1(a) in
the Computational Physics course at Lund University, FYTN03.
STUDENT:PRAVEENKUMAR HIREMATH
'''
try:
os.remove('ycoordinates.dat')
os.remove('xcoordinates.dat')
os.remove('zcoordinates.dat')
os.remove('vycoordinates.dat')
os.remove('vxcoordinates.dat')
os.remove('vzcoordinates.dat')
os.remove('RK2NRycoordinates.dat')
os.remove('RK2NRxcoordinates.dat')
os.remove('RK2NRzcoordinates.dat')
os.remove('RK2NRvycoordinates.dat')
os.remove('RK2NRvxcoordinates.dat')
os.remove('RK2NRvzcoordinates.dat')
os.remove('EulerNRycoordinates.dat')
os.remove('EulerNRxcoordinates.dat')
os.remove('EulerNRzcoordinates.dat')
os.remove('EulerNRvycoordinates.dat')
os.remove('EulerNRvxcoordinates.dat')
os.remove('EulerNRvzcoordinates.dat')
except OSError:
print ('Unwanted Files removed')
def euler_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m):
g=9.8 #gravitaional acceleration
size=N
vx_new=np.zeros([size+1])
vy_new=np.zeros([size+1])
vz_new=np.zeros([size+1])
v_new=np.zeros([size+1])
x_new=np.zeros([size+1])
y_new=np.zeros([size+1])
z_new=np.zeros([size+1])
rho_new=np.zeros([size+1])
B2_m=np.zeros([size+1])
x_new[0]=0.0
y_new[0]=0.0
z_new[0]=0.0
# Euler algorithm
rho_new[0]=float(rho_0)
v_new[0]=float(v_0)
vx_new[0]=float(vx_0)
vy_new[0]=float(vy_0)
vz_new[0]=0.0
y0=1E4 #((Kb*T)/(b_m*g))= Taken from G&N textbook
for i in range(0, N):
B2_m[i]=((0.5*C*rho_new[i]*A)/b_m)
x_new[i+1]=x_new[i]+(vx_new[i]*h)
F_drag_x=(((B2_m[i]*v_new[i]*vx_new[i]))*h)
F_drag_y=(((B2_m[i]*v_new[i]*vy_new[i]))*h)
vx_new[i+1]=vx_new[i]-((F_drag_x)*h)
y_new[i+1]=y_new[i]+(vy_new[i]*h)
vy_new[i+1]=vy_new[i]-(g*h)-((F_drag_y)*h)
z_new[i+1]=z_new[i]+(vz_new[i]*h)
vz_new[i+1]=vz_new[i]-(((S0_m*vx_new[i]*omega))*h)
v_new[i+1]=sqrt(pow(vx_new[i+1],2)+pow(vy_new[i+1],2)+pow(vz_new[i+1],2))
next_y=y_new[i]
rho_new[i+1]=rho_new[0]*exp(-next_y/y0)
if (y_new[i+1]<0.0):
negative=i+1
break
# print negative
max_rho=np.amax(rho_new)
max_B2_m=np.amax(B2_m)
# print max_rho, max_B2_m
final_height_euler=np.amax(y_new)
final_range_euler=np.amax(x_new)
curve=np.amax(z_new)
return final_height_euler,final_range_euler,x_new,y_new,z_new,vx_new,vy_new,vz_new,negative
def RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m):
m=1.0
a=0.5
b=0.5 # m, a, b chosen from book Numerical Recipes
g=9.8 #gravitaional acceleration
size=N
vx_new=np.zeros([size+1])
vy_new=np.zeros([size+1])
vz_new=np.zeros([size+1])
v_new=np.zeros([size+1])
x_new=np.zeros([size+1])
y_new=np.zeros([size+1])
z_new=np.zeros([size+1])
rho_new=np.zeros([size+1])
B2_m=np.zeros([size+1])
x_new[0]=0.0
y_new[0]=0.0
z_new[0]=0.0
# 2nd Order Runge-Kutta algorithm
rho_new[0]=float(rho_0)
v_new[0]=float(v_0)
vx_new[0]=float(vx_0)
vy_new[0]=float(vy_0)
vz_new[0]=0.0
y0=1E4 #((Kb*T)/(b_m*g))
for i in range(0, N):
B2_m[i]=((0.5*C*rho_new[i]*A)/b_m)
x_new[i+1]=x_new[i]+(vx_new[i]*h)
F1_drag_x=(((B2_m[i]*v_new[i]*vx_new[i]))*h)
k1_x=-((F1_drag_x)*h)
F2_drag_x=((B2_m[i]*v_new[i]*(vx_new[i]+(m*k1_x)))*h)
k2_x=-((F2_drag_x)*h)
vx_new[i+1]=vx_new[i]+(a*k1_x)+(b*k2_x)
y_new[i+1]=y_new[i]+(vy_new[i]*h)
F1_drag_y=(((B2_m[i]*v_new[i]*vy_new[i]))*h)
k1_y=-(g*h)-((F1_drag_y)*h)
F2_drag_y=((B2_m[i]*v_new[i]*(vy_new[i]+(m*k1_y)))*h)
k2_y=-(g*h)-((F2_drag_y)*h)
vy_new[i+1]=vy_new[i]+(a*k1_y)+(b*k2_y)
z_new[i+1]=z_new[i]+(vz_new[i]*h)
k1_z=-(((S0_m*vx_new[i]*omega))*h)
k2_z=-(((S0_m*(vx_new[i]+(m*k1_z))*omega))*h)
vz_new[i+1]=vz_new[i]+(a*k1_z)+(b*k2_z)
v_new[i+1]=sqrt(pow(vx_new[i+1],2)+pow(vy_new[i+1],2)+pow(vz_new[i+1],2))
next_y=y_new[i]
rho_new[i+1]=rho_new[0]*exp(-next_y/y0)
if (y_new[i+1]<0.0):
negative=i+1
break
max_rho=np.amax(rho_new)
max_B2_m=np.amax(B2_m)
# print max_B2_m
# print max_rho, max_B2_m
final_height_RK2=np.amax(y_new)
final_range_RK2=np.amax(x_new)
return final_height_RK2,final_range_RK2,x_new,y_new,z_new,vx_new,vy_new,vz_new,negative
##### Without Air resistance and other effects
def analytical_rangeR(v_0, angle_rad, initial_height):
g=9.8 #Gravitational acceleration
Analytic_rangeR=((pow(v_0,2)*sin(2*angle_rad))/(2*g))*(1+pow((1+((2*g*initial_height)/(pow(v_0,2)*pow(sin(angle_rad),2)))),0.5))
return Analytic_rangeR
###########GIVEN final velocity and angle and Horizontal range
def euler_reverse_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, vz_0, b_m, h, N, rho_0, S0_m):
# Euler reverse trajectory (Final to Initial position)
g=9.8 #gravitaional acceleration
size=N
vx_new=np.zeros([size+1])
vy_new=np.zeros([size+1])
vz_new=np.zeros([size+1])
v_new=np.zeros([size+1])
x_new=np.zeros([size+1])
y_new=np.zeros([size+1])
z_new=np.zeros([size+1])
rho_new=np.zeros([size+1])
B2_m=np.zeros([size+1])
x_new[N]=HR
y_new[N]=0.0
z_new[N]=ZR
# Euler algorithm
rho_new[N]=float(rho_0)
v_new[N]=float(v_0)
vx_new[N]=float(vx_0)
vy_new[N]=float(vy_0)
vz_new[N]=float(vz_0)
y0=1E4 #((Kb*T)/(b_m*g))
for i in range(N, 0, -1):
B2_m[i]=((0.5*C*rho_new[i]*A)/b_m)
x_new[i-1]=x_new[i]-(vx_new[i]*h)
F_drag_x=(((B2_m[i]*v_new[i]*vx_new[i]))*h)
F_drag_y=(((B2_m[i]*v_new[i]*vy_new[i]))*h)
vx_new[i-1]=vx_new[i]+((F_drag_x)*h)
y_new[i-1]=y_new[i]-(vy_new[i]*h)
vy_new[i-1]=vy_new[i]+(g*h)+((F_drag_y)*h)
z_new[i-1]=z_new[i]-(vz_new[i]*h)
vz_new[i-1]=vz_new[i]+(((S0_m*vx_new[i]*omega))*h)
v_new[i-1]=sqrt(pow(vx_new[i-1],2)+pow(vy_new[i-1],2)+pow(vz_new[i-1],2))
next_y=y_new[i]
rho_new[i-1]=rho_new[0]*exp(-next_y/y0)
if (y_new[i-1]<0.0):
negative=i-1
break
max_rho=np.amax(rho_new)
max_B2_m=np.amax(B2_m)
# print max_rho, max_B2_m
final_height_euler=np.amax(y_new)
final_range_euler=np.amax(x_new)
curve=np.amax(z_new)
return final_height_euler,final_range_euler,x_new,y_new,z_new,vx_new,vy_new,vz_new,negative
def cannon_RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, B2_m, S0_m):
m=1.0
a=0.5
b=0.5 # m, a, b chosen from book Numerical Recipes
g=9.8 #gravitaional acceleration
size=N
vx_new=np.zeros([size+1])
vy_new=np.zeros([size+1])
vz_new=np.zeros([size+1])
v_new=np.zeros([size+1])
x_new=np.zeros([size+1])
y_new=np.zeros([size+1])
z_new=np.zeros([size+1])
rho_new=np.zeros([size+1])
# B2_m=np.zeros([size+1])
x_new[0]=0.0
y_new[0]=0.0
z_new[0]=0.0
v_new[0]=float(v_0)
vx_new[0]=float(vx_0)
vy_new[0]=float(vy_0)
vz_new[0]=0.0
y0=1E4 #((Kb*T)/(b_m*g))
# 2nd Order Runge-Kutta algorithm
for i in range(0, N, 1):
x_new[i+1]=x_new[i]+(vx_new[i]*h)
F1_drag_x=(((B2_m*v_new[i]*vx_new[i]))*h)
k1_x=-((F1_drag_x)*h)
F2_drag_x=((B2_m*v_new[i]*(vx_new[i]+(m*k1_x)))*h)
k2_x=-((F2_drag_x)*h)
vx_new[i+1]=vx_new[i]+(a*k1_x)+(b*k2_x)
y_new[i+1]=y_new[i]+(vy_new[i]*h)
F1_drag_y=(((B2_m*v_new[i]*vy_new[i]))*h)
k1_y=-(g*h)-((F1_drag_y)*h)
F2_drag_y=((B2_m*v_new[i]*(vy_new[i]+(m*k1_y)))*h)
k2_y=-(g*h)-((F2_drag_y)*h)
vy_new[i+1]=vy_new[i]+(a*k1_y)+(b*k2_y)
z_new[i+1]=z_new[i]+(vz_new[i]*h)
k1_z=-(((S0*vx_new[i]*omega)/b_m)*h)
k2_z=-(((S0*(vx_new[i]+(m*k1_z))*omega)/b_m)*h)
vz_new[i+1]=vz_new[i]+(a*k1_z)+(b*k2_z)
v_new[i+1]=sqrt(pow(vx_new[i+1],2)+pow(vy_new[i+1],2)+pow(vz_new[i+1],2))
next_y=y_new[i]
if (y_new[i+1]<0.0):
negative=i+1
break
max_B2_m=B2_m
final_height_RK2=np.amax(y_new)
final_range_RK2=np.amax(x_new)
return final_height_RK2,final_range_RK2,x_new,y_new,z_new,vx_new,vy_new,vz_new,negative
def cannon_euler_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0,B2_m):
g=9.8 #gravitaional acceleration
size=N
vx_new=np.zeros([size+1])
vy_new=np.zeros([size+1])
vz_new=np.zeros([size+1])
v_new=np.zeros([size+1])
x_new=np.zeros([size+1])
y_new=np.zeros([size+1])
z_new=np.zeros([size+1])
x_new[0]=0.0
y_new[0]=0.0
z_new[0]=0.0
# Euler algorithm
v_new[0]=float(v_0)
vx_new[0]=float(vx_0)
vy_new[0]=float(vy_0)
vz_new[0]=0.0
for i in range(0, N, 1):
x_new[i+1]=x_new[i]+(vx_new[i]*h)
F_drag_x=(((B2_m*v_new[i]*vx_new[i]))*h)
F_drag_y=(((B2_m*v_new[i]*vy_new[i]))*h)
vx_new[i+1]=vx_new[i]-((F_drag_x)*h)
y_new[i+1]=y_new[i]+(vy_new[i]*h)
vy_new[i+1]=vy_new[i]-(g*h)-((F_drag_y)*h)
z_new[i+1]=z_new[i]+(vz_new[i]*h)
vz_new[i+1]=vz_new[i]
v_new[i+1]=sqrt(pow(vx_new[i+1],2)+pow(vy_new[i+1],2)+pow(vz_new[i+1],2))
next_y=y_new[i]
if (y_new[i+1]<0.0):
negative=i+1
break
# print negative
# max_rho=np.amax(rho_new)
# print "DRAG"
# print F_drag_x, F_drag_y
max_B2_m=B2_m
# print max_B2_m
# print max_rho, max_B2_m
final_height_euler=np.amax(y_new)
final_range_euler=np.amax(x_new)
curve=np.amax(z_new)
return final_height_euler,final_range_euler,x_new,y_new,z_new,vx_new,vy_new,vz_new,negative
def Baseball_RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, B2_m,S0_m):
m=1.0
a=0.5
b=0.5 # m, a, b chosen from book Numerical Recipes
g=9.8 #gravitaional acceleration
size=N
vx_new=np.zeros([size+1])
vy_new=np.zeros([size+1])
vz_new=np.zeros([size+1])
v_new=np.zeros([size+1])
x_new=np.zeros([size+1])
y_new=np.zeros([size+1])
z_new=np.zeros([size+1])
rho_new=np.zeros([size+1])
# B2_m=np.zeros([size+1])
x_new[0]=0.0
y_new[0]=1.128 #in meter. 1.128 meter is approximately 3.7 feet
z_new[0]=0.0
v_new[0]=float(v_0)
vx_new[0]=float(vx_0)
vy_new[0]=float(vy_0)
vz_new[0]=0.0
# 2nd Order Runge-Kutta algorithm
for i in range(0, N, 1):
x_new[i+1]=x_new[i]+(vx_new[i]*h)
F1_drag_x=(((B2_m*v_new[i]*vx_new[i]))*h)
k1_x=-((F1_drag_x)*h)
F2_drag_x=((B2_m*v_new[i]*(vx_new[i]+(m*k1_x)))*h)
k2_x=-((F2_drag_x)*h)
vx_new[i+1]=vx_new[i]+(a*k1_x)+(b*k2_x)
y_new[i+1]=y_new[i]+(vy_new[i]*h)
k1_y=-(g*h)
k2_y=-(g*h)
vy_new[i+1]=vy_new[i]+(a*k1_y)+(b*k2_y)
z_new[i+1]=z_new[i]+(vz_new[i]*h)
k1_z=-(((S0_m*vx_new[i]*omega))*h)
k2_z=-(((S0_m*(vx_new[i]+(m*k1_z))*omega))*h)
vz_new[i+1]=vz_new[i]+(a*k1_z)+(b*k2_z)
v_new[i+1]=sqrt(pow(vx_new[i+1],2)+pow(vy_new[i+1],2)+pow(vz_new[i+1],2))
next_y=y_new[i]
if (y_new[i+1]<0.0):
negative=i+1
break
max_B2_m=B2_m
final_height_RK2=np.amax(y_new)
final_range_RK2=np.amax(x_new)
return final_height_RK2,final_range_RK2,x_new,y_new,z_new,vx_new,vy_new,vz_new,negative
def Baseball_euler_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0,B2_m, S0_m):
g=9.8 #gravitaional acceleration
size=N
vx_new=np.zeros([size+1])
vy_new=np.zeros([size+1])
vz_new=np.zeros([size+1])
v_new=np.zeros([size+1])
x_new=np.zeros([size+1])
y_new=np.zeros([size+1])
z_new=np.zeros([size+1])
x_new[0]=0.0
y_new[0]=3.7
z_new[0]=0.0
# Euler algorithm
v_new[0]=float(v_0)
vx_new[0]=float(vx_0)
vy_new[0]=float(vy_0)
vz_new[0]=0.0
for i in range(0, N):
x_new[i+1]=x_new[i]+(vx_new[i]*h)
F_drag_x=(((B2_m*v_new[i]*vx_new[i]))*h)
print F_drag_x
vx_new[i+1]=vx_new[i]-((F_drag_x)*h)
y_new[i+1]=y_new[i]+(vy_new[i]*h)
vy_new[i+1]=vy_new[i]-(g*h)
z_new[i+1]=z_new[i]+(vz_new[i]*h)
vz_new[i+1]=vz_new[i]-(((S0_m*vx_new[i]*omega))*h)
v_new[i+1]=sqrt(pow(vx_new[i+1],2)+pow(vy_new[i+1],2)+pow(vz_new[i+1],2))
next_y=y_new[i]
if (y_new[i+1]<0.0):
negative=i+1
break
max_B2_m=np.amax(B2_m)
# print max_rho, max_B2_m
final_height_euler=np.amax(y_new)
final_range_euler=np.amax(x_new)
curve=np.amax(z_new)
return final_height_euler,final_range_euler,x_new,y_new,z_new,vx_new,vy_new,vz_new,negative
print "Note: Please first run task 4\n"
pi=22/7
DO=input("Enter what you want to do\n1. Cannon trajectory in presence of only air resistance, altitude effect and gravity\n2. Effect of air resistance on initial (throwing) angle in cannon (as in book G&N:only air resistance and gravity)\n3. Baseball: curve ball trajectory\n4. Test Euler algorithm with air and other effect and test 2nd Order RK algorithm with air and other effect and compare\n5. Calculate horizontal range analytically using the available expression. Then use 2nd Order RK and Euler for the case without resistance to compare with analytical value for horizontal range X (Validity of code)\n6. Calculate optimal angle for a given initial velocity (considering different influencing factors)\n7. Calculate optimal angle for a given initial velocity (under only G)\n8. Plot the reverse tranjectory when final horizontal, vertical, deviation along z-axis, velocity and angle are known\n9. Finding initial conditions when different final conditions are known\n")
print "Chosen task is =",DO
if (DO==1):
print "Cannon ball example from textbook and only air resistance, altitude effects are considered 2nd order RK used"
v_0=700 # Cannon initial velocity in ms^-1 (100.662 mph)
angle=40 # angle in degrees
angle_rad=(pi*angle)/180 # initial angle (at t=0) in radians
vx_0=v_0*cos(angle_rad) # initial velocity (at t=0) in x_direction
vy_0=v_0*sin(angle_rad) # initial velocity (at t=0) in y_direction
b_m=0.1924 # Mass of cannon in kg
B1=0.0 # Stoke's drag which is neglected and set to zero for macroscopic objects
C=0.47 # Shape dependent constant (experimentally determined C-values) for sphere (Re approximately < 20E-4)
rho_0=1.225 # Density at sea level (approximate)
A=7.854E-3 # Frontal area (cannon frontal area in m^2 as diameter is 10cm)
h1=0.000041 # step size (timestep) in s
h=h1
N=5000000; # number of euler steps (number of timesteps)
T=273 # Temperature in Kelvin
Kb=1.38064852E-23 # Boltzman constant
S0=0.00006109 # SPIN
S0_m=0.0
rpm=0 # revolutions per second
omega=2*pi*rpm # radians per second
print "Cannon ball Initial velocity =", v_0, "m/s and Initial angle =", angle_rad,"in radians=",angle,"degrees"
final_height_cannon,final_range_cannon,x_new_cannon,y_new_cannon,z_new_cannon,vx_new_cannon,vy_new_cannon,vz_new_cannon,cannon_neg_index=RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m)
x_new_cannon=x_new_cannon[:cannon_neg_index]
y_new_cannon=y_new_cannon[:cannon_neg_index]
z_new_cannon=z_new_cannon[:cannon_neg_index]
vx_new_cannon=vx_new_cannon[:cannon_neg_index]
vy_new_cannon=vy_new_cannon[:cannon_neg_index]
vz_new_cannon=vz_new_cannon[:cannon_neg_index]
cannonxyz=np.zeros(3)
cannonxyz=np.array([vx_new_cannon[cannon_neg_index-1],vy_new_cannon[cannon_neg_index-1],vz_new_cannon[cannon_neg_index-1]])
np.savetxt('cannonfinal_vel_components.dat',cannonxyz)
##### TO SAVE/OPEN COORDINATES AND VELOCITIES INTO FILES BLOCK (UNCOMMENT TO USE)
np.savetxt('cannonRK2xcoordinates.dat', x_new_cannon)
np.savetxt('cannonRK2ycoordinates.dat', y_new_cannon)
np.savetxt('cannonRK2zcoordinates.dat', z_new_cannon)
np.savetxt('cannonRK2vxcoordinates.dat', vx_new_cannon)
np.savetxt('cannonRK2vycoordinates.dat', vy_new_cannon)
np.savetxt('cannonRK2vzcoordinates.dat', vy_new_cannon)
cannonRK2_x=np.loadtxt('cannonRK2xcoordinates.dat')
cannonRK2_y=np.loadtxt('cannonRK2ycoordinates.dat')
cannonRK2_z=np.loadtxt('cannonRK2zcoordinates.dat')
#### TO PLOT TRAJECTORY IN XY/XZ PLANE BLOCK (UNCOMMENT TO USE)
# fig, ax = plt.subplots()
# ax.plot(cannonRK2_x,cannonRK2_z,'y--',label='cannonXZ-Plane Runge-Kutta 2nd Order, h=0.000041')
# ax.plot(cannonRK2_x,cannonRK2_y,'y--',label='cannonXY-Plane Runge-Kutta 2nd Order, h=0.000041')
# ax.set_title('CANNON PROJECTILE XY PLANE')
# ax.grid(True)
# xlabel('X Range (m)')
# ylabel('Height (m)')
# ylabel('Distance along Z-axis (m)')
# legend(loc='best')
# savefig('cannonXYProjectile_compare.png')
#### TO PLOT TRAJECTORY IN 3D BLOCK (UNCOMMENT TO USE)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(cannonRK2_x, cannonRK2_y, cannonRK2_z,'r--', label='Cannon trajectory 2nd Order RK, h=0.000041')
ax.set_xlabel('X (Horizonatal Range (m))')
ax.set_ylabel('Y (Elevation from ground (m))')
ax.set_zlabel('Z (distance along magnus force (m))')
legend(loc='best')
#print ax.azim
ax.view_init(azim=10)
savefig('cannon3DProjectile.png')
print "Using RK2, cannon final height, cannon final range, cannon z_deflection respectively are:", final_height_cannon,final_range_cannon,z_new_cannon[cannon_neg_index-1]
if (DO==2):
######## DIFFERENT ANGLES
print "Cannon ball example from textbook and only air resistance is considered"
v_0=700 # Cannon initial velocity in ms^-1 (100.662 mph)
angle=40 # angle in degrees
angle_rad=(pi*angle)/180 # initial angle (at t=0) in radians
vx_0=v_0*cos(angle_rad) # initial velocity (at t=0) in x_direction
vy_0=v_0*sin(angle_rad) # initial velocity (at t=0) in y_direction
b_m=0.1924 # Mass of cannon in kg
B1=0.0 # Stoke's drag which is neglected and set to zero for macroscopic objects
C=1.00 # Shape dependent constant (experimentally determined C-values) for sphere (C=1 for with air drag, C=0 for no air drag)
rho_0=1.225 # Density at sea level (approximate)
A=7.854E-3 # Frontal area (Baseball frontal area in m^2=0.004417865)
d=0.1 # diameter of cannon
B2_m=1E-1 # Calculated by ((0.5*C*rho_0*A)/b_m) where A=((pi*d*d)/4)
h1=0.00041 # step size (timestep) in s
h=h1
N=500000; # number of euler steps (number of timesteps)
T=273 # Temperature in Kelvin
Kb=1.38064852E-23 # Boltzman constant
S0=0.00006109 # SPIN
S0_m=0.0
rpm=0 # revolutions per second
omega=2*pi*rpm # radians per second
print "Cannon ball Initial velocity =", v_0, "m/s and Initial angle =", angle_rad,"in radians=",angle,"degrees"
Num_angle_iter=7
angles_degree=np.linspace(30,60,Num_angle_iter)
angles_radians=(pi*angles_degree)/180
final_height_cannonRK2=np.zeros(Num_angle_iter)
final_range_cannonRK2=np.zeros(Num_angle_iter)
all_xranges_cannonRK2=np.zeros(Num_angle_iter)
cannonneg_index=np.zeros(Num_angle_iter)
for i in range(0,Num_angle_iter,1):
vx_0=v_0*cos(angles_radians[i])
vy_0=v_0*sin(angles_radians[i])
final_height_cannonRK2[i],final_range_cannonRK2[i],x_newcannon,y_newcannon,z_newcannon,vx_newcannon,vy_newcannon,vz_newcannon,cannonneg_index[i]=cannon_RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, B2_m, S0_m)
all_xranges_cannonRK2[i]=final_range_cannonRK2[i]
#print angles_degree[i], final_range_cannonRK2[i]
temp=int(cannonneg_index[i])
#print temp
x_newcannon=x_newcannon[:temp]
x_newcannon=x_newcannon/1000
y_newcannon=y_newcannon[:temp]
y_newcannon=y_newcannon/1000
z_newcannon=z_newcannon[:temp]
z_newcannon=z_newcannon/1000
##### ANGLE DEPENDENCE IN PRESENCE OF OTHER EFFECTS + AIR RESISTANCE PLOT BLOCK (UNCOMMENT TO USE)
plt.plot(x_newcannon, y_newcannon, label=angles_degree[i])
plt.axis([0, 55, 0, 25])
plt.xlabel('X Range (km)')
plt.ylabel('Height (km)')
plt.grid(True)
plt.title('CANNON WITH A GIVEN VELOCITY (Without air drag)\nAT DIFFERENT ANGLES (DEGREES) XY PLANE (RK2)')
legend(loc='best',prop={'size': 8})
savefig('CANNON_XYRK_Angles_No_factor_projectile.png')
for i in range(1,Num_angle_iter,1):
index_max_x=argmax(all_xranges_cannonRK2)
Opt_angle= angles_degree[index_max_x]
# print "Optimal initial angle (degree) of initial velocity ="
print "Optimal angle and xrange"
print Opt_angle, all_xranges_cannonRK2[index_max_x]
if (DO==3):
print "Baseball: Trajectory of side arm curve ball"
v_0=31.2928 # Baseball initial velocity in ms^-1 (70 mph)
angle=0.0 # angle=0.0 degrees because the ball is thrown with an initial velocity in x-direction
angle_rad=(pi*angle)/180 # initial angle (at t=0) in radians
vx_0=v_0*cos(angle_rad) # initial velocity (at t=0) in x_direction
vy_0=v_0*sin(angle_rad) # initial velocity (at t=0) in y_direction
b_m=0.149 # Mass of baseball in kg
B1=0.0 # Stoke's drag which is neglected and set to zero for macroscopic objects
C=0.47 # Shape dependent constant (experimentally determined C-values) for sphere (C=1 for with air drag, C=0 for no air drag)
rho_0=1.225 # Density at sea level (approximate)
A=4.5365E-3 # Frontal area (Baseball frontal area in m^2=0.004417865)
d=0.076 # diameter of baseball
B2_m=8.76465E-3 # Calculated by ((0.5*C*rho_0*A)/b_m) where A=((pi*d*d)/4)
h1=0.000041 # step size (timestep) in s
h=h1
N=500000; # number of euler steps (number of timesteps)
T=273 # Temperature in Kelvin
Kb=1.38064852E-23 # Boltzman constant
S0=0.06109 # Taken from textbook (G&N) for baseball. This value here is per kg.
S0_m=4.1E-4
rpm=30 # revolutions per second
omega=2*pi*rpm # radians per second
print "Cannon ball Initial velocity =", v_0, "m/s and Initial angle =", angle_rad,"in radians=",angle,"degrees"
print "angle=0.0 degrees because the ball is thrown with an initial velocity in x-direction"
final_height_BaseRK2,final_range_BaseRK2,x_new_BaseRK2,y_new_BaseRK2,z_new_BaseRK2,vx_new_BaseRK2,vy_new_BaseRK2,vz_new_BaseRK2,BaseRK2_neg_index=Baseball_RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, B2_m, S0_m)
#final_height_BaseRK2,final_range_BaseRK2,x_new_BaseRK2,y_new_BaseRK2,z_new_BaseRK2,vx_new_BaseRK2,vy_new_BaseRK2,vz_new_BaseRK2,BaseRK2_neg_index=Baseball_euler_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, B2_m, S0_m)
x_new_BaseRK2=x_new_BaseRK2[:BaseRK2_neg_index]
y_new_BaseRK2=y_new_BaseRK2[:BaseRK2_neg_index]
z_new_BaseRK2=z_new_BaseRK2[:BaseRK2_neg_index]
vx_new_BaseRK2=vx_new_BaseRK2[:BaseRK2_neg_index]
vy_new_BaseRK2=vy_new_BaseRK2[:BaseRK2_neg_index]
vz_new_BaseRK2=vz_new_BaseRK2[:BaseRK2_neg_index]
Basetempxyz=np.zeros(3)
Basetempxyz=np.array([vx_new_BaseRK2[BaseRK2_neg_index-1],vy_new_BaseRK2[BaseRK2_neg_index-1],vz_new_BaseRK2[BaseRK2_neg_index-1]])
#np.savetxt('baseball_final_vel_components.dat',tempxyz)
##### TO SAVE/OPEN COORDINATES AND VELOCITIES INTO FILES BLOCK (UNCOMMENT TO USE)
np.savetxt('BaseRK2xcoordinates.dat', x_new_BaseRK2)
np.savetxt('BaseRK2ycoordinates.dat', y_new_BaseRK2)
np.savetxt('BaseRK2zcoordinates.dat', z_new_BaseRK2)
np.savetxt('BaseRK2vxcoordinates.dat', vx_new_BaseRK2)
np.savetxt('BaseRK2vycoordinates.dat', vy_new_BaseRK2)
np.savetxt('BaseRK2vzcoordinates.dat', vy_new_BaseRK2)
BaseRK2_x=np.loadtxt('BaseRK2xcoordinates.dat')
BaseRK2_y=np.loadtxt('BaseRK2ycoordinates.dat')
BaseRK2_z=np.loadtxt('BaseRK2zcoordinates.dat')
#### TO PLOT TRAJECTORY IN XY/XZ PLANE BLOCK (UNCOMMENT TO USE)
#fig, ax = plt.subplots()
#ax.plot(BaseRK2_x,BaseRK2_z,'r-x',label='Baseball XZ-Plane Runge-Kutta 2nd Order, h=0.000041')
#ax.plot(BaseRK2_x,BaseRK2_y,'r-x',label='Baseball XY-Plane Runge-Kutta 2nd Order, h=0.000041')
#ax.set_title('BASEBALL TRAJECTORY XY PLANE')
#ax.grid(True)
#xlabel('X Range (m)')
#ylabel('Height (m)')
#ylabel('Distance along Z-axis (m)')
#legend(loc='best')
#savefig('BaseballXYProjectile_compare.png')
#### TO PLOT TRAJECTORY IN 3D BLOCK (UNCOMMENT TO USE)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(BaseRK2_x, BaseRK2_y, BaseRK2_z,'r-x', label='Baseball trajectory 2nd Order RK, h=0.000041')
ax.set_xlabel('X (Horizonatal Range (m))')
ax.set_ylabel('Y (Elevation from ground (m))')
ax.set_zlabel('Z (distance along magnus force (m))')
legend(loc='best')
#print ax.azim
ax.view_init(azim=10)
savefig('Baseball3DProjectile.png')
print "Using RK2, Baseball Initial height, final horizontal range, z_deflection respectively are (in m):",final_height_BaseRK2, final_range_BaseRK2,z_new_BaseRK2[BaseRK2_neg_index-1]
#print "Using Euler, Baseball Initial height, final horizontal range, z_deflection respectively are (in m):",final_height_BaseRK2, final_range_BaseRK2,z_new_BaseRK2[BaseRK2_neg_index-1]
###### Comparison of Euler and RK2 performance
if (DO==4):
v_0=45 # initial velocity in ms^-1 (100.662 mph)
angle=40 # angle in degrees
angle_rad=(pi*angle)/180 # initial angle (at t=0) in radians
vx_0=v_0*cos(angle_rad) # initial velocity (at t=0) in x_direction
vy_0=v_0*sin(angle_rad) # initial velocity (at t=0) in y_direction
b_m=0.149 # Mass of baseball in kg
B1=0.0 # Stoke's drag which is neglected and set to zero for macroscopic objects
C=0.47 # Shape dependent constant (experimentally determined C-values) for sphere (C=1 for with air drag, C=0 for no air drag)
rho_0=1.225 # Density at sea level (approximate)
A=4.5365E-3 # Frontal area (Baseball diameter = 7.6cm)
d=0.076 # diameter of baseball
B2_m=8.76465E-3 # Calculated by ((0.5*C*rho_0*A)/b_m) where A=((pi*d*d)/4)
h1=0.000041 # step size (timestep) in s
h=h1
N=500000; # number of euler steps (number of timesteps)
T=273 # Temperature in Kelvin
Kb=1.38064852E-23 # Boltzman constant
S0=0.06109 # Taken from textbook (G&N) for baseball. This value here is per kg.
S0_m=4.1E-4 # Taken from textbook (G&N) for baseball.
rpm=30 # revolutions per second
omega=2*pi*rpm # radians per second
print "Initial velocity =", v_0, "m/s and Initial angle =", angle_rad,"in radians=",angle,"degrees"
final_height_euler,final_range_euler,x_new_euler,y_new_euler,z_new_euler,vx_new_euler,vy_new_euler,vz_new_euler,euler_neg_index=euler_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m)
x_new_euler=x_new_euler[:euler_neg_index]
y_new_euler=y_new_euler[:euler_neg_index]
z_new_euler=z_new_euler[:euler_neg_index]
##### TO SAVE/OPEN COORDINATES AND VELOCITIES INTO FILES BLOCK (UNCOMMENT TO USE)
np.savetxt('xcoordinates.dat', x_new_euler)
np.savetxt('ycoordinates.dat', y_new_euler)
np.savetxt('zcoordinates.dat', z_new_euler)
np.savetxt('vxcoordinates.dat', vx_new_euler)
np.savetxt('vycoordinates.dat', vy_new_euler)
np.savetxt('vzcoordinates.dat', vy_new_euler)
print "Using euler, Final height, final range, z_deflection respectively are:", final_height_euler,final_range_euler,z_new_euler[euler_neg_index-1]
h2=0.000041
h=h2
final_height_RK2,final_range_RK2,x_new_RK2,y_new_RK2,z_new_RK2,vx_new_RK2,vy_new_RK2,vz_new_RK2,RK2_neg_index=RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m)
x_new_RK2=x_new_RK2[:RK2_neg_index]
y_new_RK2=y_new_RK2[:RK2_neg_index]
z_new_RK2=z_new_RK2[:RK2_neg_index]
vx_new_RK2=vx_new_RK2[:RK2_neg_index]
vy_new_RK2=vy_new_RK2[:RK2_neg_index]
vz_new_RK2=vz_new_RK2[:RK2_neg_index]
tempxyz=np.zeros(3)
tempxyz=np.array([vx_new_RK2[RK2_neg_index-1],vy_new_RK2[RK2_neg_index-1],vz_new_RK2[RK2_neg_index-1]])
np.savetxt('final_vel_components.dat',tempxyz)
##### TO SAVE/OPEN COORDINATES AND VELOCITIES INTO FILES BLOCK (UNCOMMENT TO USE)
np.savetxt('RK2xcoordinates.dat', x_new_RK2)
np.savetxt('RK2ycoordinates.dat', y_new_RK2)
np.savetxt('RK2zcoordinates.dat', z_new_RK2)
np.savetxt('RK2vxcoordinates.dat', vx_new_RK2)
np.savetxt('RK2vycoordinates.dat', vy_new_RK2)
np.savetxt('RK2vzcoordinates.dat', vy_new_RK2)
euler_x=np.loadtxt('xcoordinates.dat')
euler_y=np.loadtxt('ycoordinates.dat')
euler_z=np.loadtxt('zcoordinates.dat')
RK2_x=np.loadtxt('RK2xcoordinates.dat')
RK2_y=np.loadtxt('RK2ycoordinates.dat')
RK2_z=np.loadtxt('RK2zcoordinates.dat')
#### TO PLOT TRAJECTORY IN XY/XZ PLANE BLOCK (UNCOMMENT TO USE)
#fig, ax = plt.subplots()
#ax.plot(euler_x,euler_z,'r-x',label='XZ-Plane Euler Forward, h=0.000041')
#ax.plot(RK2_x,RK2_z,'y--',label='XZ-Plane Runge-Kutta 2nd Order, h=0.000041')
#ax.plot(euler_x,euler_y,'r-x',label='XY-Plane Euler Forward, h=0.000041')
#ax.plot(RK2_x,RK2_y,'y--',label='XY-Plane Runge-Kutta 2nd Order, h=0.000041')
#ax.set_title('PROJECTILE XY PLANE')
#ax.grid(True)
#xlabel('X Range (m)')
#ylabel('Height (m)')
#ylabel('Distance along Z-axis (m)')
#legend(loc='best')
#savefig('XYProjectile_compare.png')
#### TO PLOT TRAJECTORY IN 3D BLOCK (UNCOMMENT TO USE)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(euler_x, euler_y, euler_z, 'r-x', label='Ball trajectory Euler, h=0.000041')
ax.plot(RK2_x, RK2_y, RK2_z,'y--', label='Ball trajectory 2nd Order RK, h=0.000041')
ax.set_xlabel('X (Horizonatal Range (m))')
ax.set_ylabel('Y (Elevation from ground (m))')
ax.set_zlabel('Z (distance along magnus force (m))')
legend(loc='best')
ax.view_init(azim=10)
savefig('3DProjectile.png')
print "Using RK2, Final height, final range, z_deflection respectively are:", final_height_RK2,final_range_RK2,z_new_RK2[RK2_neg_index-1]
RK2_x=np.loadtxt('RK2xcoordinates.dat')
RK2_y=np.loadtxt('RK2ycoordinates.dat')
RK2_z=np.loadtxt('RK2zcoordinates.dat')
final_range_RK2=np.amax(RK2_x)
final_height_RK2=np.amax(RK2_y)
max_z_deflection=np.amax(RK2_z)
max_z_deflection=np.amin(RK2_z)
if (DO==5):
#### In absence of air resistance and other influencing factors
v_0=45 # initial velocity in ms^-1 (100.662 mph)
angle=40 # angle in degrees
angle_rad=(pi*angle)/180 # initial angle (at t=0) in radians
vx_0=v_0*cos(angle_rad) # initial velocity (at t=0) in x_direction
vy_0=v_0*sin(angle_rad) # initial velocity (at t=0) in y_direction
b_m=0.149 # Mass of baseball in kg
B1=0.0 # Stoke's drag which is neglected and set to zero for macroscopic objects
C=0.00 # Shape dependent constant (experimentally determined C-values) for sphere (C=1 for with air drag, C=0 for no air drag)
rho_0=1.225 # Density at sea level (approximate)
A=4.5365E-3 # Frontal area (Baseball frontal area in m^2)
d=0.076 # diameter of baseball
B2_m=8.76465E-3 # Calculated by ((0.5*C*rho_0*A)/b_m) where A=((pi*d*d)/4)
h1=0.00041 # step size (timestep) in s
h=h1
N=500000; # number of euler steps (number of timesteps)
T=273 # Temperature in Kelvin
Kb=1.38064852E-23 # Boltzman constant
S0=0.06109 # Taken from textbook (G&N) for baseball. This value here is per kg.
S0_m=0.0 # Not considering spin
rpm=0.0 # revolutions per second
omega=2*pi*rpm # radians per second
final_height_RK2_without_resistance,final_range_RK2_without_resistance,x_new_RK2_WO_R,y_new_RK2_WO_R,z_new_RK2_WO_R,vx_new_RK2_WO_R,vy_new_RK2_WO_R,vz_new_RK2_WO_R,RK2_neg_index_WO_R=RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m)
x_new_RK2_WO_R=x_new_RK2_WO_R[:RK2_neg_index_WO_R]
y_new_RK2_WO_R=y_new_RK2_WO_R[:RK2_neg_index_WO_R]
z_new_RK2_WO_R=z_new_RK2_WO_R[:RK2_neg_index_WO_R]
np.savetxt('RK2NRxcoordinates.dat', x_new_RK2_WO_R)
np.savetxt('RK2NRycoordinates.dat', y_new_RK2_WO_R)
np.savetxt('RK2NRzcoordinates.dat', z_new_RK2_WO_R)
np.savetxt('RK2NRvxcoordinates.dat', vx_new_RK2_WO_R)
np.savetxt('RK2NRvycoordinates.dat', vy_new_RK2_WO_R)
np.savetxt('RK2NRvzcoordinates.dat', vy_new_RK2_WO_R)
print "Horizontal range when there is no resistance, using RK 2nd order="
print final_range_RK2_without_resistance
print "Vertical range when there is no resistance, using RK 2nd order="
print final_height_RK2_without_resistance
h2=0.00041
h=h2
final_height_Euler_without_resistance,final_range_Euler_without_resistance,x_new_Euler_WO_R,y_new_Euler_WO_R,z_new_Euler_WO_R,vx_new_Euler_WO_R,vy_new_Euler_WO_R,vz_new_Euler_WO_R,Euler_neg_index_WO_R=euler_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m)
x_new_Euler_WO_R=x_new_Euler_WO_R[:Euler_neg_index_WO_R]
y_new_Euler_WO_R=y_new_Euler_WO_R[:Euler_neg_index_WO_R]
z_new_Euler_WO_R=z_new_Euler_WO_R[:Euler_neg_index_WO_R]
np.savetxt('EulerNRxcoordinates.dat', x_new_Euler_WO_R)
np.savetxt('EulerNRycoordinates.dat', y_new_Euler_WO_R)
np.savetxt('EulerNRzcoordinates.dat', z_new_Euler_WO_R)
np.savetxt('EulerNRvxcoordinates.dat', vx_new_Euler_WO_R)
np.savetxt('EulerNRvycoordinates.dat', vy_new_Euler_WO_R)
np.savetxt('EulerNRvzcoordinates.dat', vy_new_Euler_WO_R)
print "Horizontal range when there is no resistance, using euler="
print final_range_Euler_without_resistance
print "Vertical range when there is no resistance, using euler="
print final_height_Euler_without_resistance
euler_NR_x=np.loadtxt('EulerNRxcoordinates.dat')
euler_NR_y=np.loadtxt('EulerNRycoordinates.dat')
euler_NR_z=np.loadtxt('EulerNRzcoordinates.dat')
RK2_NR_x=np.loadtxt('RK2NRxcoordinates.dat')
RK2_NR_y=np.loadtxt('RK2NRycoordinates.dat')
RK2_NR_z=np.loadtxt('RK2NRzcoordinates.dat')
##### NO AIR RESISTANCE PLOT BLOCK (UNCOMMENT TO USE)
#fig, ax = plt.subplots()
### Uncomment following two lines for XZ plot
#ax.plot(euler_NR_x,euler_NR_z,'r-x',label='XZ-Plane Euler Forward, h=0.000041')
#ax.plot(RK2_NR_x,RK2_NR_z,'y--',label='XZ-Plane Runge-Kutta 2nd Order, h=0.000041')
### Uncomment following two lines for XY plot
#ax.plot(euler_NR_x,euler_NR_y,'r-x',label='XY-Plane Euler Forward, h=0.000041')
#ax.plot(RK2_NR_x,RK2_NR_y,'y--',label='XY-Plane Runge-Kutta 2nd Order, h=0.000041')
#ax.set_title('PROJECTILE (NO RESISTANCE) XY PLANE')
#ax.grid(True)
#xlabel('X Range (m)')
#ylabel('Height (m)')
#ylabel('Distance along Z-axis (m)')
#legend(loc='best')
#savefig('NO_R_XYProjectile_compare.png')
#### NO AIR RESISTANCE 3D PLOT BLOCK (UNCOMMENT TO USE)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(euler_NR_x, euler_NR_y, euler_NR_z, 'r-x', label='No resistance trajectory Euler, h=0.000041')
ax.plot(RK2_NR_x, RK2_NR_y, RK2_NR_z,'y--', label='No resistance trajectory 2nd Order RK, h=0.000041')
ax.set_xlabel('X (Horizonatal Range (m))')
ax.set_ylabel('Y (Elevation from ground (m))')
ax.set_zlabel('Z (distance along magnus force (m))')
legend(loc='best')
ax.view_init(azim=10)
savefig('No_R_3DProjectile.png')
initial_height=0.0 #For analytical horizontal range R in the absence of air resistance
Analytic_rangeR=analytical_rangeR(v_0, angle_rad, initial_height)
print "Analytically found horizontal range when there is no resistance="
print Analytic_rangeR
if (DO==6):
######## DIFFERENT ANGLES
v_0=45 # initial velocity in ms^-1
b_m=0.149 # Mass of baseball in kg
B1=0.0 # Stoke's drag which is neglected and set to zero for macroscopic objects
C=0.47 # Shape dependent constant (experimentally determined C-values) for sphere (C=1 for with air drag, C=0 for no air drag)
rho_0=1.225 # Density at sea level (approximate)
A=4.5365E-3 # Frontal area (Baseball frontal area in m^2)
d=0.076 # diameter of baseball
B2_m=8.76465E-3 # Calculated by ((0.5*C*rho_0*A)/b_m) where A=((pi*d*d)/4). I dont use this when effect of altitude is considered
h2=0.000041 # step size (timestep) in s
h=h2
N=500000; # number of euler steps (number of timesteps)
T=273 # Temperature in Kelvin
Kb=1.38064852E-23 # Boltzman constant
S0=0.06109 # Taken from textbook (G&N) for baseball. This value here is per kg.
S0_m=4.1E-4 # Taken from textbook (G&N) for baseball.
rpm=30 # revolutions per second
omega=2*pi*rpm # radians per second
Num_angle_iter=25
angles_degree=np.linspace(20,70,Num_angle_iter)
angles_radians=(pi*angles_degree)/180
final_height_RK2=np.zeros(Num_angle_iter)
final_range_RK2=np.zeros(Num_angle_iter)
all_xranges_RK2=np.zeros(Num_angle_iter)
neg_index=np.zeros(Num_angle_iter)
for i in range(0,Num_angle_iter,1):
vx_0=v_0*cos(angles_radians[i])
vy_0=v_0*sin(angles_radians[i])
final_height_RK2[i],final_range_RK2[i],x_new,y_new,z_new,vx_new,vy_new,vz_new,neg_index[i]=RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m)
all_xranges_RK2[i]=final_range_RK2[i]
temp=int(neg_index[i])
# print temp
x_new=x_new[:temp]
y_new=y_new[:temp]
z_new=z_new[:temp]
##### ANGLE DEPENDENCE IN PRESENCE OF OTHER EFFECTS + AIR RESISTANCE PLOT BLOCK (UNCOMMENT TO USE)
plt.plot(x_new, y_new, label=angles_degree[i])
# plt.plot(x_new, z_new, label=angles_degree[i])
# plt.axis([0, 50, 5, -10])
plt.axis([0, 500, 0, 150])
plt.xlabel('X Range (m)')
plt.ylabel('Height (m)')
# plt.ylabel('Distance moved along Z (m)')
# plt.grid(True)
plt.title('PROJECTILE WITH A GIVEN VELOCITY AT DIFFERENT\n ANGLES (DEGREES) XY PLANE (RK2)')
legend(loc='best',prop={'size': 8})
savefig('ALL_EFFECTS_XYRK_Angles_Air_Altitude_Spin_projectile.png')
for i in range(1,Num_angle_iter,1):
index_max_x=argmax(all_xranges_RK2)
Opt_angle= angles_degree[index_max_x]
print "Optimal initial angle (degree) is=",Opt_angle,"for initial velocity (m/s) =", v_0, "\n to get max x range =", all_xranges_RK2[index_max_x]
if (DO==7):
########EFFECT OF DIFFERENT ANGLES IN ABSENCE OF ANY INFLUENCING FACTOR
v_0=45 # initial velocity in ms^-1
b_m=0.149 # Mass of baseball in kg
B1=0.0 # Stoke's drag which is neglected and set to zero for macroscopic objects
C=0.00 # Shape dependent constant (experimentally determined C-values) for sphere (C=1 for with air drag, C=0 for no air drag)
rho_0=1.225 # Density at sea level (approximate)
A=4.5365E-3 # Frontal area (Baseball frontal area in m^2)
d=0.076 # diameter of baseball
B2_m=8.76465E-3 # Calculated by ((0.5*C*rho_0*A)/b_m) where A=((pi*d*d)/4). I dont use this when effect of altitude is considered
h2=0.000041 # step size (timestep) in s
h=h2
N=500000; # number of euler steps (number of timesteps)
T=273 # Temperature in Kelvin
Kb=1.38064852E-23 # Boltzman constant
S0=0.06109 # Taken from textbook (G&N) for baseball. This value here is per kg.
S0_m=0.0
rpm=0.0 # revolutions per second
omega=2*pi*rpm # radians per second
Num_angle_iter=25
angles_degree=np.linspace(20,70,Num_angle_iter)
angles_radians=(pi*angles_degree)/180
final_height_RK2=np.zeros(Num_angle_iter)
final_range_RK2=np.zeros(Num_angle_iter)
all_xranges_RK2=np.zeros(Num_angle_iter)
neg_index=np.zeros(Num_angle_iter)
for i in range(0,Num_angle_iter,1):
vx_0=v_0*cos(angles_radians[i])
vy_0=v_0*sin(angles_radians[i])
final_height_RK2[i],final_range_RK2[i],x_new,y_new,z_new,vx_new,vy_new,vz_new,neg_index[i]=RK2_trajectory(omega, S0, Kb, T, C, A, v_0, vx_0, vy_0, b_m, h, N, rho_0, S0_m)
all_xranges_RK2[i]=final_range_RK2[i]
temp=int(neg_index[i])
# print temp
x_new=x_new[:temp]
y_new=y_new[:temp]
z_new=z_new[:temp]
##### ANGLE DEPENDENCE IN ABSENCE OF OTHER EFFECTS + AIR RESISTANCE PLOT BLOCK (UNCOMMENT TO USE)
plt.plot(x_new, y_new, label=angles_degree[i])
# plt.plot(x_new, z_new, label=angles_degree[i])
# plt.axis([0, 50, 5, -10])
plt.axis([0, 500, 0, 150])
plt.xlabel('X Range (m)')
plt.ylabel('Height (m)')
# plt.ylabel('Distance moved along Z (m)')
# plt.grid(True)
plt.title('PROJECTILE WITH A GIVEN VELOCITY AT DIFFERENT ANGLES\n (DEGREES) IN ABSENCE OF OTHER FACTORS, XY PLANE (RK2)')
legend(loc='best',prop={'size': 8})
savefig('XYRK_Angles_Absence_projectile.png')
for i in range(1,Num_angle_iter,1):
index_max_x=argmax(all_xranges_RK2)
Opt_angle= angles_degree[index_max_x]
print "Optimal initial angle (degree) is=",Opt_angle,"for initial velocity (m/s) =", v_0, "\n to get max x range =", all_xranges_RK2[index_max_x]
vel_comps=np.loadtxt('final_vel_components.dat')
prev_vx_RK=vel_comps[0]
prev_vy_RK=vel_comps[1]
prev_vz_RK=vel_comps[2]
########Finding initial conditions by traversing back to initial conditions from final conditions. Reverse path.
if (DO==8):
b_m=0.149 # Mass of baseball in kg
B1=0.0 # Stoke's drag which is neglected and set to zero for macroscopic objects
C=0.47 # Shape dependent constant (experimentally determined C-values) for sphere (C=1 for with air drag, C=0 for no air drag)
rho_0=1.225 # Density at sea level (approximate)
A=4.5365E-3 # Frontal area (Baseball frontal area in m^2)
d=0.076 # diameter of baseball
B2_m=8.76465E-3 # Calculated by ((0.5*C*rho_0*A)/b_m) where A=((pi*d*d)/4). I dont use this when effect of altitude is considered
h2=0.000041 # step size (timestep) in s
h=h2
N=500000; # number of euler steps (number of timesteps)
T=273 # Temperature in Kelvin
Kb=1.38064852E-23 # Boltzman constant
S0=0.06109 # Taken from textbook (G&N) for baseball. This value here is per kg.
S0_m=4.1E-4 # Taken from textbook (G&N) for baseball.
rpm=30 # revolutions per second
omega=2*pi*rpm # radians per second
print "Final velocity from task 4 (2RK) is used here"
v_fi=sqrt(pow(prev_vx_RK,2)+pow(prev_vy_RK,2)+pow(prev_vz_RK,2))
print "Using previous maximum Horizontal range and maximum z deflection. Final angle is calculated using the Final velocity from task 4 (2RK).\n This is done to show that this function successfully travels from final position to initial position and retriews initial conditions that were input in task 4.\n So run task 4 first and then run this task"
HR=final_range_RK2
# HR=input("Enter the horizontal range=")
ZR=max_z_deflection
# ZR=input("Enter the range in Z direction (negative value because of magnus force acting in negative direction)=")
fi_alpha_ang_rad=acos((prev_vx_RK/v_fi))
# fi_alpha_ang_degree=input("Enter the final angle alpha (angle with +x-axis)=(43 for the above range)")
# fi_alpha_ang_rad=(pi*fi_alpha_ang_degree)/180
fi_beta_ang_rad=acos((prev_vy_RK/v_fi))
# fi_beta_ang_degree=input("Enter the final angle beta (angle with +y-axis (90<beta<180))=(119 for the above range)")
# fi_beta_ang_rad=(pi*fi_beta_ang_degree)/180
fi_gamma_ang_rad=acos((prev_vz_RK/v_fi))
# fi_gamma_ang_degree=input("Enter the final angle beta (angle with +z-axis (90<beta<180))=(119 for the above range)")
# fi_gamma_ang_rad=(pi*fi_gamma_ang_degree)/180
vx_0=v_fi*cos(fi_alpha_ang_rad)
vy_0=v_fi*cos(fi_beta_ang_rad)
vz_0=v_fi*cos(fi_gamma_ang_rad)
# v_fi=input("Enter Final velocity=")
print fi_alpha_ang_rad, fi_beta_ang_rad, fi_gamma_ang_rad
Max_reverse_height_euler,final_reverse_range_euler,x_new_reverse_euler,y_new_reverse_euler,z_new_reverse_euler,vx_new_reverse_euler,vy_new_reverse_euler,vz_new_reverse_euler,euler_neg_index=euler_reverse_trajectory(omega, S0, Kb, T, C, A, v_fi, vx_0, vy_0, vz_0, b_m, h, N, rho_0, S0_m)
x_new_reverse_euler=x_new_reverse_euler[euler_neg_index:]