Sheet 4
Example 1: Free Fall with Air Resistance
Calculate the velocity of an object in free fall considering air resistance. Prepare a function to
compute the velocity as a function of time.
% t: time in seconds
% m: mass of the object in kilograms
% k: spring constant (N/m)
% c: damping coefficient (kg/s)
Example 2: The Motion of a Damped Harmonic Oscillator
For a damped harmonic oscillator (like a spring with friction), the displacement as a function
of time can be computed. Write a function that computes displacement as a function of time.
% t: time in seconds
% L: length of the pendulum in meters
% g: acceleration due to gravity (m/s2)
𝑔
𝜔=√
𝐿
𝜃 = 𝜃𝑜 cos(𝜔𝑡)
Example 3: Simple Pendulum Motion
Write a function to compute the angle of a simple pendulum at any given time, assuming
small-angle approximations (i.e., θ is small enough that sin (θ)≈θ).
% t: time in seconds
% m: mass of the object in kilograms
% cd: drag coefficient in kg/s
% 𝑣𝑖 : initial velocity in m/s
𝑚𝑔 −𝐶𝑑 𝑡 𝑚𝑔
𝑣 = (𝑣𝑖 + )𝑒 𝑚 −
𝐶𝑑 𝐶𝑑
Example 4: Grading System
Ask the user for a score (0–100), and display the grade:
• 90–100: A
• 80–89: B
• 70–79: C
• 60–69: D
• Below 60: F
Example 5: Temperature Check
Write a program to check if the temperature entered by the user is cold, moderate, or hot:
• Below 15°C: Cold
• 15–30°C: Moderate
• Above 30°C: Hot
Example 6: Basic Calculator
Ask the user to enter two numbers and an operator (+, -, *, or /), then perform the calculation.
Example 7: Traffic Light Color (Switch Version)
Ask the user to enter a traffic light color ('Red', 'Yellow', or 'Green'), and display the
corresponding message:
• 'Red': Stop
• 'Yellow': Slow down
• 'Green': Go
Example 8: Month Name (Switch Version)
Write a program that asks the user to enter a number between 1 and 12 (representing a
month), and displays the corresponding month name.
Example 9: Simple Calculator (Switch Version)
Create a simple calculator that asks the user to enter two numbers and an operator (+, -, *, /)
using the switch structure.
Example 10: Number Range Check (Switch Version)
Write a program that checks the range of a number entered by the user:
• If the number is less than 0, display "Negative".
• If the number is between 0 and 10, display "Small".
• If the number is between 10 and 100, display "Medium".
• If the number is greater than 100, display "Large".