Module: Joules

Defined in:
lib/joules/waves.rb,
lib/joules/forces.rb,
lib/joules/density.rb,
lib/joules/quantum.rb,
lib/joules/geometry.rb,
lib/joules/pressure.rb,
lib/joules/constants.rb,
lib/joules/conversion.rb,
lib/joules/kinematics.rb,
lib/joules/electricity.rb,
lib/joules/mass_weight.rb,
lib/joules/oscillations.rb,
lib/joules/stress_strain.rb,
lib/joules/thermodynamics.rb,
lib/joules/circular_motion.rb,
lib/joules/electric_fields.rb,
lib/joules/magnetic_fields.rb,
lib/joules/momentum_impulse.rb,
lib/joules/energy_work_power.rb,
lib/joules/gravitational_fields.rb

Overview

Gravitational fields module (gravitational_fields.rb)

Constant Summary collapse

SPEED_OF_LIGHT =
Note:

This quantity is in metres per second.

Speed of light in free space.

3.00e8
FREE_SPACE_PERMEABILITY =
Note:

This quantity is in henries per metre.

Permeability of free space.

(4 * Math::PI * 1e-7)
FREE_SPACE_PERMITTIVITY =
Note:

This quantity is in farads per metre.

Permittivity of free space.

8.85e-12
ELEMENTARY_CHARGE =
Note:

This quantity is in coulombs.

Elementary charge.

1.6e-19
PLANCK_CONSTANT =
Note:

This quantity is in joule seconds.

Planck constant.

6.63e-34
UNIFIED_ATOMIC_MASS_UNIT =
Note:

This quantiy is in kilograms.

Unified atomic mass unit.

1.66e-27
ELECTRON_MASS =
Note:

This quantity is in kilograms.

Rest mass of electron.

9.11e-31
PROTON_MASS =
Note:

This quantity is in kilograms.

Rest mass of proton.

1.67e-27
NEUTRON_MASS =
Note:

This quantity is in kilograms.

Rest mass of neutron.

1.67e-27
MOLAR_GAS_CONSTANT =
Note:

This quantity is in joules per kelvin mole.

Molar gas constant.

8.31
AVOGADRO_CONSTANT =
Note:

This quantity is in per mole.

Avogadro constant.

6.02e23
BOLTZMANN_CONSTANT =
Note:

This quantity is in joules per kelvin.

Boltzmann constant.

1.38e-23
STEFAN_CONSTANT =
Note:

This quantity is in watts per metre squared kelvin to the fourth power.

Stefan constant.

5.67e-8
WIEN_CONSTANT =
Note:

This quantity is in metre kelvins.

Wien constant.

2.9e-3
GRAVITATIONAL_CONSTANT =
Note:

This quantity is in newton metres squared per kilogram squared.

Gravitational constant.

6.67e-11
FREE_FALL_ACCELERATION =
Note:

This quantity is in metres per second squared.

Acceleration of free fall.

9.81

Waves Methods collapse

Forces Methods collapse

Density Methods collapse

Quantum Methods collapse

Arc Length Method collapse

Circumference Method collapse

Area Methods collapse

Volume Methods collapse

Surface Area Methods collapse

Pressure Methods collapse

Angle Conversion Methods collapse

Temperature Conversion Methods collapse

Velocity Conversion Methods collapse

Kinematics Methods collapse

Electricity Methods collapse

Mass and Weight Methods collapse

Oscillations Methods collapse

Stress and Strain Methods collapse

Thermodynamics Methods collapse

Circular Motion Methods collapse

Electric Fields Methods collapse

Magnetic Fields Methods collapse

Momentum and Impulse Methods collapse

Energy, Work, and Power Methods collapse

Gravitational Fields Methods collapse

Class Method Details

.acceleration(initial_velocity, final_velocity, time) ⇒ Float

Calculates the acceleration given initial velocity, final velocity, and time.

Examples:

Joules.acceleration(20, 35, 2.4) #=> 6.25

Parameters:

  • initial_velocity (Int, Float)

    initial_velocity is in metres per second

  • final_velocity (Int, Float)

    final_velocity is in metres per second

  • time (Int, Float)

    time > 0; time is in seconds

Returns:

  • (Float)

    return value is in metres per second squared

Raises:

  • (ZeroDivisionError)

    if time = 0



64
65
66
67
68
69
70
# File 'lib/joules/kinematics.rb', line 64

def acceleration(initial_velocity, final_velocity, time)
  if time.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (final_velocity - initial_velocity) / time.to_f
  end
end

.angular_acceleration(initial_angular_velocity, final_angular_velocity, time) ⇒ Float

Calculates the angular acceleration given initial angular velocity, final angular velocity, and time.

Examples:

Joules.angular_acceleration(20, 35, 2.4) #=> 6.25

Parameters:

  • initial_angular_velocity (Int, Float)

    initial_angular_velocity is in radians per second

  • final_angular_velocity (Int, Float)

    final_angular_velocity is in radians per second

  • time (Int, Float)

    time > 0; time is in seconds

Returns:

  • (Float)

    return value is in radians per second squared

Raises:

  • (ZeroDivisionError)

    if time = 0



59
60
61
62
63
64
65
# File 'lib/joules/circular_motion.rb', line 59

def angular_acceleration(initial_angular_velocity, final_angular_velocity, time)
	if time.zero?
    raise ZeroDivisionError.new('divided by 0')
	else
    return (final_angular_velocity - initial_angular_velocity) / time.to_f
  end
end

.angular_kinetic_energy(moment_of_inertia, angular_velocity) ⇒ Float

Calculates the angular kinetic energy given moment of inertia and angular velocity.

Examples:

Joules.angular_kinetic_energy(5, 2.3) #=> 13.224999999999998

Parameters:

  • moment_of_inertia (Int, Float)

    moment_of_inertia is in kilogram metres squared

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

Returns:

  • (Float)

    return value is in joules



158
159
160
# File 'lib/joules/circular_motion.rb', line 158

def angular_kinetic_energy(moment_of_inertia, angular_velocity)
  return 0.5 * moment_of_inertia * (angular_velocity ** 2)
end

.angular_momentum(moment_of_inertia, angular_velocity) ⇒ Float

Calculates the angular momentum given moment of inertia and angular velocity.

Examples:

Joules.angular_momentum(2, 2.5) #=> 5.0

Parameters:

  • moment_of_inertia (Int, Float)

    moment_of_inertia is in kilogram metres squared

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

Returns:

  • (Float)

    return value is in kilogram metres squared per second



145
146
147
# File 'lib/joules/circular_motion.rb', line 145

def angular_momentum(moment_of_inertia, angular_velocity)
  return moment_of_inertia * angular_velocity.to_f
end

.angular_velocity_v1(linear_velocity, radius) ⇒ Float

Note:

There is one other method for calculating angular velocity.

Calculates the angular velocity given linear velocity and radius.

Examples:

Joules.angular_velocity_v1(9, 3) #=> 3.0

Parameters:

  • linear_velocity (Int, Float)

    linear_velocity is in metres per second

  • radius (Int, Float)

    radius > 0; radius is in metres

Returns:

  • (Float)

    return value is in radians per second

Raises:

  • (ZeroDivisionError)

    if radius = 0



27
28
29
30
31
32
33
# File 'lib/joules/circular_motion.rb', line 27

def angular_velocity_v1(linear_velocity, radius)
	if radius.zero?
    raise ZeroDivisionError.new('divided by 0')
	else 
    return linear_velocity / radius.to_f
  end
end

.angular_velocity_v2(frequency_of_rotation) ⇒ Float

Note:

There is one other method for calculating angular velocity.

Calculates the angular velocity given frequency of rotation.

Examples:

Joules.angular_velocity_v2(1.5) #=> 9.42477796076938

Parameters:

  • frequency_of_rotation (Int, Float)

    frequency_of_rotation >= 0; frequency_of_rotation is in hertz

Returns:

  • (Float)

    return value >= 0; return value is in radians per second



43
44
45
# File 'lib/joules/circular_motion.rb', line 43

def angular_velocity_v2(frequency_of_rotation)
	return 2 * Math::PI * frequency_of_rotation
end

.arc_length(radius, central_angle) ⇒ Float

Calculates the arc length of a circle given radius and central angle.

Examples:

Joules.arc_length(12, (Math::PI/4)) #=> 9.42477796076938

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

  • central_angle (Int, Float)

    central_angle >= 0; central_angle is in radians

Returns:

  • (Float)

    return value >= 0; return value has the same units as radius



25
26
27
# File 'lib/joules/geometry.rb', line 25

def arc_length(radius, central_angle)
  return radius * central_angle.to_f
end

.avg_speed(distance, time) ⇒ Float

Calculates the average speed given distance and time.

Examples:

Joules.avg_speed(30, 2.4) #=> 12.5

Parameters:

  • distance (Int, Float)

    distance >= 0; distance is in metres

  • time (Int, Float)

    time > 0; time is in seconds

Returns:

  • (Float)

    return value >= 0; return value is in metres per second

Raises:

  • (ZeroDivisionError)

    if time = 0



26
27
28
29
30
31
32
# File 'lib/joules/kinematics.rb', line 26

def avg_speed(distance, time)
  if time.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return distance / time.to_f
  end
end

.avg_velocity(displacement, time) ⇒ Float

Calculates the average velocity given displacement and time.

Examples:

Joules.avg_velocity(180, 4.8) #=> 37.5 

Parameters:

  • displacement (Int, Float)

    displacement is in metres

  • time (Int, Float)

    time > 0; time is in seconds

Returns:

  • (Float)

    return value is in metres per second

Raises:

  • (ZeroDivisionError)

    if time = 0



44
45
46
47
48
49
50
# File 'lib/joules/kinematics.rb', line 44

def avg_velocity(displacement, time)
  if time.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return displacement / time.to_f
  end
end

.buoyant_force(density, volume_of_liquid_displaced) ⇒ Float

Calculates the buoyant force given density and volume of liquid displaced.

Examples:

Joules.buoyant_force(1000, 0.00150) #=> 14.715

Parameters:

  • density (Int, Float)

    density >= 0; density is in kilograms per metre cubed

  • volume_of_liquid_displaced (Int, Float)

    volume_of_liquid_displaced >= 0; volume_of_liquid_displaced is in metres cubed

Returns:

  • (Float)

    return value >= 0; return value is in newtons



89
90
91
# File 'lib/joules/forces.rb', line 89

def buoyant_force(density, volume_of_liquid_displaced)
  return density * FREE_FALL_ACCELERATION * volume_of_liquid_displaced
end

.capacitance(charge, voltage) ⇒ Float

Calculates the total capacitance given charge and voltage.

Examples:

Joules.capacitance(2e-3, 100) #=> 2.0e-05

Parameters:

  • charge (Int, Float)

    charge is in coulombs

  • voltage (Int, Float)

    voltage != 0; voltage is in volts

Returns:

  • (Float)

    return value is in farads

Raises:

  • (ZeroDivisionError)

    if voltage = 0



137
138
139
140
141
142
143
# File 'lib/joules/electricity.rb', line 137

def capacitance(charge, voltage)
  if voltage.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return charge / voltage.to_f
  end
end

.capacitance_in_parallel(capacitances) ⇒ Float

Calculates the total capacitance of capacitors in parallel.

Examples:

Joules.capacitance_in_parallel([10, 5, 3.4, 6.3]) #=> 24.7

Parameters:

  • capacitances (Array<Int, Float>)

    each capacitance in capacitances is in farads

Returns:

  • (Float)

    return value is in farads



218
219
220
221
222
223
224
# File 'lib/joules/electricity.rb', line 218

def capacitance_in_parallel(capacitances)
  total_capacitance = 0
  capacitances.each do |capacitance|
    total_capacitance += capacitance
  end
  return total_capacitance.to_f
end

.capacitance_in_series(capacitances) ⇒ Float

Calculates the total capacitance of capacitors in series.

Examples:

Joules.capacitance_in_series([0.5, 0.25, 0.125]) #=> 0.07142857142857142

Parameters:

  • capacitances (Array<Int, Float>)

    each capacitance in resistances != 0; each capacitance in capacitances is in farads

Returns:

  • (Float)

    return value is in farads



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/joules/electricity.rb', line 199

def capacitance_in_series(capacitances)
  total_capacitance = 0
  if capacitances.empty?
    return total_capacitance.to_f
  else
    capacitances.each do |capacitance|
      total_capacitance += (1.0 / capacitance)
    end
    return 1 / total_capacitance
  end
end

.capacitor_potential_energy_v1(charge, voltage) ⇒ Float

Note:

There are two other methods for calculating capacitor potential energy.

Calculates the capacitor potential energy given charge and voltage.

Examples:

Joules.capacitor_potential_energy_v1(1.5, 30) #=> 22.5

Parameters:

  • charge (Int, Float)

    charge is in coulombs

  • voltage (Int, Float)

    voltage is in volts

Returns:

  • (Float)

    return value is in joules



155
156
157
# File 'lib/joules/electricity.rb', line 155

def capacitor_potential_energy_v1(charge, voltage)
  return 0.5 * charge * voltage
end

.capacitor_potential_energy_v2(capacitance, voltage) ⇒ Float

Note:

There are two other methods for calculating capacitor potential energy.

Calculates the capacitor potential energy given capacitance and voltage.

Examples:

Joules.capacitor_potential_energy_v2(10e-6, 20) #=> 0.002

Parameters:

  • capacitance (Int, Float)

    capacitance is in farads

  • voltage (Int, Float)

    voltage is in volts

Returns:

  • (Float)

    return value is in joules



169
170
171
# File 'lib/joules/electricity.rb', line 169

def capacitor_potential_energy_v2(capacitance, voltage)
  return 0.5 * capacitance * (voltage ** 2)
end

.capacitor_potential_energy_v3(charge, capacitance) ⇒ Float

Note:

There are two other methods for calculating capacitor potential energy.

Calculates the capacitor potential energy given charge and capacitance.

Examples:

Joules.capacitor_potential_energy_v3(25, 50) #=> 6.25

Parameters:

  • charge (Int, Float)

    charge is in coulombs

  • capacitance (Int, Float)

    capacitance != 0; capacitance is in farads

Returns:

  • (Float)

    return value is in joules

Raises:

  • (ZeroDivisionError)

    if capacitance = 0



184
185
186
187
188
189
190
# File 'lib/joules/electricity.rb', line 184

def capacitor_potential_energy_v3(charge, capacitance)
  if capacitance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (0.5 * (charge ** 2)) / capacitance
  end
end

.centripetal_acceleration_v1(linear_velocity, radius) ⇒ Float

Note:

There is one other method for calculating centripetal acceleration.

Calculates the centripetal acceleration given linear velocity and radius.

Examples:

Joules.centripetal_acceleration_v1(9, 3) #=> 27.0

Parameters:

  • linear_velocity (Int, Float)

    linear_velocity is in metres per second

  • radius (Int, Float)

    radius > 0; radius is in metres

Returns:

  • (Float)

    return value >= 0; return value is in metres per second squared

Raises:

  • (ZeroDivisionError)

    if radius = 0



78
79
80
81
82
83
84
# File 'lib/joules/circular_motion.rb', line 78

def centripetal_acceleration_v1(linear_velocity, radius)
	if radius.zero?
    raise ZeroDivisionError.new('divided by 0')
	else 
	  return (linear_velocity ** 2.0) / radius
  end
end

.centripetal_acceleration_v2(angular_velocity, radius) ⇒ Float

Note:

There is one other method for calculating centripetal acceleration.

Calculates the centripetal acceleration given angular velocity and radius.

Examples:

Joules.centripetal_acceleration_v2(3, 3) #=> 27.0

Parameters:

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

  • radius (Int, Float)

    radius >= 0; radius is in metres

Returns:

  • (Float)

    return value >= 0; return value is in metres per second squared



96
97
98
# File 'lib/joules/circular_motion.rb', line 96

def centripetal_acceleration_v2(angular_velocity, radius)
	return (angular_velocity ** 2.0) * radius
end

.centripetal_force_v1(mass, linear_velocity, radius) ⇒ Float

Note:

There is one other method for calculating centripetal force.

Calculates the centripetal force given mass, linear velocity, and radius.

Examples:

Joules.centripetal_force_v1(2000, 5.56, 2.1) #=> 29441.523809523802

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • linear_velocity (Int, Float)

    linear_velocity is in metres per second

  • radius (Int, Float)

    radius >= 0; radius is in metres

Returns:

  • (Float)

    return value >= 0; return value is in newtons



112
113
114
115
116
117
118
# File 'lib/joules/circular_motion.rb', line 112

def centripetal_force_v1(mass, linear_velocity, radius)
	if radius.zero?
    raise ZeroDivisionError.new('divided by 0')
	else
    return (mass * (linear_velocity ** 2.0)) / radius
  end 
end

.centripetal_force_v2(mass, angular_velocity, radius) ⇒ Float

Note:

There is one other method for calculating centripetal force.

Calculates the centripetal force given mass, angular velocity, and radius.

Examples:

Joules.centripetal_force_v2(53.5, 3, 3) #=> 1444.5

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

  • radius (Int, Float)

    radius >= 0; radius is in metres

Returns:

  • (Float)

    return value >= 0; return value is in newtons



132
133
134
# File 'lib/joules/circular_motion.rb', line 132

def centripetal_force_v2(mass, angular_velocity, radius)
	return mass * (angular_velocity ** 2.0) * radius
end

.circle_area(radius) ⇒ Float

Calculates the area of a circle given radius.

Examples:

Joules.circle_area(12) #=> 452.38934211693 

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

Returns:

  • (Float)

    return value >= 0; return value has the same units squared as radius



96
97
98
# File 'lib/joules/geometry.rb', line 96

def circle_area(radius)
  return Math::PI * (radius ** 2)
end

.circumference(radius) ⇒ Float

Calculates the circumference of a circle given radius.

Examples:

Joules.circumference(12) #=> 75.398223686155 

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

Returns:

  • (Float)

    return value >= 0; return value has the same units as radius



40
41
42
# File 'lib/joules/geometry.rb', line 40

def circumference(radius)
  return 2 * Math::PI * radius
end

.cone_surface_area(radius, slant_height) ⇒ Float

Calculates the surface area of a cone given radius and slant height.

Examples:

Joules.cone_surface_area(3, 5.83) #=> 83.22078939359362

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

  • slant_height (Int, Float)

    slant_height >= 0; slant_height has the same units as radius

Returns:

  • (Float)

    return value >= 0; return value has the same units squared as radius



165
166
167
# File 'lib/joules/geometry.rb', line 165

def cone_surface_area(radius, slant_height)
  return circle_area(radius) + (Math::PI * radius * slant_height)
end

.cone_volume(radius, height) ⇒ Float

Calculates the volume of a cone given radius and height.

Examples:

Joules.cone_volume(6.5, 3) #=> 132.73228961416876

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

  • height (Int, Float)

    height >= 0; height has the same units as radius

Returns:

  • (Float)

    return value >= 0; return value has the same units cubed as radius



124
125
126
# File 'lib/joules/geometry.rb', line 124

def cone_volume(radius, height)
  return (circle_area(radius) * height) / 3
end

.current_v1(charge, time) ⇒ Float

Note:

There is one other method for calculating current.

Calculates the current given charge and time.

Examples:

Joules.current_v1(325, 5) #=> 65.0

Parameters:

  • charge (Int, Float)

    charge is in coulombs

  • time (Int, Float)

    time > 0; time is in seconds

Returns:

  • (Float)

    return value is in amperes

Raises:

  • (ZeroDivisionError)

    if time = 0



27
28
29
30
31
32
33
# File 'lib/joules/electricity.rb', line 27

def current_v1(charge, time)
  if time.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return charge / time.to_f
  end
end

.current_v2(cross_sectional_area, charge_density, drift_velocity, charge) ⇒ Float

Note:

There is one other method for calculating current.

Calculates the current given cross sectional area, charge density, drift velocity, and charge.

Examples:

Joules.current_v2(0.9, 5e28, 8e-4, 1.6e-19) #=> 5759999.999999999

Parameters:

  • cross_sectional_area (Int, Float)

    cross_sectional_area >= 0; cross_sectional_area is in metres squared

  • charge_density (Int, Float)

    charge_density is in per metres cubed

  • drift_velocity (Int, Float)

    drift_velocity is in metres per second

  • charge (Int, Float)

    charge is in coulombs

Returns:

  • (Float)

    return value is in amperes



49
50
51
# File 'lib/joules/electricity.rb', line 49

def current_v2(cross_sectional_area, charge_density, drift_velocity, charge)
  return cross_sectional_area * charge_density * drift_velocity * charge
end

.cylinder_surface_area(radius, height) ⇒ Float

Calulates the surface area of a cylinder given radius and height.

Examples:

Joules.cylinder_surface_area(6.5, 3) #=> 122.522113490002

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

  • height (Int, Float)

    height >= 0; height has the same units as radius

Returns:

  • (Float)

    return value >= 0; return value has the same units squared as radius



178
179
180
# File 'lib/joules/geometry.rb', line 178

def cylinder_surface_area(radius, height)
  return circumference(radius) * height
end

.cylinder_volume(radius, height) ⇒ Float

Calculates the volume of a cylinder given radius and height.

Examples:

Joules.cylinder_volume(6.5, 3) #=> 398.196868842506 

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

  • height (Int, Float)

    height >= 0; height has the same units as radius

Returns:

  • (Float)

    return value >= 0; return value has the same units cubed as radius



137
138
139
# File 'lib/joules/geometry.rb', line 137

def cylinder_volume(radius, height)
  return circle_area(radius) * height
end

.decay_constant(half_life) ⇒ Float

Calculates the decay constant of a decaying quantity given half-life.

Examples:

Joules.decay_constant(9) #=> 0.0770163533955495

Parameters:

  • half_life (Int, Float)

    half_life != 0; half_life is in seconds

Returns:

  • (Float)

    return value is in per second

Raises:

  • (ZeroDivisionError)

    if half_life = 0



63
64
65
66
67
68
69
# File 'lib/joules/quantum.rb', line 63

def decay_constant(half_life)
  if half_life.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return Math.log(2) / half_life
  end
end

.density(mass, volume) ⇒ Float

Calculates the density given mass and volume.

Examples:

Joules.density(8.96, 0.002) #=> 4480.0

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • volume (Int, Float)

    volume > 0; volume is in metres cubed

Returns:

  • (Float)

    return value >= 0; return value is in kilograms per metre cubed

Raises:

  • (ZeroDivisionError)

    if volume = 0



26
27
28
29
30
31
32
# File 'lib/joules/density.rb', line 26

def density(mass, volume)
  if volume.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return mass / volume.to_f
  end
end

.displacement_v1(initial_velocity, final_velocity, time) ⇒ Float

Note:

There are two other methods for calculating displacement.

Calculates the displacement given initial velocity, final velocity, and time.

Examples:

Joules.displacement_v1(20, 35, 2.4) #=> 66.0

Parameters:

  • initial_velocity (Int, Float)

    initial_velocity is in metres per second

  • final_velocity (Int, Float)

    final_velocity is in metres per second

  • time (Int, Float)

    time >= 0; time is in seconds

Returns:

  • (Float)

    return value is in metres



116
117
118
# File 'lib/joules/kinematics.rb', line 116

def displacement_v1(initial_velocity, final_velocity, time)
  return 0.5 * (initial_velocity + final_velocity) * time
end

.displacement_v2(initial_velocity, acceleration, time) ⇒ Float

Note:

There are two other methods for calculating displacement.

Calculates the displacement given initial velocity, acceleration, and time.

Examples:

Joules.displacement_v2(20, 6.25, 2.4) #=> 66.0

Parameters:

  • initial_velocity (Int, Float)

    initial_velocity is in metres per second

  • acceleration (Int, Float)

    acceleration is in metres per second squared

  • time (Int, Float)

    time >= 0; time is in seconds

Returns:

  • (Float)

    return value is in metres



132
133
134
# File 'lib/joules/kinematics.rb', line 132

def displacement_v2(initial_velocity, acceleration, time)
  return (initial_velocity * time) + (0.5 * acceleration * (time ** 2))
end

.displacement_v3(final_velocity, acceleration, time) ⇒ Float

Note:

There are two other methods for calculating displacement.

Calculates the displacement given final velocity, acceleration, and time.

Examples:

Joules.displacement_v3(35, 6.25, 2.4) #=> 66.0

Parameters:

  • final_velocity (Int, Float)

    final_velocity is in metres per second

  • acceleration (Int, Float)

    acceleration is in metres per second squared

  • time (Int, Float)

    time >= 0; time is in seconds

Returns:

  • (Float)

    return value is in metres



148
149
150
# File 'lib/joules/kinematics.rb', line 148

def displacement_v3(final_velocity, acceleration, time)
  return (final_velocity * time) - (0.5 * acceleration * (time ** 2))
end

.elastic_potential_energy(spring_constant, extension) ⇒ Float

Calculates the elastic potential energy given spring constant and extension.

Examples:

Joules.elastic_potential_energy(81.75, 2.4) #=> 235.44

Parameters:

  • spring_constant (Int, Float)

    spring_constant >= 0; spring_constant is in newtons per metre

  • extension (Int, Float)

    extension >= 0; extension is in metres

Returns:

  • (Float)

    return value >= 0; return value is in joules



38
39
40
# File 'lib/joules/energy_work_power.rb', line 38

def elastic_potential_energy(spring_constant, extension)
  return 0.5 * spring_constant * (extension ** 2)
end

.electric_field_strength_v1(voltage, distance) ⇒ Float

Note:

There are two other method for calculating electric field strength.

Calculates the electric field strength given voltage and distance between two plates.

Examples:

Joules.electric_field_strength_v1(9, 0.1) #=> 90.0

Parameters:

  • voltage (Int, Float)

    voltage is in volts

  • distance (Int, Float)

    distance > 0; distance is in metres

Returns:

  • (Float)

    return value is in newtons per coulomb/volts per metre

Raises:

  • (ZeroDivisionError)

    if distance = 0



27
28
29
30
31
32
33
# File 'lib/joules/electric_fields.rb', line 27

def electric_field_strength_v1(voltage, distance)
  if distance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return voltage / distance.to_f
  end
end

.electric_field_strength_v2(force, charge) ⇒ Float

Note:

There are two other method for calculating electric field strength.

Calculates the electric field strength for an uniform field given force and charge.

Examples:

Joules.electric_field_strength_v2(50, 1.3e-6) #=> 38461538.461538464

Parameters:

  • force (Int, Float)

    force is in newtons

  • charge (Int, Float)

    charge != 0; charge is in coulombs

Returns:

  • (Float)

    return value is in newtons per coulomb/volts per metre

Raises:

  • (ZeroDivisionError)

    if charge = 0



46
47
48
49
50
51
52
# File 'lib/joules/electric_fields.rb', line 46

def electric_field_strength_v2(force, charge)
  if charge.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return force / charge.to_f
  end
end

.electric_field_strength_v3(charge, distance) ⇒ Float

Note:

There are two other method for calculating electric field strength.

Calculates the electric field strength for a radial field given charge and distance.

Examples:

Joules.electric_field_strength_v3(3.2e-19, 0.2) #=> 7.193443755565889e-08

Parameters:

  • charge (Int, Float)

    charge is in coulombs

  • distance (Int, Float)

    distance > 0; distance is in metres

Returns:

  • (Float)

    return value is in newtons per coulomb/volts per metre

Raises:

  • (ZeroDivisionError)

    if distance = 0



65
66
67
68
69
70
71
# File 'lib/joules/electric_fields.rb', line 65

def electric_field_strength_v3(charge, distance)
  if distance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return charge / (4 * Math::PI * FREE_SPACE_PERMITTIVITY * (distance ** 2))
  end
end

.electric_potential(charge, distance) ⇒ Float

Calculates the electric potential given charge and distance.

Examples:

Joules.electric_potential(3.2e-19, 0.2) #=> 1.4386887511131779e-08

Parameters:

  • charge (Int, Float)

    charge is in coulombs

  • distance (Int, Float)

    distance > 0; distance is in metres

Returns:

  • (Float)

    return value is in volts

Raises:

  • (ZeroDivisionError)

    if distance = 0



83
84
85
86
87
88
89
# File 'lib/joules/electric_fields.rb', line 83

def electric_potential(charge, distance)
  if distance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return charge / (4 * Math::PI * FREE_SPACE_PERMITTIVITY * distance)
  end
end

.energy_efficiency(useful_energy_output, energy_input) ⇒ Float

Calculates the energy efficiency given useful energy output and energy input.

Examples:

Joules.energy_efficiency(16, 20) #=> 80.0 

Parameters:

  • useful_energy_output (Int, Float)

    0 <= useful_energy_output <= energy_input; useful_energy_output is in joules

  • energy_input (Int, Float)

    energy_input > 0; energy_input is in joules

Returns:

  • (Float)

    return value >= 0

Raises:

  • (ZeroDivisionError)

    if energy_input = 0



116
117
118
119
120
121
122
# File 'lib/joules/energy_work_power.rb', line 116

def energy_efficiency(useful_energy_output, energy_input)
  if energy_input.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (useful_energy_output / energy_input.to_f) * 100
  end
end

.energy_v1(mass, specific_heat_capacity, temperature_change) ⇒ Float

Note:

There are two other methods for calculating energy.

Calculates the energy given mass, specific heat capacity, and temperature change.

Examples:

Joules.energy_v1(500, 2.46, 3.6) #=> 4428.0

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • specific_heat_capacity (Int, Float)

    specific_heat_capacity >= 0; specific_heat_capacity is in joules per kilogram celcius

  • temperature_change (Int, Float)

    temperature_change is in celcius

Returns:

  • (Float)

    return value is in joules



28
29
30
# File 'lib/joules/thermodynamics.rb', line 28

def energy_v1(mass, specific_heat_capacity, temperature_change)
  return mass * specific_heat_capacity * temperature_change.to_f
end

.energy_v2(mass, specific_latent_heat) ⇒ Float

Note:

There are two other methods for calculating energy.

Calculates the energy given mass and specific latent heat.

Examples:

Joules.energy_v2(84.3, 72.1) #=> 6078.03

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • specific_latent_heat (Int, Float)

    specific_latent_heat >= 0; specific_latent_heat is in joules per kilogram

Returns:

  • (Float)

    return value is in joules



42
43
44
# File 'lib/joules/thermodynamics.rb', line 42

def energy_v2(mass, specific_latent_heat)
  return mass * specific_latent_heat.to_f
end

.energy_v3(voltage, current, time) ⇒ Float

Note:

There are two other methods for calculating energy.

Calculates the energy given voltage, current, and time.

Examples:

Joules.energy_v3(1.8, 0.6, 5) #=> 5.4

Parameters:

  • voltage (Int, Float)

    voltage is in volts

  • current (Int, Float)

    current is in amperes

  • time (Int, Float)

    time >= 0; time is in seconds

Returns:

  • (Float)

    return value is in joules



318
319
320
# File 'lib/joules/electricity.rb', line 318

def energy_v3(voltage, current, time)
  return power_v2(voltage, current) * time
end

.energy_v4(mass) ⇒ Float

Note:

There are three other methods for calculating energy.

Calculates the energy given mass.

Examples:

Joules.energy_v4(60.5) #=> 5.445e+18

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

Returns:

  • (Float)

    return value >= 0; return value is in joules



35
36
37
# File 'lib/joules/quantum.rb', line 35

def energy_v4(mass)
  return mass * (SPEED_OF_LIGHT ** 2)
end

.final_velocity_v1(initial_velocity, acceleration, time) ⇒ Float

Note:

There is one other method for calculating final velocity.

Calculates the final velocity given initial velocity, acceleration, and time.

Examples:

Joules.final_velocity_v1(20, 6.25, 2.4) #=> 35.0

Parameters:

  • initial_velocity (Int, Float)

    initial_velocity is in metres per second

  • acceleration (Int, Float)

    acceleration is in metres per second squared

  • time (Int, Float)

    time >= 0; time is in seconds

Returns:

  • (Float)

    return value is in metres per second



84
85
86
# File 'lib/joules/kinematics.rb', line 84

def final_velocity_v1(initial_velocity, acceleration, time)
  return initial_velocity + (acceleration * time.to_f)
end

.final_velocity_v2(initial_velocity, acceleration, displacement) ⇒ Float

Note:

There is one other method for calculating final velocity.

Calculates the final velocity given initial velocity, acceleration, and displacement.

Examples:

Joules.final_velocity_v2(20, 6.25, 66) #=> 35.0

Parameters:

  • initial_velocity (Int, Float)

    initial_velocity is in metres per second

  • acceleration (Int, Float)

    acceleration is in metres per second squared

  • displacement (Int, Float)

    displacement is in metres

Returns:

  • (Float)

    return value is in metres per second



100
101
102
# File 'lib/joules/kinematics.rb', line 100

def final_velocity_v2(initial_velocity, acceleration, displacement)
  return ((initial_velocity ** 2) + (2 * acceleration * displacement)) ** 0.5
end

.focal_length(object_distance, image_distance) ⇒ Float

Calculates the focal length of a lens given object distance and image distance.

Examples:

Joules.focal_length(45.7, 22.8) #=> 15.21109489051095

Parameters:

  • object_distance (Int, Float)

    object_distance > 0; object_distance is in metres

  • image_distance (Int, Float)

    image_distance > 0; image_distance is in metres

Returns:

  • (Float)

    return value >= 0; return value is in metres

Raises:

  • (ZeroDivisionError)

    if object_distance = 0 or image_distance = 0



163
164
165
166
167
168
169
# File 'lib/joules/waves.rb', line 163

def focal_length(object_distance, image_distance)
  if object_distance.zero? || image_distance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return 1 / ((1.0 / object_distance) + (1.0 / image_distance))
  end
end

.force_v1(mass, acceleration) ⇒ Float

Note:

There are two other methods for calculating force.

Calculates the force given mass and acceleration.

Examples:

Joules.force_v1(120, 2.67) #=> 320.4

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • acceleration (Int, Float)

    acceleration is in metres per second squared

Returns:

  • (Float)

    return value is in newtons



26
27
28
# File 'lib/joules/forces.rb', line 26

def force_v1(mass, acceleration)
  return mass * acceleration.to_f
end

.force_v2(spring_constant, extension) ⇒ Float

Note:

There are two other methods for calculating force.

Calculates the force given spring constant and extension.

Examples:

Joules.force_v2(81.75, 2.4) #=> 196.2

Parameters:

  • spring_constant (Int, Float)

    spring_constant >= 0; spring_constant is in newtons per metre

  • extension (Int, Float)

    extension >= 0; extension is in metres

Returns:

  • (Float)

    return value >= 0; return value is in newtons



40
41
42
# File 'lib/joules/forces.rb', line 40

def force_v2(spring_constant, extension)
  return spring_constant * extension.to_f
end

.force_v3(initial_velocity, final_velocity, mass, time) ⇒ Float

Note:

There are two other methods for calculating force.

Calculates the force given initial velocity, final velocity, mass, and time.

Examples:

Joules.force_v3(20, 35, 50, 2.4) #=> 312.5

Parameters:

  • initial_velocity (Int, Float)

    initial_velocity is in metres per second

  • final_velocity (Int, Float)

    final_velocity is in metres per second

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • time (Int, Float)

    time > 0; time is in seconds

Returns:

  • (Float)

    return value is in newtons

Raises:

  • (ZeroDivisionError)

    if time = 0



59
60
61
62
63
64
65
# File 'lib/joules/forces.rb', line 59

def force_v3(initial_velocity, final_velocity, mass, time)
  if time.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return ((final_velocity - initial_velocity) * mass) / time.to_f
  end
end

.frequency_v1(wave_speed, wavelength) ⇒ Float

Note:

There is one other method for calculating frequency.

Calculates the frequency given wave speed and wavelength.

Examples:

Joules.frequency_v1(325, 0.1) #=> 3250.0

Parameters:

  • wave_speed (Int, Float)

    wave_speed >= 0; wave_speed is in metres per second

  • wavelength (Int, Float)

    wavelength > 0; wavelength is in metres

Returns:

  • (Float)

    return value is in hertz

Raises:

  • (ZeroDivisionError)

    if wavelength = 0



58
59
60
61
62
63
64
# File 'lib/joules/waves.rb', line 58

def frequency_v1(wave_speed, wavelength)
  if wavelength.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return wave_speed / wavelength.to_f
  end
end

.frequency_v2(time_period) ⇒ Float

Note:

There is one other method for calculating frequency.

Calculates the frequency given time period.

Examples:

Joules.frequency_v2(12.5) #=> 0.08

Parameters:

  • time_period (Int, Float)

    time_period > 0; time_period is in seconds

Returns:

  • (Float)

    return value > 0; return value is in hertz

Raises:

  • (ZeroDivisionError)

    if time_period = 0



75
76
77
78
79
80
81
# File 'lib/joules/waves.rb', line 75

def frequency_v2(time_period)
  if time_period.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return 1.0 / time_period
  end
end

.gravitational_field_strength_v1(force, mass) ⇒ Float

Note:

There is one other method for calculating gravitational field strength.

Calculates the gravitational field strength given force and mass.

Examples:

Joules.gravitational_field_strength_v1(20, 0.5) #=> 40.0

Parameters:

  • force (Int, Float)

    force is in newtons

  • mass (Int, Float)

    mass > 0; mass is in kilograms

Returns:

  • (Float)

    return value is in metres per second squared

Raises:

  • (ZeroDivisionError)

    if mass = 0



48
49
50
51
52
53
54
# File 'lib/joules/gravitational_fields.rb', line 48

def gravitational_field_strength_v1(force, mass)
  if mass.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return force / mass.to_f
  end
end

.gravitational_field_strength_v2(mass, distance) ⇒ Float

Note:

There is one other method for calculating gravitational field strength.

Calculates the gravitational field strength given mass and distance.

Examples:

Joules.gravitational_field_strength_v2(34.7, 9.3) #=> 2.6760203491733148e-11

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • distance (Int, Float)

    distance > 0; distance is in metres

Returns:

  • (Float)

    return value >= 0; return value is in metres per second squared

Raises:

  • (ZeroDivisionError)

    if distance = 0



67
68
69
70
71
72
73
# File 'lib/joules/gravitational_fields.rb', line 67

def gravitational_field_strength_v2(mass, distance)
  if distance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (GRAVITATIONAL_CONSTANT * mass) / (distance ** 2)
  end
end

.gravitational_force(object_mass1, object_mass2, distance) ⇒ Float

Calculates the gravitational force given object mass 1, object mass 2, and distance between the centres of the two objects.

Examples:

Joules.gravitational_force(2e30, 1.9e27, 7.8e11) #=> 4.166009204470743e+23

Parameters:

  • object_mass1 (Int, Float)

    object_mass1 >= 0; object_mass1 is in kilograms

  • object_mass2 (Int, Float)

    object_mass2 >= 0; object_mass2 is in kilograms

  • distance (Int, Float)

    distance > 0; distance is in metres

Returns:

  • (Float)

    return value >= 0; return value is in newtons

Raises:

  • (ZeroDivisionError)

    if distance = 0



28
29
30
31
32
33
34
35
# File 'lib/joules/gravitational_fields.rb', line 28

def gravitational_force(object_mass1, object_mass2, distance)
  if distance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (GRAVITATIONAL_CONSTANT * object_mass1 * object_mass2) /
           (distance ** 2)
  end
end

.gravitational_potential(mass, distance) ⇒ Float

Calculates the gravitational potential given mass and distance.

Examples:

Joules.gravitational_potential(6e24, 6.4e6) #=> -62531250.0

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • distance (Int, Float)

    distance > 0; distance is in metres

Returns:

  • (Float)

    return value <= 0; return value is in joules per kilogram

Raises:

  • (ZeroDivisionError)

    if distance = 0



85
86
87
88
89
90
91
# File 'lib/joules/gravitational_fields.rb', line 85

def gravitational_potential(mass, distance)
  if distance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (-GRAVITATIONAL_CONSTANT * mass) / distance
  end
end

.gravitational_potential_energy(mass, height) ⇒ Float

Calculates the gravitational potential energy given mass and height.

Examples:

Joules.gravitational_potential_energy(0.5, 6) #=> 29.43

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • height (Int, Float)

    height >= 0; height is in metres

Returns:

  • (Float)

    return value >= 0; return value is in joules



25
26
27
# File 'lib/joules/energy_work_power.rb', line 25

def gravitational_potential_energy(mass, height)
  return mass * FREE_FALL_ACCELERATION * height
end

.half_life(decay_constant) ⇒ Float

Calculates the half-life of a decaying quantity given decay constant.

Examples:

Joules.half_life(7.7e4) #=> 9.001911435843445e-06

Parameters:

  • decay_constant (Int, Float)

    decay_constant != 0; decay_constant is in per second

Returns:

  • (Float)

    return value is in seconds

Raises:

  • (ZeroDivisionError)

    if decay_constant = 0



47
48
49
50
51
52
53
# File 'lib/joules/quantum.rb', line 47

def half_life(decay_constant)
  if decay_constant.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return Math.log(2) / decay_constant
  end
end

.hydrostatic_pressure(density, height) ⇒ Float

Calculates the hydrostatic pressure given density and height.

Examples:

Joules.hydrostatic_pressure(1000, 5) #=> 49050.0

Parameters:

  • density (Int, Float)

    density >= 0; density is in kilograms per metre cubed

  • height (Int, Float)

    height >= 0; height is in metres

Returns:

  • (Float)

    return value >= 0; return value is in pascals



43
44
45
# File 'lib/joules/pressure.rb', line 43

def hydrostatic_pressure(density, height)
  return density * FREE_FALL_ACCELERATION * height
end

.impulse_v1(force, time) ⇒ Float

Note:

There is one other method for calculating impulse.

Calculates the impulse given force and time.

Examples:

Joules.impulse_v1(30.8, 9.6) #=> 295.68

Parameters:

  • force (Int, Float)

    force is in newtons

  • time (Int, Float)

    time >= 0; time is in seconds

Returns:

  • (Float)

    return value is in newton seconds



39
40
41
# File 'lib/joules/momentum_impulse.rb', line 39

def impulse_v1(force, time)
  return force * time.to_f
end

.impulse_v2(initial_velocity, final_velocity, mass) ⇒ Float

Note:

There is one other method for calculating impulse.

Calculates the impulse given initial velocity, final velocity, and mass.

Examples:

Joules.impulse_v2(20, 35, 2.4) #=> 36.0

Parameters:

  • initial_velocity (Int, Float)

    initial_velocity is in metres per second

  • final_velocity (Int, Float)

    final_velocity is in metres per second

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

Returns:

  • (Float)

    return value is in newton seconds



55
56
57
# File 'lib/joules/momentum_impulse.rb', line 55

def impulse_v2(initial_velocity, final_velocity, mass)
  return (final_velocity - initial_velocity) * mass.to_f
end

.kinetic_energy_v1(mass, velocity) ⇒ Float

Note:

There is one other method for calculating kinetic energy.

Calculates the kinetic energy given mass and velocity.

Examples:

Joules.kinetic_energy_v1(500, 22) #=> 121000.0

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • velocity (Int, Float)

    velocity is in metres per second

Returns:

  • (Float)

    return value >= 0; return value is in joules



52
53
54
# File 'lib/joules/energy_work_power.rb', line 52

def kinetic_energy_v1(mass, velocity)
  return 0.5 * mass * (velocity ** 2)
end

.kinetic_energy_v2(voltage, charge) ⇒ Float

Note:

There is one other method for calculating kinetic energy.

Calculates the kinetic energy of an electron given voltage.

Examples:

Joules.kinetic_energy_v2(20, 2) #=> 2.5e+20

Parameters:

  • voltage (Int, Float)

    voltage is in volts

  • charge (Int, Float)

    charge is in coulombs

Returns:

  • (Float)

    return value is in electronvolts



101
102
103
# File 'lib/joules/electric_fields.rb', line 101

def kinetic_energy_v2(voltage, charge)
  return voltage * (charge / ELEMENTARY_CHARGE)
end

.magnetic_flux(flux_density, area, angle = 0) ⇒ Float

Calculates the magnetic flux given flux density, area, and angle.

Examples:

Joules.magnetic_flux(0.945, 9e-4) #=> 0.0008504999999999999

Parameters:

  • flux_density (Int, Float)

    flux_density is in teslas

  • area (Int, Float)

    area >= 0; area is in metres squared

  • angle (Int, Float) (defaults to: 0)

    angle is in degrees

Returns:

  • (Float)

    return value is in webers



63
64
65
# File 'lib/joules/magnetic_fields.rb', line 63

def magnetic_flux(flux_density, area, angle = 0)
  return flux_density * area * Math.cos(to_radians(angle))
end

.magnetic_flux_linkage(magnetic_flux, number_of_coils) ⇒ Float

Calculates the magnetic flux linkage given magnetic flux and number of coils in the wire.

Examples:

Joules.magnetic_flux_linkage(9.4, 10) #=> 94.0

Parameters:

  • magnetic_flux (Int, Float)

    magnetic_flux is in webers

  • number_of_coils (Int, Float)

    number_of_coils >= 0

Returns:

  • (Float)

    return value is in webers



76
77
78
# File 'lib/joules/magnetic_fields.rb', line 76

def magnetic_flux_linkage(magnetic_flux, number_of_coils)
  return magnetic_flux * number_of_coils.to_f
end

.magnetic_force_v1(flux_density, current, conductor_length, angle = 90) ⇒ Float

Note:

There is one other method for calculating magnetic force.

Calculates the magnetic force on a current given flux density, current, conductor length, and angle.

Examples:

Joules.magnetic_force_v1(0.06, 20, 4.5) #=> 5.3999999999999995

Parameters:

  • flux_density (Int, Float)

    flux_density is in teslas

  • current (Int, Float)

    current is in amperes

  • conductor_length (Int, Float)

    conductor_length >= 0; conductor_length is in metres

  • angle (Int, Float) (defaults to: 90)

    angle is in degrees

Returns:

  • (Float)

    return value is in newtons



30
31
32
# File 'lib/joules/magnetic_fields.rb', line 30

def magnetic_force_v1(flux_density, current, conductor_length, angle = 90)
  return flux_density * current * conductor_length * Math.sin(to_radians(angle))
end

.magnetic_force_v2(flux_density, charge, velocity, angle = 90) ⇒ Float

Note:

There is one other method for calculating magnetic force.

Calculates the magnetic force on a moving charge given flux density, charge, velocity, and angle.

Examples:

Joules.magnetic_force_v2(0.06, 34, 60) #=> 122.4

Parameters:

  • flux_density (Int, Float)

    flux_density is in teslas

  • charge (Int, Float)

    charge is in coulombs

  • velocity (Int, Float)

    velocity is in metres per second

  • angle (Int, Float) (defaults to: 90)

    angle is in degrees

Returns:

  • (Float)

    return value is in newtons



48
49
50
# File 'lib/joules/magnetic_fields.rb', line 48

def magnetic_force_v2(flux_density, charge, velocity, angle = 90)
  return flux_density * charge * velocity * Math.sin(to_radians(angle))
end

.magnification(image_height, object_height) ⇒ Float

Calculates the magnification given image height and object height.

Examples:

Joules.magnification(10, 5) #=> 2.0 

Parameters:

  • image_height (Int, Float)

    image_height >= 0; image_height is in a unit of length

  • object_height (Int, Float)

    object_height > 0; object_height has the same units as image_height

Returns:

  • (Float)

    return value >= 0

Raises:

  • (ZeroDivisionError)

    if object_height = 0



145
146
147
148
149
150
151
# File 'lib/joules/waves.rb', line 145

def magnification(image_height, object_height)
  if object_height.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return image_height / object_height.to_f
  end
end

.mass(weight) ⇒ Float

Calculates the mass given weight.

Examples:

Joules.mass(779.0121) #=> 79.41

Parameters:

  • weight (Int, Float)

    weight >= 0; weight is in newtons

Returns:

  • (Float)

    return value >= 0; return value is in kilograms



34
35
36
# File 'lib/joules/mass_weight.rb', line 34

def mass(weight)
  return weight / FREE_FALL_ACCELERATION
end

.max_particle_acceleration(angular_velocity, amplitude) ⇒ Float

Calculates the maximum acceleration of a particle in oscillation given angular velocity and amplitude.

Examples:

Joules.maximum_acceleration(2.4, 5) #=> 28.799999999999997

Parameters:

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

  • amplitude (Int, Float)

    amplitude >= 0; amplitude is in metres

Returns:

  • (Float)

    return value >= 0; return value is in metres per second squared



91
92
93
# File 'lib/joules/oscillations.rb', line 91

def max_particle_acceleration(angular_velocity, amplitude)
  return (angular_velocity ** 2.0) * amplitude
end

.max_particle_speed(angular_velocity, amplitude) ⇒ Float

Calculates the maximum speed of a particle in oscillation given angular velocity and amplitude.

Examples:

Joules.maximum_speed(2.4, 5) #=> 12.0

Parameters:

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

  • amplitude (Int, Float)

    amplitude >= 0; amplitude is in metres

Returns:

  • (Float)

    return value is in metres per second



78
79
80
# File 'lib/joules/oscillations.rb', line 78

def max_particle_speed(angular_velocity, amplitude)
  return angular_velocity * amplitude.to_f
end

.maximum_friction_force(coefficient_of_friction, normal_force) ⇒ Float

Calculates the maximum friction force given coefficient of friction and normal force.

Examples:

Joules.maximum_friction_force(0.4, 29.43) #=> 11.772

Parameters:

  • coefficient_of_friction (Int, Float)

    coefficient_of_friction >= 0

  • normal_force (Int, Force)

    normal_force is in newtons

Returns:

  • (Float)

    return value is in newtons



76
77
78
# File 'lib/joules/forces.rb', line 76

def maximum_friction_force(coefficient_of_friction, normal_force)
  return coefficient_of_friction * normal_force.to_f
end

.moment(force, distance, angle = 90) ⇒ Float

Calculates the moment given force, distance, and angle.

Examples:

Joules.moment(23, 4.5) #=> 103.5

Parameters:

  • force (Int, Float)

    force is in newtons

  • distance (Int, Float)

    distance >= 0; distance is in metres

  • angle (Int, Float) (defaults to: 90)

    angle is in degrees

Returns:

  • (Float)

    return value is in newton metres



104
105
106
# File 'lib/joules/forces.rb', line 104

def moment(force, distance, angle = 90)
  return force * distance * Math.sin(to_radians(angle))
end

.momentum(mass, velocity) ⇒ Float

Calculates the momentum given mass and velocity.

Examples:

Joules.momentum(52, 4.7) #=> 244.4

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • velocity (Int, Float)

    velocity is in metres per second

Returns:

  • (Float)

    return value is in newton seconds



25
26
27
# File 'lib/joules/momentum_impulse.rb', line 25

def momentum(mass, velocity)
  return mass * velocity.to_f
end

.particle_acceleration(angular_velocity, particle_displacement) ⇒ Float

Note:

There is one other method for calculating acceleration.

Calculates the acceleration of a particle in oscillation given angular velocity and particle displacement.

Examples:

Joules.particle_acceleration(2.4, 3) #=> -17.28

Parameters:

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

  • particle_displacement (Int, Float)

    particle_displacement is in metres

Returns:

  • (Float)

    return value is in metres per second squared



26
27
28
# File 'lib/joules/oscillations.rb', line 26

def particle_acceleration(angular_velocity, particle_displacement)
  return (- (angular_velocity ** 2.0) * displacement)
end

.particle_displacement(amplitude, angular_velocity, time) ⇒ Float

Calculates the displacement of a particle in oscillation given amplitude, angular velocity, and time.

Examples:

Joules.particle_displacement(5, 2.4, 3) #=> 3.041756572661276

Parameters:

  • amplitude (Int, Float)

    amplitude >= 0; amplitude is in metres

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

  • time (Int, Float)

    time >= 0; time is in seconds

Returns:

  • (Float)

    return value is in metres



41
42
43
# File 'lib/joules/oscillations.rb', line 41

def particle_displacement(amplitude, angular_velocity, time)
  return amplitude * Math.cos(angular_velocity * time)
end

.particle_velocity(angular_velocity, amplitude, particle_displacement, return_sign = nil) ⇒ Float+

Calculates the velocity of a particle in oscillation given angular velocity, amplitude, and particle displacement.

Examples:

Joules.particle_velocity(2.4, 5, 3) #=> [9.6, -9.6]

Parameters:

  • angular_velocity (Int, Float)

    angular_velocity is in radians per second

  • amplitude (Int, Float)

    amplitude >= 0; amplitude is in metres

  • particle_displacement (Int, Float)

    particle_displacement is in metres

  • return_sign (String) (defaults to: nil)

    return_sign is either ‘-’ or ‘+’

Returns:

  • (Float, Array<Float>)

    return list has a length of 2; each velocity in return list or return value is in metres per second



58
59
60
61
62
63
64
65
66
67
# File 'lib/joules/oscillations.rb', line 58

def particle_velocity(angular_velocity, amplitude, particle_displacement, return_sign = nil)
  return_value = angular_velocity * (((amplitude ** 2) - (particle_displacement ** 2)) ** 0.5)
  if return_sign == '-'
    return (- return_value)
  elsif return_sign == '+'
    return return_value
  else
    return [return_value, (- return_value)]
  end
end

.photon_energy(frequency) ⇒ Float

Calculates the photon energy given frequency.

Examples:

Joules.photon_energy(509337860780984.75) #=> 3.376910016977929e-19

Parameters:

  • frequency (Int, Float)

    frequency > 0; frequency is in hertz

Returns:

  • (Float)

    return value > 0; return value is in joules



23
24
25
# File 'lib/joules/quantum.rb', line 23

def photon_energy(frequency)
  return PLANCK_CONSTANT * frequency
end

.power_efficiency(useful_power_output, power_input) ⇒ Float

Calculates the power efficiency given useful power output and power input.

Examples:

Joules.power_efficiency(26, 40) #=> 65.0

Parameters:

  • useful_power_output (Int, Float)

    0 <= useful_power_output <= power_input; useful_power_output is in watts

  • power_input (Int, Float)

    power_input > 0; power_input is in watts

Returns:

  • (Float)

    return value >= 0

Raises:

  • (ZeroDivisionError)

    if power_input = 0



134
135
136
137
138
139
140
# File 'lib/joules/energy_work_power.rb', line 134

def power_efficiency(useful_power_output, power_input)
  if power_input.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (useful_power_output / power_input.to_f) * 100
  end
end

.power_of_lens(focal_length) ⇒ Float

Calculates the power of a lens given focal length.

Examples:

Joules.power_of_lens(2) #=> 0.5

Parameters:

  • focal_length (Int, Float)

    focal_length > 0; focal_length is in metres

Returns:

  • (Float)

    return value > 0; return value is in dioptres

Raises:

  • (ZeroDivisionError)

    if focal_length = 0



179
180
181
182
183
184
185
# File 'lib/joules/waves.rb', line 179

def power_of_lens(focal_length)
  if focal_length.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return 1.0 / focal_length
  end
end

.power_v1(work_done, time) ⇒ Float

Note:

There are four other methods for calculating power.

Calculates the power given work done and time.

Examples:

Joules.power_v1(28, 7) #=> 4.0

Parameters:

  • work_done (Int, Float)

    work_done is in joules

  • time (Int, Float)

    time > 0; time is in seconds

Returns:

  • (Float)

    return value is in watts

Raises:

  • (ZeroDivisionError)

    if time = 0



82
83
84
85
86
87
88
# File 'lib/joules/energy_work_power.rb', line 82

def power_v1(work_done, time)
  if time.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return work_done / time.to_f
  end
end

.power_v2(force, velocity, angle = 0) ⇒ Float

Note:

There are four other methods for calculating power.

Calculates the power given force, velocity, and angle.

Examples:

Joules.power_v2(42, 2.3) #=> 96.6

Parameters:

  • force (Int, Float)

    force is in newtons

  • velocity (Int, Float)

    velocity is in meters per second

  • angle (Int, Float) (defaults to: 0)

    angle is in degrees

Returns:

  • (Float)

    return value is in watts



102
103
104
# File 'lib/joules/energy_work_power.rb', line 102

def power_v2(force, velocity, angle = 0)
  return force * velocity * Math.cos(to_radians(angle))
end

.power_v3(voltage, current) ⇒ Float

Note:

There are four other methods for calculating power.

Calculates the power given voltage and current.

Examples:

Joules.power_v3(1.8, 0.6) #=> 1.08

Parameters:

  • voltage (Int, Float)

    voltage is in volts

  • current (Int, Float)

    current is in amperes

Returns:

  • (Float)

    return value is in watts



269
270
271
# File 'lib/joules/electricity.rb', line 269

def power_v3(voltage, current)
  return voltage * current.to_f
end

.power_v4(current, resistance) ⇒ Float

Note:

There are four other methods for calculating power.

Calculates the power given current and resistance.

Examples:

Joules.power_v4(0.6, 3) #=> 1.08

Parameters:

  • current (Int, Float)

    current is in amperes

  • resistance (Int, Float)

    resistance is in ohms

Returns:

  • (Float)

    return value is in watts



283
284
285
# File 'lib/joules/electricity.rb', line 283

def power_v4(current, resistance)
  return (current ** 2.0) * resistance
end

.power_v5(voltage, resistance) ⇒ Float

Note:

There are four other methods for calculating power.

Calculates the power given voltage and resistance.

Examples:

Joules.power_v5(1.8, 3) #=> 1.08

Parameters:

  • voltage (Int, Float)

    voltage is in volts

  • resistance (Int, Float)

    resistance != 0; resistance is in ohms

Returns:

  • (Float)

    return value is in watts

Raises:

  • (ZeroDivisionError)

    if resistance = 0



298
299
300
301
302
303
304
# File 'lib/joules/electricity.rb', line 298

def power_v5(voltage, resistance)
  if resistance.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (voltage ** 2.0) / resistance
  end
end

.pressure(force, area) ⇒ Float

Calculates the pressure given force and area.

Examples:

Joules.pressure(98, 0.04) #=> 2450.0 

Parameters:

  • force (Int, Float)

    force >= 0; force is in newtons

  • area (Int, Float)

    area > 0; area is in metres squared

Returns:

  • (Float)

    return value >= 0; return value is in pascals

Raises:

  • (ZeroDivisionError)

    if area = 0



26
27
28
29
30
31
32
# File 'lib/joules/pressure.rb', line 26

def pressure(force, area)
  if area.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return force / area.to_f
  end
end

.rectangle_area(length, width) ⇒ Float

Calculates the area of a rectangle given length and width.

Examples:

Joules.rectangle_area(2, 3.4) #=> 6.8

Parameters:

  • length (Int, Float)

    length >= 0; length is in a unit of length

  • width (Int, Float)

    width >= 0; width has the same units as length

Returns:

  • (Float)

    return value >= 0; return value has the same units squared as length



85
86
87
# File 'lib/joules/geometry.rb', line 85

def rectangle_area(length, width)
  return length * width.to_f
end

.refractive_index_v1(angle_of_incidence, angle_of_refraction) ⇒ Float

Note:

There is one other method for calculating refractive index.

Calculates the refractive index of a substance given angle of incidence and angle of refraction.

Examples:

Joules.refractive_index_v1(50, 35) #=> 1.3355577296591308

Parameters:

  • angle_of_incidence (Int, Float)

    angle_of_incidence is in degrees

  • angle_of_refraction (Int, Float)

    angle_of_refraction != 0; angle_of_refraction is in degrees

Returns:

  • (Float)

Raises:

  • (ZeroDivisionError)

    if angle_of_refraction = 0



110
111
112
113
114
115
116
117
# File 'lib/joules/waves.rb', line 110

def refractive_index_v1(angle_of_incidence, angle_of_refraction)
  if angle_of_refraction.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return Math.sin(to_radians(angle_of_incidence)) /
           Math.sin(to_radians(angle_of_refraction))
  end
end

.refractive_index_v2(critical_angle) ⇒ Float

Note:

There is one other method for calculating refractive index.

Calculates the refractive index of a substance given critical angle.

Examples:

Joules.refractive_index_v2(48.7535) #=> 1.3299993207924483

Parameters:

  • critical_angle (Int, Float)

    critical_angle != 0; critical_angle is in degrees

Returns:

  • (Float)

Raises:

  • (ZeroDivisionError)

    if critical_angle = 0



127
128
129
130
131
132
133
# File 'lib/joules/waves.rb', line 127

def refractive_index_v2(critical_angle)
  if critical_angle.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return 1.0 / Math.sin(to_radians(critical_angle))
  end
end

.resistance_in_parallel(resistances) ⇒ Float

Calculates the total resistance of resistors in parallel.

Examples:

Joules.resistance_in_parallel([0.5, 0.25, 0.125]) #=> 0.07142857142857142

Parameters:

  • resistances (Array<Int, Float>)

    each resistance in resistances != 0; each resistance in resistances is in ohms

Returns:

  • (Float)

    return value is in ohms



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/joules/electricity.rb', line 115

def resistance_in_parallel(resistances)
  total_resistance = 0
  if resistances.empty?
    return total_resistance.to_f 
  else
    resistances.each do |resistance|
      total_resistance += (1.0 / resistance)
    end
    return 1 / total_resistance
  end
end

.resistance_in_series(resistances) ⇒ Float

Calculates the total resistance of resistors in series.

Examples:

Joules.resistance_in_series([10, 5, 3.4, 6.3]) #=> 24.7

Parameters:

  • resistances (Array<Int, Float>)

    each resistance in resistances is in ohms

Returns:

  • (Float)

    return value is in ohms



100
101
102
103
104
105
106
# File 'lib/joules/electricity.rb', line 100

def resistance_in_series(resistances)
  total_resistance = 0
  resistances.each do |resistance|
    total_resistance += resistance
  end
  return total_resistance.to_f
end

.resistance_v1(voltage, current) ⇒ Float

Note:

There is one other method for calculating resistance.

Calculates the resistance given voltage and current.

Examples:

Joules.resistance_v1(1.8, 0.6) #=> 3.0

Parameters:

  • voltage (Int, Float)

    voltage is in volts

  • current (Int, Float)

    current != 0; current is in amperes

Returns:

  • (Float)

    return value is in ohms

Raises:

  • (ZeroDivisionError)

    if current = 0



64
65
66
67
68
69
70
# File 'lib/joules/electricity.rb', line 64

def resistance_v1(voltage, current)
  if current.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return voltage / current.to_f
  end
end

.resistance_v2(resistivity, wire_length, cross_sectional_area) ⇒ Float

Note:

There is one other method for calculating resistance.

Calculates the resistance given resistivity, wire length, and cross sectional area.

Examples:

Joules.resistance_v2(1e13, 250, 0.4) #=> 6.25e+15

Parameters:

  • resistivity (Int, Float)

    resistivity >= 0; resistivity is in ohm metres

  • wire_length (Int, Float)

    wire_length >= 0; wire_length is in metres

  • cross_sectional_area (Int, Float)

    cross_sectional_area > 0; cross_sectional_area is in metres squared

Returns:

  • (Float)

    return value >= 0; return value is in ohms

Raises:

  • (ZeroDivisionError)

    if cross_sectional_area = 0



85
86
87
88
89
90
91
# File 'lib/joules/electricity.rb', line 85

def resistance_v2(resistivity, wire_length, cross_sectional_area)
  if cross_sectional_area.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return (resistivity * wire_length) / cross_sectional_area.to_f
  end
end

.sphere_surface_area(radius) ⇒ Float

Calculates the surface area of a sphere given radius.

Examples:

Joules.sphere_surface_area(12) #=> 1809.5573684677208

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

Returns:

  • (Float)

    return value >= 0; return value has the same units squared as radius



152
153
154
# File 'lib/joules/geometry.rb', line 152

def sphere_surface_area(radius)
  return 4 * circle_area(radius)
end

.sphere_volume(radius) ⇒ Float

Calculates the volume of a sphere given radius.

Examples:

Joules.sphere_volume(12) #=> 7238.229473870883

Parameters:

  • radius (Int, Float)

    radius >= 0; radius is in a unit of length

Returns:

  • (Float)

    return value >= 0; return value has the same units cubed as radius



111
112
113
# File 'lib/joules/geometry.rb', line 111

def sphere_volume(radius)
  return (4 * circle_area(radius) * radius) / 3
end

.tensile_strain(extension, length) ⇒ Float

Calculates the tensile strain given extension and length.

Examples:

Joules.tensile_strain(2, 10) #=> 0.2

Parameters:

  • extension (Int, Float)

    extension >= 0; extension is in metres

  • length (Int, Float)

    length > 0; length is in metres

Returns:

  • (Float)

    return value >= 0

Raises:

  • (ZeroDivisionError)

    if length = 0



44
45
46
47
48
49
50
# File 'lib/joules/stress_strain.rb', line 44

def tensile_strain(extension, length)
  if length.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return extension / length.to_f
  end
end

.tensile_stress(force, area) ⇒ Float

Calculates the tensile stress given force and area.

Examples:

Joules.tensile_stress(98, 0.04) #=> 2450.0

Parameters:

  • force (Int, Float)

    force >= 0; force is in newtons

  • area (Int, Float)

    area > 0; area is in metres squared

Returns:

  • (Float)

    return value >= 0; return value is in pascals

Raises:

  • (ZeroDivisionError)

    if area = 0



26
27
28
29
30
31
32
# File 'lib/joules/stress_strain.rb', line 26

def tensile_stress(force, area)
  if area.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return force / area.to_f
  end
end

.time_period_v1(frequency) ⇒ Float

Note:

There are two other methods for calculating time period.

Calculates the time period given frequency.

Examples:

Joules.time_period_v1(0.08) #=> 12.5

Parameters:

  • frequency (Int, Float)

    frequency > 0; frequency is in hertz

Returns:

  • (Float)

    return value > 0; return value is in seconds

Raises:

  • (ZeroDivisionError)

    if frequency = 0



92
93
94
95
96
97
98
# File 'lib/joules/waves.rb', line 92

def time_period_v1(frequency)
  if frequency.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return 1.0 / frequency
  end
end

.time_period_v2(mass, spring_constant) ⇒ Float

Note:

There are two other methods for calculating time period.

Calculates the time period of a mass-spring system given mass and spring constant.

Examples:

Joules.time_period_v2(20, 5) #=> 12.566370614359172

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

  • spring_constant (Int, Float)

    spring_constant > 0; spring_constant is in newtons per metre

Returns:

  • (Float)

    return value >= 0; return value is in seconds

Raises:

  • (ZeroDivisionError)

    if spring_constant = 0



106
107
108
109
110
111
112
# File 'lib/joules/oscillations.rb', line 106

def time_period_v2(mass, spring_constant)
  if spring_constant.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return 2 * Math::PI * ((mass / spring_constant.to_f) ** 0.5)
  end
end

.time_period_v3(pendulum_length) ⇒ Float

Note:

There are two other methods for calculating time period.

Calculates the time period of a simple pendulum given pendulum length.

Examples:

Joules.time_period_v3(8.4) #=> 5.814133609631141

Parameters:

  • pendulum_length (Int, Float)

    pendulum_length >= 0; pendulum_length is in metres

Returns:

  • (Float)

    return value >= 0; return value is in seconds



122
123
124
# File 'lib/joules/oscillations.rb', line 122

def time_period_v3(pendulum_length)
  return 2 * Math::PI * ((pendulum_length / FREE_FALL_ACCELERATION) ** 0.5)
end

.to_celcius(temperature) ⇒ Float

Calculates the equivalent temperature in celcius given kelvins.

Examples:

Joules.to_celcius(293.15) #=> 20.0

Parameters:

  • temperature (Int, Float)

    temperature is in kelvins

Returns:

  • (Float)

    return value is in celcius



60
61
62
# File 'lib/joules/conversion.rb', line 60

def to_celcius(temperature)
  return temperature - 273.15
end

.to_degrees(angle) ⇒ Float

Calculates the equivalent angle in degrees given radians.

Examples:

Joules.to_degrees(Math::PI/6) #=> 29.999999999999996

Parameters:

  • angle (Int, Float)

    angle is in radians

Returns:

  • (Float)

    return value is in degrees



23
24
25
# File 'lib/joules/conversion.rb', line 23

def to_degrees(angle)
  return (angle * 180) / Math::PI
end

.to_kelvins(temperature) ⇒ Float

Calculates the equivalent temperature in kelvins given celcius.

Examples:

Joules.to_kelvins(20) #=> 293.15

Parameters:

  • temperature (Int, Float)

    temperature is in celcius

Returns:

  • (Float)

    return value is in kelvins



49
50
51
# File 'lib/joules/conversion.rb', line 49

def to_kelvins(temperature)
  return temperature + 273.15
end

.to_kilometres_per_hour(velocity) ⇒ Float

Calculates the equivalent velocity in kilometres per hour given metres per second.

Examples:

Joules.to_kilometres_per_hour(50) #=> 180.0

Parameters:

  • velocity (Int, Float)

    velocity is in metres per second

Returns:

  • (Float)

    return value is in kilometres per hour



86
87
88
# File 'lib/joules/conversion.rb', line 86

def to_kilometres_per_hour(velocity)
  return (velocity * 3600) / 1000.0
end

.to_metres_per_second(velocity) ⇒ Float

Calculates the equivalent velocity in metres per second given kilometres per hour.

Examples:

Joules.to_metres_per_second(200) #=> 55.55555555555556 

Parameters:

  • velocity (Int, Float)

    velocity is in kilometres per hour

Returns:

  • (Float)

    return value is in metres per second



75
76
77
# File 'lib/joules/conversion.rb', line 75

def to_metres_per_second(velocity)
  return (velocity * 1000) / 3600.0
end

.to_radians(angle) ⇒ Float

Calculates the equivalent angle in radians given degrees.

Examples:

Joules.to_radians(30) #=> 0.5235987755982988

Parameters:

  • angle (Int, Float)

    angle is in degrees

Returns:

  • (Float)

    return value is in radians



34
35
36
# File 'lib/joules/conversion.rb', line 34

def to_radians(angle)
  return (angle * Math::PI) / 180
end

.trapezium_area(top_base, bottom_base, height) ⇒ Float

Calculates the area of a trapezium given top base, bottom base, and height.

Examples:

Joules.trapezium_area(10, 15, 3) #=> 37.5

Parameters:

  • top_base (Int, Float)

    top_base >= 0; top_base is in a unit of length

  • bottom_base (Int, Float)

    bottom_base >= 0; bottom_base has the same units as top_base

  • height (Int, Float)

    height >= 0; height has the same units as top_base

Returns:

  • (Float)

    return value >= 0; return value has the same units squared as top_base



72
73
74
# File 'lib/joules/geometry.rb', line 72

def trapezium_area(top_base, bottom_base, height)
  return 0.5 * (top_base + bottom_base) * height
end

.triangle_area(base, height) ⇒ Float

Calculates the area of a triangle given base and height.

Examples:

Joules.triangle_area(2, 3.4) #=> 3.4

Parameters:

  • base (Int, Float)

    base >= 0; base is in a unit of length

  • height (Int, Float)

    height >= 0; height has the same units as base

Returns:

  • (Float)

    return value >= 0; return value has the same units squared as base



57
58
59
# File 'lib/joules/geometry.rb', line 57

def triangle_area(base, height)
  return 0.5 * base * height
end

.voltage_v1(energy, charge) ⇒ Float

Note:

There is one other method for calculating voltage.

Calculates the voltage given energy and charge.

Examples:

Joules.voltage_v1(1.8, 0.6) #=> 3.0

Parameters:

  • energy (Int, Float)

    energy is in joules

  • charge (Int, Float)

    charge != 0; charge is in coulombs

Returns:

  • (Float)

    return value is in volts

Raises:

  • (ZeroDivisionError)

    if charge = 0



237
238
239
240
241
242
243
# File 'lib/joules/electricity.rb', line 237

def voltage_v1(energy, charge)
  if charge.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return energy / charge.to_f
  end
end

.voltage_v2(current, resistance) ⇒ Float

Note:

There is one other method for calculating voltage.

Calculates the voltage given current and resistance.

Examples:

Joules.voltage_v2(0.6, 3) #=> 1.8

Parameters:

  • current (Int, Float)

    current is in amperes

  • resistance (Int, Float)

    resistance is in ohms

Returns:

  • (Float)

    return value is in volts



255
256
257
# File 'lib/joules/electricity.rb', line 255

def voltage_v2(current, resistance)
  return current * resistance.to_f
end

.wave_speed(frequency, wavelength) ⇒ Float

Calculates the wave speed given frequency and wavelength.

Examples:

Joules.wave_speed(3250, 0.1) #=> 325.0

Parameters:

  • frequency (Int, Float)

    frequency > 0; frequency is in hertz

  • wavelength (Int, Float)

    wavelength >= 0; wavelength is in metres

Returns:

  • (Float)

    return value >= 0; return value is in metres per second



25
26
27
# File 'lib/joules/waves.rb', line 25

def wave_speed(frequency, wavelength)
  return frequency * wavelength.to_f
end

.wavelength(wave_speed, frequency) ⇒ Float

Calculates the wavelength given wave speed and frequency.

Examples:

Joules.wavelength(325, 3250) #=> 0.1

Parameters:

  • wave_speed (Int, Float)

    wave_speed >= 0; wave_speed is in metres per second

  • frequency (Int, Float)

    frequency > 0; frequency is in hertz

Returns:

  • (Float)

    return value is in metres

Raises:

  • (ZeroDivisionError)

    if frequency = 0



39
40
41
42
43
44
45
# File 'lib/joules/waves.rb', line 39

def wavelength(wave_speed, frequency)
  if frequency.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return wave_speed / frequency.to_f
  end
end

.weight(mass) ⇒ Float

Calculates the weight given mass.

Examples:

Joules.weight(79.41) #=> 779.0121

Parameters:

  • mass (Int, Float)

    mass >= 0; mass is in kilograms

Returns:

  • (Float)

    return value >= 0; return value is in newtons



23
24
25
# File 'lib/joules/mass_weight.rb', line 23

def weight(mass)
  return mass * FREE_FALL_ACCELERATION 
end

.work_done(force, displacement, angle = 0) ⇒ Float

Calculates the work done given force, displacement, and angle.

Examples:

Joules.work_done(40, 2.34) #=> 93.6

Parameters:

  • force (Int, Float)

    force is in newtons

  • displacement (Int, Float)

    displacement is in metres

  • angle (Int, Float) (defaults to: 0)

    angle is in degrees

Returns:

  • (Float)

    return value is in joules



67
68
69
# File 'lib/joules/energy_work_power.rb', line 67

def work_done(force, displacement, angle = 0)
  return force * displacement * Math.cos(to_radians(angle))
end

.young_modulus(tensile_stress, tensile_strain) ⇒ Float

Calculates the Young modulus given tensile stress and tensile strain.

Examples:

Joules.young_modulus(2450, 0.2) #=> 12250.0

Parameters:

  • tensile_stress (Int, Float)

    tensile_stress >= 0; tensile_stress is in pascals

  • tensile_strain (Int, Float)

    tensile_strain > 0

Returns:

  • (Float)

    return value >= 0; return value is in pascals

Raises:

  • (ZeroDivisionError)

    if tensile_strain = 0



62
63
64
65
66
67
68
# File 'lib/joules/stress_strain.rb', line 62

def young_modulus(tensile_stress, tensile_strain)
  if tensile_strain.zero?
    raise ZeroDivisionError.new('divided by 0')
  else
    return tensile_stress / tensile_strain.to_f
  end
end