Mark Yourself Question
A MATLAB script scans five recorded package masses in kg. Negative values flag invalid readings. The intended behavior is to consume each visited reading once, add only nonnegative masses, and stop when the accepted total reaches at least 10 kg or all five readings have been consumed.
readings = [-2 4 -1 7 8]; % kg; negative entries are invalid k = 1; total = 0; % kg while (k <= 5) && (total < 10) if readings(k) < 0 continue end total = total + readings(k); k = k + 1; end
Explain why this code does not terminate. Rewrite the loop so that it implements the intended behavior, then determine the final accepted total and the value of k, where k identifies the next unread entry.
Unlock full course notes by signing up
Timing an energy-reduction loop
A MATLAB simulation models an energy-reduction test. Each completed cycle takes 4 s and halves the stored energy. Assume these updates describe the model exactly, with no additional elapsed time.
What is the final value of time? Answer in seconds, to one decimal place.
Tracing separate temperature decisions
A thermal test script assigns a dimensionless inspection score from a scalar temperature reading. Run the following MATLAB code separately for each temperature in the table, starting with score = 0 every time. Enter the final score as an exact integer.
The variable temperature contains the numerical reading in °C.
Skipping and stopping during batch acceptance
A packaging script scans proposed batch masses in kg. A negative entry is an invalid reading and is skipped. An entry above 8 kg stops the scan before that batch is accepted.
What is the final value of acceptedMass?
Tracking component energy through nested loops
An engineering test applies energy pulses to three components. They receive two, three, and four pulses, respectively. The first pulse delivered to each component supplies 6 J; every later pulse supplies 2 J.
The MATLAB script uses nested loops, meaning that one loop lies inside another. Every energy variable below stores a numerical value in joules.
After the entire script finishes, determine componentEnergy in J, totalEnergy in J, and the dimensionless pulseCount. Enter all three values as exact integers.