joules 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +9 -5
- data/lib/joules/constants.rb +8 -8
- data/lib/joules/conversion.rb +11 -11
- data/lib/joules/density.rb +10 -5
- data/lib/joules/electric_fields.rb +43 -9
- data/lib/joules/electricity.rb +47 -12
- data/lib/joules/energy_work_power.rb +31 -13
- data/lib/joules/forces.rb +13 -8
- data/lib/joules/geometry.rb +9 -9
- data/lib/joules/gravitational_fields.rb +31 -11
- data/lib/joules/kinematics.rb +42 -27
- data/lib/joules/magnetic_fields.rb +78 -0
- data/lib/joules/mass_weight.rb +1 -1
- data/lib/joules/momentum_impulse.rb +4 -4
- data/lib/joules/pressure.rb +10 -5
- data/lib/joules/quantum.rb +13 -3
- data/lib/joules/stress_strain.rb +68 -0
- data/lib/joules/thermodynamics.rb +1 -1
- data/lib/joules/waves.rb +67 -22
- data/lib/joules.rb +3 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73cc9f5843a96d24a698edd34d88f242df38678a
|
4
|
+
data.tar.gz: 76ca90cf9af87effc2f4adbaab64e5e1145c6925
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ac9efd15828c56da8c5268a7cb67de5f71325fee00e971c512cbd9f5963ed245b9f1a0e0285cd6883f24b3b4e12afa0e5ffbcc718d47bf3b1fe446fe360ba53
|
7
|
+
data.tar.gz: 0ac87b0cb54ccdb32d99cd1a53144f43539e6a2c3fcd5c814e634bd0d3f0a2306016e8a65e60377b2e549b8a0e555133c699188b6e3b553117211b066c0207a5
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ gem build joules.gemspec
|
|
15
15
|
|
16
16
|
### Library Installation
|
17
17
|
```Bash
|
18
|
-
gem install joules-0.3.
|
18
|
+
gem install joules-0.3.2.gem
|
19
19
|
```
|
20
20
|
|
21
21
|
### Library Import
|
@@ -26,15 +26,15 @@ require 'joules'
|
|
26
26
|
### Usage
|
27
27
|
#### Simple Method Call
|
28
28
|
```Ruby
|
29
|
-
gravitational_potential_energy(0.5, 6)
|
29
|
+
Joules.gravitational_potential_energy(0.5, 6)
|
30
30
|
```
|
31
31
|
This would return 29.43.
|
32
32
|
#### Method Composition
|
33
33
|
```Ruby
|
34
|
-
power(work_done(40, 2.34), 4)
|
34
|
+
Joules.power(Joules.work_done(40, 2.34), 4)
|
35
35
|
```
|
36
36
|
This would return 23.4.
|
37
|
-
> Note: For documentation on methods and constants, please visit [http://www.rubydoc.info/gems/joules/](http://www.rubydoc.info/gems/joules/).
|
37
|
+
> Note: For documentation on usage of methods and constants, please visit [http://www.rubydoc.info/gems/joules/](http://www.rubydoc.info/gems/joules/).
|
38
38
|
|
39
39
|
### Change Log
|
40
40
|
#### Version 0.0.1
|
@@ -61,7 +61,11 @@ This would return 23.4.
|
|
61
61
|
* Added more geometric and conversion formulas commonly used in Physics
|
62
62
|
|
63
63
|
#### Version 0.3.1
|
64
|
-
* Added Physics formulas in gravitational fields and electric fields
|
64
|
+
* Added Physics formulas in gravitational fields and electric fields
|
65
|
+
|
66
|
+
#### Version 0.3.2
|
67
|
+
* Added Physics formulas in magnetic fields
|
68
|
+
* Added Physics formulas in stress and strain
|
65
69
|
|
66
70
|
### Contribution
|
67
71
|
Please feel free to submit any ideas, questions, or problems by reporting an issue.
|
data/lib/joules/constants.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
# @description: Module for providing constants
|
5
5
|
# @author: Elisha Lai
|
6
|
-
# @version: 0.3.
|
6
|
+
# @version: 0.3.2 03/06/2015
|
7
7
|
#==============================================================================
|
8
8
|
|
9
9
|
# Constants module (constants.rb)
|
@@ -11,15 +11,15 @@
|
|
11
11
|
module Joules
|
12
12
|
|
13
13
|
# Speed of light in free space.
|
14
|
-
# @note This quantity is in
|
14
|
+
# @note This quantity is in metres per second.
|
15
15
|
SPEED_OF_LIGHT = 3.00e8
|
16
16
|
|
17
17
|
# Permeability of free space.
|
18
|
-
# @note This quantity is in henries per
|
19
|
-
FREE_SPACE_PERMEABILITY = 4 * Math::PI * 1e-7
|
18
|
+
# @note This quantity is in henries per metre.
|
19
|
+
FREE_SPACE_PERMEABILITY = (4 * Math::PI * 1e-7)
|
20
20
|
|
21
21
|
# Permittivity of free space.
|
22
|
-
# @note This quantity is in farads per
|
22
|
+
# @note This quantity is in farads per metre.
|
23
23
|
FREE_SPACE_PERMITTIVITY = 8.85e-12
|
24
24
|
|
25
25
|
# Elementary charge.
|
@@ -59,11 +59,11 @@ module Joules
|
|
59
59
|
BOLTZMANN_CONSTANT = 1.38e-23
|
60
60
|
|
61
61
|
# Stefan constant.
|
62
|
-
# @note This quantity is in watts per
|
62
|
+
# @note This quantity is in watts per metre squared kelvin to the fourth power.
|
63
63
|
STEFAN_CONSTANT = 5.67e-8
|
64
64
|
|
65
65
|
# Wien constant.
|
66
|
-
# @note This quantity is in
|
66
|
+
# @note This quantity is in metre kelvins.
|
67
67
|
WIEN_CONSTANT = 2.9e-3
|
68
68
|
|
69
69
|
# Gravitational constant.
|
@@ -71,7 +71,7 @@ module Joules
|
|
71
71
|
GRAVITATIONAL_CONSTANT = 6.67e-11
|
72
72
|
|
73
73
|
# Acceleration of free fall.
|
74
|
-
# @note This quantity is in
|
74
|
+
# @note This quantity is in metres per second squared.
|
75
75
|
FREE_FALL_ACCELERATION = 9.81
|
76
76
|
|
77
77
|
end
|
data/lib/joules/conversion.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
# @description: Module for providing conversion formulas
|
5
5
|
# @author: Elisha Lai
|
6
|
-
# @version: 0.3.
|
6
|
+
# @version: 0.3.2 03/06/2015
|
7
7
|
#==============================================================================
|
8
8
|
|
9
9
|
# Conversion module (conversion.rb)
|
@@ -55,25 +55,25 @@ module Joules
|
|
55
55
|
return temperature - 273.15
|
56
56
|
end
|
57
57
|
|
58
|
-
# Calculates the equivalent velocity in
|
58
|
+
# Calculates the equivalent velocity in metres per second given kilometres per hour.
|
59
59
|
# @param velocity [Int, Float]
|
60
|
-
# velocity is in
|
60
|
+
# velocity is in kilometres per hour
|
61
61
|
# @return [Float]
|
62
|
-
# return value is in
|
62
|
+
# return value is in metres per second
|
63
63
|
# @example
|
64
|
-
# Joules.
|
65
|
-
def
|
64
|
+
# Joules.to_metres_per_second(200) #=> 55.55555555555556
|
65
|
+
def to_metres_per_second(velocity)
|
66
66
|
return (velocity * 1000) / 3600.0
|
67
67
|
end
|
68
68
|
|
69
|
-
# Calculates the equivalent velocity in
|
69
|
+
# Calculates the equivalent velocity in kilometres per hour given metres per second.
|
70
70
|
# @param velocity [Int, Float]
|
71
|
-
# velocity is in
|
71
|
+
# velocity is in metres per second
|
72
72
|
# @return [Float]
|
73
|
-
# return value is in
|
73
|
+
# return value is in kilometres per hour
|
74
74
|
# @example
|
75
|
-
# Joules.
|
76
|
-
def
|
75
|
+
# Joules.to_kilometres_per_hour(50) #=> 180.0
|
76
|
+
def to_kilometres_per_hour(velocity)
|
77
77
|
return (velocity * 3600) / 1000.0
|
78
78
|
end
|
79
79
|
|
data/lib/joules/density.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
# @description: Module for providing density formulas
|
5
5
|
# @author: Elisha Lai
|
6
|
-
# @version: 0.3.
|
6
|
+
# @version: 0.3.2 03/06/2015
|
7
7
|
#==============================================================================
|
8
8
|
|
9
9
|
# Density module (density.rb)
|
@@ -15,13 +15,18 @@ module Joules
|
|
15
15
|
# @param mass [Int, Float]
|
16
16
|
# mass >= 0; mass is in kilograms
|
17
17
|
# @param volume [Int, Float]
|
18
|
-
# volume > 0; volume is in
|
18
|
+
# volume > 0; volume is in metres cubed
|
19
19
|
# @return [Float]
|
20
|
-
# return value >= 0; return value is in kilograms per
|
20
|
+
# return value >= 0; return value is in kilograms per metre cubed
|
21
|
+
# @raise [ZeroDivisionError] if volume = 0
|
21
22
|
# @example
|
22
|
-
# Joules.density(8.96, 0.002) #=> 4480.0
|
23
|
+
# Joules.density(8.96, 0.002) #=> 4480.0
|
23
24
|
def density(mass, volume)
|
24
|
-
|
25
|
+
if volume.zero?
|
26
|
+
raise ZeroDivisionError.new('divided by 0')
|
27
|
+
else
|
28
|
+
return mass / volume.to_f
|
29
|
+
end
|
25
30
|
end
|
26
31
|
|
27
32
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
# @description: Module for providing electric fields formulas
|
5
5
|
# @author: Elisha Lai
|
6
|
-
# @version: 0.3.
|
6
|
+
# @version: 0.3.2 03/06/2015
|
7
7
|
#==============================================================================
|
8
8
|
|
9
9
|
# Electric fields module (electric_fields.rb)
|
@@ -15,55 +15,89 @@ module Joules
|
|
15
15
|
# @param voltage [Int, Float]
|
16
16
|
# voltage is in volts
|
17
17
|
# @param distance [Int, Float]
|
18
|
-
# distance > 0; distance is in
|
18
|
+
# distance > 0; distance is in metres
|
19
19
|
# @return [Float]
|
20
20
|
# return value is in newtons per coulomb/volts per metre
|
21
|
+
# @raise [ZeroDivisionError] if distance = 0
|
21
22
|
# @example
|
22
23
|
# Joules.electric_field_strength_v1(9, 0.1) #=> 90.0
|
23
24
|
# @note There are two other method for calculating electric field strength.
|
24
25
|
def electric_field_strength_v1(voltage, distance)
|
25
|
-
|
26
|
+
if distance.zero?
|
27
|
+
raise ZeroDivisionError.new('divided by 0')
|
28
|
+
else
|
29
|
+
return voltage / distance.to_f
|
30
|
+
end
|
26
31
|
end
|
27
32
|
|
28
33
|
# Calculates the electric field strength for an uniform field given force and charge.
|
29
34
|
# @param force [Int, Float]
|
30
35
|
# force is in newtons
|
31
36
|
# @param charge [Int, Float]
|
32
|
-
# charge is in coulombs
|
37
|
+
# charge != 0; charge is in coulombs
|
33
38
|
# @return [Float]
|
34
39
|
# return value is in newtons per coulomb/volts per metre
|
40
|
+
# @raise [ZeroDivisionError] if charge = 0
|
35
41
|
# @example
|
36
42
|
# Joules.electric_field_strength_v2(50, 1.3e-6) #=> 38461538.461538464
|
37
43
|
# @note There are two other method for calculating electric field strength.
|
38
44
|
def electric_field_strength_v2(force, charge)
|
39
|
-
|
45
|
+
if charge.zero?
|
46
|
+
raise ZeroDivisionError.new('divided by 0')
|
47
|
+
else
|
48
|
+
return force / charge.to_f
|
49
|
+
end
|
40
50
|
end
|
41
51
|
|
42
52
|
# Calculates the electric field strength for a radial field given charge and distance.
|
43
53
|
# @param charge [Int, Float]
|
44
54
|
# charge is in coulombs
|
45
55
|
# @param distance [Int, Float]
|
46
|
-
# distance > 0; distance is in
|
56
|
+
# distance > 0; distance is in metres
|
47
57
|
# @return [Float]
|
48
58
|
# return value is in newtons per coulomb/volts per metre
|
59
|
+
# @raise [ZeroDivisionError] if distance = 0
|
49
60
|
# @example
|
50
61
|
# Joules.electric_field_strength_v3(3.2e-19, 0.2) #=> 7.193443755565889e-08
|
51
62
|
# @note There are two other method for calculating electric field strength.
|
52
63
|
def electric_field_strength_v3(charge, distance)
|
53
|
-
|
64
|
+
if distance.zero?
|
65
|
+
raise ZeroDivisionError.new('divided by 0')
|
66
|
+
else
|
67
|
+
return charge / (4 * Math::PI * FREE_SPACE_PERMITTIVITY * (distance ** 2))
|
68
|
+
end
|
54
69
|
end
|
55
70
|
|
56
71
|
# Calculates the electric potential given charge and distance.
|
57
72
|
# @param charge [Int, Float]
|
58
73
|
# charge is in coulombs
|
59
74
|
# @param distance [Int, Float]
|
60
|
-
# distance > 0; distance is in
|
75
|
+
# distance > 0; distance is in metres
|
61
76
|
# @return [Float]
|
62
77
|
# return value is in volts
|
78
|
+
# @raise [ZeroDivisionError] if distance = 0
|
63
79
|
# @example
|
64
80
|
# Joules.electric_potential(3.2e-19, 0.2) #=> 1.4386887511131779e-08
|
65
81
|
def electric_potential(charge, distance)
|
66
|
-
|
82
|
+
if distance.zero?
|
83
|
+
raise ZeroDivisionError.new('divided by 0')
|
84
|
+
else
|
85
|
+
return charge / (4 * Math::PI * FREE_SPACE_PERMITTIVITY * distance)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Calculates the kinetic energy of an electron given voltage.
|
90
|
+
# @param voltage [Int, Float]
|
91
|
+
# voltage is in volts
|
92
|
+
# @param charge [Int, Float]
|
93
|
+
# charge is in coulombs
|
94
|
+
# @return [Float]
|
95
|
+
# return value is in electronvolts
|
96
|
+
# @example
|
97
|
+
# Joules.kinetic_energy_v2(20, 2) #=> 2.5e+20
|
98
|
+
# @note There is one other method for calculating kinetic energy.
|
99
|
+
def kinetic_energy_v2(voltage, charge)
|
100
|
+
return voltage * (charge / ELEMENTARY_CHARGE)
|
67
101
|
end
|
68
102
|
|
69
103
|
end
|
data/lib/joules/electricity.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
# @description: Module for providing electricity formulas
|
5
5
|
# @author: Elisha Lai
|
6
|
-
# @version: 0.3.
|
6
|
+
# @version: 0.3.2 03/06/2015
|
7
7
|
#==============================================================================
|
8
8
|
|
9
9
|
# Electricity module (electricity.rb)
|
@@ -18,10 +18,15 @@ module Joules
|
|
18
18
|
# time > 0; time is in seconds
|
19
19
|
# @return [Float]
|
20
20
|
# return value is in amperes
|
21
|
+
# @raise [ZeroDivisionError] if time = 0
|
21
22
|
# @example
|
22
23
|
# Joules.current(325, 5) #=> 65.0
|
23
24
|
def current(charge, time)
|
24
|
-
|
25
|
+
if time.zero?
|
26
|
+
raise ZeroDivisionError.new('divided by 0')
|
27
|
+
else
|
28
|
+
return charge / time.to_f
|
29
|
+
end
|
25
30
|
end
|
26
31
|
|
27
32
|
# Calculates the resistance given voltage and current.
|
@@ -31,27 +36,37 @@ module Joules
|
|
31
36
|
# current != 0; current is in amperes
|
32
37
|
# @return [Float]
|
33
38
|
# return value is in ohms
|
39
|
+
# @raise [ZeroDivisionError] if current = 0
|
34
40
|
# @example
|
35
41
|
# Joules.resistance_v1(1.8, 0.6) #=> 3.0
|
36
42
|
# @note There is one other method for calculating resistance.
|
37
43
|
def resistance_v1(voltage, current)
|
38
|
-
|
44
|
+
if current.zero?
|
45
|
+
raise ZeroDivisionError.new('divided by 0')
|
46
|
+
else
|
47
|
+
return voltage / current.to_f
|
48
|
+
end
|
39
49
|
end
|
40
50
|
|
41
51
|
# Calculates the resistance given resistivity, wire length, and cross sectional area.
|
42
52
|
# @param resistivity [Int, Float]
|
43
53
|
# resistivity >= 0; resistivity is in ohm metres
|
44
54
|
# @param wire_length [Int, Float]
|
45
|
-
# wire_length >= 0; wire_length is in
|
55
|
+
# wire_length >= 0; wire_length is in metres
|
46
56
|
# @param cross_sectional_area [Int, Float]
|
47
|
-
# cross_sectional_area > 0; cross_sectional_area is in
|
57
|
+
# cross_sectional_area > 0; cross_sectional_area is in metres squared
|
48
58
|
# @return [Float]
|
49
59
|
# return value >= 0; return value is in ohms
|
60
|
+
# @raise [ZeroDivisionError] if cross_sectional_area = 0
|
50
61
|
# @example
|
51
62
|
# Joules.resistance_v2(1e13, 250, 0.4) #=> 6.25e+15
|
52
63
|
# @note There is one other method for calculating resistance.
|
53
64
|
def resistance_v2(resistivity, wire_length, cross_sectional_area)
|
54
|
-
|
65
|
+
if cross_sectional_area.zero?
|
66
|
+
raise ZeroDivisionError.new('divided by 0')
|
67
|
+
else
|
68
|
+
return (resistivity * wire_length) / cross_sectional_area.to_f
|
69
|
+
end
|
55
70
|
end
|
56
71
|
|
57
72
|
# Calculates the total resistance of resistors in series.
|
@@ -95,10 +110,15 @@ module Joules
|
|
95
110
|
# voltage != 0; voltage is in volts
|
96
111
|
# @return [Float]
|
97
112
|
# return value is in farads
|
113
|
+
# @raise [ZeroDivisionError] if voltage = 0
|
98
114
|
# @example
|
99
115
|
# Joules.capacitance(2e-3, 100) #=> 2.0e-05
|
100
116
|
def capacitance(charge, voltage)
|
101
|
-
|
117
|
+
if voltage.zero?
|
118
|
+
raise ZeroDivisionError.new('divided by 0')
|
119
|
+
else
|
120
|
+
return charge / voltage.to_f
|
121
|
+
end
|
102
122
|
end
|
103
123
|
|
104
124
|
# Calculates the capacitor potential energy given charge and voltage.
|
@@ -133,14 +153,19 @@ module Joules
|
|
133
153
|
# @param charge [Int, Float]
|
134
154
|
# charge is in coulombs
|
135
155
|
# @param capacitance [Int, Float]
|
136
|
-
# capacitance is in farads
|
156
|
+
# capacitance != 0; capacitance is in farads
|
137
157
|
# @return [Float]
|
138
158
|
# return value is in joules
|
159
|
+
# @raise [ZeroDivisionError] if capacitance = 0
|
139
160
|
# @example
|
140
161
|
# Joules.capacitor_potential_energy_v3(25, 50) #=> 6.25
|
141
162
|
# @note There are two other methods for calculating capacitor potential energy.
|
142
163
|
def capacitor_potential_energy_v3(charge, capacitance)
|
143
|
-
|
164
|
+
if capacitance.zero?
|
165
|
+
raise ZeroDivisionError.new('divided by 0')
|
166
|
+
else
|
167
|
+
return (0.5 * (charge ** 2)) / capacitance
|
168
|
+
end
|
144
169
|
end
|
145
170
|
|
146
171
|
# Calculates the total capacitance of capacitors in series.
|
@@ -172,7 +197,7 @@ module Joules
|
|
172
197
|
def capacitance_in_parallel(capacitances)
|
173
198
|
total_capacitance = 0
|
174
199
|
capacitances.each do |capacitance|
|
175
|
-
|
200
|
+
total_capacitance += capacitance
|
176
201
|
end
|
177
202
|
return total_capacitance.to_f
|
178
203
|
end
|
@@ -184,11 +209,16 @@ module Joules
|
|
184
209
|
# charge != 0; charge is in coulombs
|
185
210
|
# @return [Float]
|
186
211
|
# return value is in volts
|
212
|
+
# @raise [ZeroDivisionError] if charge = 0
|
187
213
|
# @example
|
188
214
|
# Joules.voltage_v1(1.8, 0.6) #=> 3.0
|
189
215
|
# @note There is one other method for calculating voltage.
|
190
216
|
def voltage_v1(energy, charge)
|
191
|
-
|
217
|
+
if charge.zero?
|
218
|
+
raise ZeroDivisionError.new('divided by 0')
|
219
|
+
else
|
220
|
+
return energy / charge.to_f
|
221
|
+
end
|
192
222
|
end
|
193
223
|
|
194
224
|
# Calculates the voltage given current and resistance.
|
@@ -240,11 +270,16 @@ module Joules
|
|
240
270
|
# resistance != 0; resistance is in ohms
|
241
271
|
# @return [Float]
|
242
272
|
# return value is in watts
|
273
|
+
# @raise [ZeroDivisionError] if resistance = 0
|
243
274
|
# @example
|
244
275
|
# Joules.power_v5(1.8, 3) #=> 1.08
|
245
276
|
# @note There are four other methods for calculating power.
|
246
277
|
def power_v5(voltage, resistance)
|
247
|
-
|
278
|
+
if resistance.zero?
|
279
|
+
raise ZeroDivisionError.new('divided by 0')
|
280
|
+
else
|
281
|
+
return (voltage ** 2.0) / resistance
|
282
|
+
end
|
248
283
|
end
|
249
284
|
|
250
285
|
# Calculates the energy given voltage, current, and time.
|
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
# @description: Module for providing energy, work, and power formulas
|
5
5
|
# @author: Elisha Lai
|
6
|
-
# @version: 0.3.
|
6
|
+
# @version: 0.3.2 03/06/2015
|
7
7
|
#==============================================================================
|
8
8
|
|
9
9
|
# Energy, work, and power module (energy_work_power.rb)
|
@@ -15,7 +15,7 @@ module Joules
|
|
15
15
|
# @param mass [Int, Float]
|
16
16
|
# mass >= 0; mass is in kilograms
|
17
17
|
# @param height [Int, Float]
|
18
|
-
# height >= 0; height is in
|
18
|
+
# height >= 0; height is in metres
|
19
19
|
# @return [Float]
|
20
20
|
# return value >= 0; return value is in joules
|
21
21
|
# @example
|
@@ -26,9 +26,9 @@ module Joules
|
|
26
26
|
|
27
27
|
# Calculates the elastic potential energy given spring constant and extension.
|
28
28
|
# @param spring_constant [Int, Float]
|
29
|
-
# spring_constant >= 0; spring_constant is in newtons per
|
29
|
+
# spring_constant >= 0; spring_constant is in newtons per metre
|
30
30
|
# @param extension [Int, Float]
|
31
|
-
# extension >= 0; extension is in
|
31
|
+
# extension >= 0; extension is in metres
|
32
32
|
# @return [Float]
|
33
33
|
# return value >= 0; return value is in joules
|
34
34
|
# @example
|
@@ -41,12 +41,13 @@ module Joules
|
|
41
41
|
# @param mass [Int, Float]
|
42
42
|
# mass >= 0; mass is in kilograms
|
43
43
|
# @param velocity [Int, Float]
|
44
|
-
# velocity is in
|
44
|
+
# velocity is in metres per second
|
45
45
|
# @return [Float]
|
46
46
|
# return value >= 0; return value is in joules
|
47
47
|
# @example
|
48
|
-
# Joules.
|
49
|
-
|
48
|
+
# Joules.kinetic_energy_v1(500, 22) #=> 121000.0
|
49
|
+
# @note There is one other method for calculating kinetic energy.
|
50
|
+
def kinetic_energy_v1(mass, velocity)
|
50
51
|
return 0.5 * mass * (velocity ** 2)
|
51
52
|
end
|
52
53
|
|
@@ -54,13 +55,15 @@ module Joules
|
|
54
55
|
# @param force [Int, Float]
|
55
56
|
# force is in newtons
|
56
57
|
# @param displacement [Int, Float]
|
57
|
-
# displacement is in
|
58
|
+
# displacement is in metres
|
59
|
+
# @param angle [Int, Float]
|
60
|
+
# angle is in degrees
|
58
61
|
# @return [Float]
|
59
62
|
# return value is in joules
|
60
63
|
# @example
|
61
64
|
# Joules.work_done(40, 2.34) #=> 93.6
|
62
|
-
def work_done(force, displacement)
|
63
|
-
return force * displacement.to_f
|
65
|
+
def work_done(force, displacement, angle = 0)
|
66
|
+
return force * displacement.to_f * Math.cos(to_radians(angle))
|
64
67
|
end
|
65
68
|
|
66
69
|
# Calculates the power given work done and time.
|
@@ -70,11 +73,16 @@ module Joules
|
|
70
73
|
# time > 0; time is in seconds
|
71
74
|
# @return [Float]
|
72
75
|
# return value is in watts
|
76
|
+
# @raise [ZeroDivisionError] if time = 0
|
73
77
|
# @example
|
74
78
|
# Joules.power_v1(28, 7) #=> 4.0
|
75
79
|
# @note There are four other methods for calculating power.
|
76
80
|
def power_v1(work_done, time)
|
77
|
-
|
81
|
+
if time.zero?
|
82
|
+
raise ZeroDivisionError.new('divided by 0')
|
83
|
+
else
|
84
|
+
return work_done / time.to_f
|
85
|
+
end
|
78
86
|
end
|
79
87
|
|
80
88
|
# Calculates the power given force and velocity.
|
@@ -98,10 +106,15 @@ module Joules
|
|
98
106
|
# energy_input > 0; energy_input is in joules
|
99
107
|
# @return [Float]
|
100
108
|
# return value >= 0
|
109
|
+
# @raise [ZeroDivisionError] if energy_input = 0
|
101
110
|
# @example
|
102
111
|
# Joules.energy_efficiency(16, 20) #=> 80.0
|
103
112
|
def energy_efficiency(useful_energy_output, energy_input)
|
104
|
-
|
113
|
+
if energy_input.zero?
|
114
|
+
raise ZeroDivisionError.new('divided by 0')
|
115
|
+
else
|
116
|
+
return (useful_energy_output / energy_input.to_f) * 100
|
117
|
+
end
|
105
118
|
end
|
106
119
|
|
107
120
|
# Calculates the power efficiency given useful power output and power input.
|
@@ -111,10 +124,15 @@ module Joules
|
|
111
124
|
# power_input > 0; power_input is in watts
|
112
125
|
# @return [Float]
|
113
126
|
# return value >= 0
|
127
|
+
# @raise [ZeroDivisionError] if power_input = 0
|
114
128
|
# @example
|
115
129
|
# Joules.power_efficiency(26, 40) #=> 65.0
|
116
130
|
def power_efficiency(useful_power_output, power_input)
|
117
|
-
|
131
|
+
if power_input.zero?
|
132
|
+
raise ZeroDivisionError.new('divided by 0')
|
133
|
+
else
|
134
|
+
return (useful_power_output / power_input.to_f) * 100
|
135
|
+
end
|
118
136
|
end
|
119
137
|
|
120
138
|
end
|
data/lib/joules/forces.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
#
|
4
4
|
# @description: Module for providing forces formulas
|
5
5
|
# @author: Elisha Lai
|
6
|
-
# @version: 0.3.
|
6
|
+
# @version: 0.3.2 03/06/2015
|
7
7
|
#==============================================================================
|
8
8
|
|
9
9
|
# Forces module (forces.rb)
|
@@ -15,7 +15,7 @@ module Joules
|
|
15
15
|
# @param mass [Int, Float]
|
16
16
|
# mass >= 0; mass is in kilograms
|
17
17
|
# @param acceleration [Int, Float]
|
18
|
-
# acceleration is in
|
18
|
+
# acceleration is in metres per second squared
|
19
19
|
# @return [Float]
|
20
20
|
# return value is in newtons
|
21
21
|
# @example
|
@@ -27,9 +27,9 @@ module Joules
|
|
27
27
|
|
28
28
|
# Calculates the force given spring constant and extension.
|
29
29
|
# @param spring_constant [Int, Float]
|
30
|
-
# spring_constant >= 0; spring_constant is in newtons per
|
30
|
+
# spring_constant >= 0; spring_constant is in newtons per metre
|
31
31
|
# @param extension [Int, Float]
|
32
|
-
# extension >= 0; extension is in
|
32
|
+
# extension >= 0; extension is in metres
|
33
33
|
# @return [Float]
|
34
34
|
# return value >= 0; return value is in newtons
|
35
35
|
# @example
|
@@ -41,27 +41,32 @@ module Joules
|
|
41
41
|
|
42
42
|
# Calculates the force given initial velocity, final velocity, mass, and time.
|
43
43
|
# @param initial_velocity [Int, Float]
|
44
|
-
# initial_velocity is in
|
44
|
+
# initial_velocity is in metres per second
|
45
45
|
# @param final_velocity [Int, Float]
|
46
|
-
# final_velocity is in
|
46
|
+
# final_velocity is in metres per second
|
47
47
|
# @param mass [Int, Float]
|
48
48
|
# mass >= 0; mass is in kilograms
|
49
49
|
# @param time [Int, Float]
|
50
50
|
# time > 0; time is in seconds
|
51
51
|
# @return [Float]
|
52
52
|
# return value is in newtons
|
53
|
+
# @raise [ZeroDivisionError] if time = 0
|
53
54
|
# @example
|
54
55
|
# Joules.force_v3(20, 35, 50, 2.4) #=> 312.5
|
55
56
|
# @note There are two other methods for calculating force.
|
56
57
|
def force_v3(initial_velocity, final_velocity, mass, time)
|
57
|
-
|
58
|
+
if time.zero?
|
59
|
+
raise ZeroDivisionError.new('divided by 0')
|
60
|
+
else
|
61
|
+
return ((final_velocity - initial_velocity) * mass) / time.to_f
|
62
|
+
end
|
58
63
|
end
|
59
64
|
|
60
65
|
# Calculates the moment given force and distance.
|
61
66
|
# @param force [Int, Float]
|
62
67
|
# force is in newtons
|
63
68
|
# @param distance [Int, Float]
|
64
|
-
# distance >= 0; distance is in
|
69
|
+
# distance >= 0; distance is in metres
|
65
70
|
# @return [Float]
|
66
71
|
# return value is in newton metres
|
67
72
|
# @example
|