Motor and propeller physics basics in Drone Programming - Time & Space Complexity
When programming drone motors and propellers, it is important to understand how the code's running time changes as we control more motors or propellers.
We want to know how the program's work grows when we increase the number of motors or propellers.
Analyze the time complexity of the following code snippet.
for motor in motors:
motor_speed = calculate_speed(motor.thrust, motor.propeller_diameter)
motor.set_speed(motor_speed)
This code sets the speed for each motor based on its thrust and propeller size.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each motor to calculate and set speed.
- How many times: Once for each motor in the list.
As the number of motors increases, the program does more work, one step per motor.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calculations and speed settings |
| 100 | 100 calculations and speed settings |
| 1000 | 1000 calculations and speed settings |
Pattern observation: The work grows directly with the number of motors.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as we add more motors.
[X] Wrong: "The code runs at the same speed no matter how many motors there are."
[OK] Correct: Each motor needs its own calculation, so more motors mean more work and more time.
Understanding how your code scales with more motors shows you can write efficient drone control programs that work well as drones get more complex.
"What if we added nested loops to control pairs of motors together? How would the time complexity change?"
Practice
Solution
Step 1: Understand thrust formula basics
Thrust depends on the square of the motor speed (rpm) and the propeller area.Step 2: Identify which factor increases thrust
Increasing rpm increases thrust because thrust ∝ rpm² x propeller area.Final Answer:
Increasing the motor speed (rpm) -> Option BQuick Check:
Thrust ∝ rpm² [OK]
- Thinking smaller propellers increase thrust
- Confusing power with thrust
- Ignoring rpm's squared effect
Solution
Step 1: Recall Python exponent syntax
Python uses ** for exponentiation, so rpm cubed is rpm ** 3.Step 2: Match formula for power
Power depends on rpm cubed times propeller area, so multiply rpm ** 3 by propeller_area.Final Answer:
power = rpm ** 3 * propeller_area -> Option DQuick Check:
Use ** for power, not ^ or *3 [OK]
- Using ^ instead of ** for exponent
- Multiplying rpm by 3 instead of cubing
- Adding instead of multiplying
rpm = 1000 propeller_area = 0.05 thrust = rpm ** 2 * propeller_area print(round(thrust, 1))
Solution
Step 1: Calculate rpm squared
rpm ** 2 = 1000 * 1000 = 1,000,000.Step 2: Multiply by propeller_area
1,000,000 * 0.05 = 50,000.0.Step 3: Check print rounding
round(50000.0, 1) = 50000.0 and print outputs 50000.0.Final Answer:
50000.0 -> Option AQuick Check:
Thrust = 1000² x 0.05 = 50000.0 [OK]
- Misreading rounding precision
- Calculating rpm squared incorrectly
- Confusing thrust with power
rpm = 1200 propeller_area = 0.04 power = rpm ^ 3 * propeller_area print(power)
Solution
Step 1: Check exponentiation operator
In Python, ^ is bitwise XOR, not exponentiation. Use ** for powers.Step 2: Understand impact on calculation
Using ^ causes wrong calculation or unexpected result, not power of rpm.Final Answer:
Using ^ instead of ** for exponentiation -> Option CQuick Check:
Use ** for powers, not ^ [OK]
- Confusing ^ with **
- Assuming parentheses fix ^ operator
- Ignoring operator precedence
Solution
Step 1: Calculate new rpm ratio squared
Reducing rpm by 20% means new rpm = 0.8 x original rpm. Squared: 0.8² = 0.64.Step 2: Find required propeller area increase
To keep thrust constant, propeller_area x 0.64 = original propeller_area. So, new propeller_area = 1 / 0.64 ≈ 1.5625 times original, or 56.25% increase.Final Answer:
Increase propeller area by 56.25% -> Option AQuick Check:
Thrust constant -> area ∝ 1 / (rpm²) = 1/0.64 = 1.5625 [OK]
- Forgetting to square rpm ratio
- Adding instead of multiplying percentages
- Assuming area stays same
Ready to prove it?
Pass the test and this pattern moves unknown
