-
Notifications
You must be signed in to change notification settings - Fork 0
/
set 6.py
131 lines (100 loc) · 3.1 KB
/
set 6.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
# -*- coding: utf-8 -*-
"""
(1) Using the following parametric curve, find the area of the space enclosed within the red curve.
Monte Carlo method:
"""
#plot parameterized function
#x = t**5 - 4*t**3
#y = t**2
ptsIn = 0
ptsTotal = 10000000
for i in range(ptsTotal):
randx = random.random()*3 - 1.5
randy = random.random()*4 - 1.5
if randx**2 + (randy - np.sqrt(abs(randx)))**2 <= 2:
ptsIn += 1
parameterizedFunction = (ptsIn/ptsTotal)*(3*4)
"""Problem F-3
(1) Table of values for the next 250 days
Source: https://www.tutorialspoint.com/python_data_science/python_normal_distribution.htm
HW3 Suggested Solutions
"""
import numpy as np
from random import *
from tabulate import tabulate
from scipy.optimize import curve_fit
import math
import matplotlib.pyplot as plt
#np.random.normal asks for the mu and sigma (standard deviation, not the variance)
mu1, sigma1 = -0.005, 0.015 # mean and standard deviation
mu2, sigma2 = 0.010, 0.013
#x is the dow index's change rate
#Dow(t+1) = Dow(t)(1 + x)
#make empty array for dow for the first 60 days
dow = []
#we only need 59 values now
for x in range(60):
#if x equals 0, then a x-1 value does not exist.
if(x == 0):
#add original value
s = np.random.normal(mu1, sigma1,1)
dow.append(1 + s[0])
else:
s = np.random.normal(mu1, sigma1,1)
#add a new value (previous value times 1 + new change rate)
dow.append(dow[x-1] * (1 + s[0]))
#the next 190 days
for x in range(190):
#if x equals 0, then a x-1 value does not exist.
if(x == 0):
s = np.random.normal(mu2, sigma2,1)
dow.append(1 + s[0])
else:
s = np.random.normal(mu2, sigma2,1)
#add a new value (previous value times 1 + new change rate)
dow.append(dow[x-1] * (1 + s[0]))
days = []
for x in range(250):
days.append(x+1)
info = {'Day': days, 'Dow index value': dow}
#print(tabulate(info, headers='keys'))
print(tabulate(info, headers='keys', tablefmt='fancy_grid'))
"""(2) Six even data points from the first 60 days (5th, 15th, 25th, 35th, 45th, 55th) and fit them into the function """
def func(x, D, b):
return D*np.exp(b*x)
dayVals = [4,14,24,34,44,54]
dowVals = [dow[4],dow[14],dow[24],dow[34],dow[44],dow[54]]
param_vals, _ = curve_fit(func, dayVals, dowVals)
a2, b2 = param_vals
print("alpha is", a2)
print("beta is", b2)
"""Problem F-4
IVP Problem
"""
a = 8
v0 = 20
vb = 30
def dydx(x, y, b):
return ((v0 / vb) * (1 - ((x**2)/(a**2))) * (1 / math.cos(b)) + (math.sin(b) / math.cos(b)))
#forwardEuler Method
def forwardEuler(x0, y0, x, h,b):
n = (int)((x - x0)/h)
y = y0
result = [y]
for i in range(n):
y = y + h * dydx(x0,y,b)
x0 += h
result.append(y)
return y, result
x0 = a
y0 = 0
x = 0
n = 10000
h = (x - x0)/n
results = []
for x in range(360):
y_Euler, y_line_Euler = forwardEuler(x0, y0, x, h,x)
results.append(abs(y_Euler))
print('The values for each angle value (absolute value), [0-360), are shown: ' + str(results))
print('The minimum value is: ' + str(min(results)))
print('The angle at this value is: ' + str(results.index(min(results))) + ' degrees which is the optimal value at which the boat travels on the shortest path')