📖 Scenario: You want to create a simple animation in MATLAB that shows a point moving along a sine wave. This is useful for understanding how to update plots dynamically.
🎯 Goal: Build a MATLAB script that animates a point moving along the sine curve from 0 to 2π.
📋 What You'll Learn
Create a vector of x values from 0 to 2π
Create a vector of y values as sine of x
Set up a figure and plot the initial point
Update the point position in a loop to animate movement
Use pause to control animation speed
💡 Why This Matters
🌍 Real World
Animating data points helps visualize changes over time, useful in physics simulations, engineering, and data analysis.
💼 Career
Understanding animation basics in MATLAB is valuable for engineers and scientists who need to present dynamic data clearly.
Progress0 / 4 steps
1
Create the data vectors
Create a vector called x with 100 points linearly spaced between 0 and 2*pi. Then create a vector called y where each element is the sine of the corresponding x value.
MATLAB
Hint
Use linspace to create evenly spaced points and sin to get sine values.
2
Set up the figure and initial plot
Create a figure window and plot the first point of the sine wave using plot with a red circle marker. Store the plot handle in a variable called h.
MATLAB
Hint
Use plot to draw the point and axis to set the viewing window.
3
Animate the point along the sine wave
Use a for loop with variable i from 1 to 100. Inside the loop, update the XData and YData properties of h to the i-th elements of x and y. Use pause(0.05) to slow down the animation.
MATLAB
Hint
Change the XData and YData of the plot handle inside the loop.
4
Display the animation result
Run the script to see the red point move smoothly along the sine wave from left to right.
MATLAB
Hint
Run the whole script in MATLAB. You should see a red dot moving along the sine curve.
Practice
(1/5)
1. What is the main purpose of using drawnow in a MATLAB animation loop?
easy
A. To update the figure window and show the latest plot changes immediately
B. To pause the animation for a fixed time
C. To clear the figure before plotting new data
D. To set the axis limits for the plot
Solution
Step 1: Understand the role of drawnow
drawnow forces MATLAB to update the figure window immediately with any changes made to the plot.
Step 2: Differentiate from other commands
Unlike pause which delays execution, drawnow just refreshes the display without stopping the code.
Final Answer:
To update the figure window and show the latest plot changes immediately -> Option A
Quick Check:
drawnow updates display immediately [OK]
Hint: Remember: drawnow refreshes the plot instantly [OK]
Common Mistakes:
Confusing drawnow with pause
Thinking drawnow clears the plot
Using drawnow to set axis limits
2. Which of the following is the correct syntax to create a simple animation loop in MATLAB?
easy
A. loop i in 1:10 { plot(i, i^2); pause(0.5); drawnow; }
B. for i = 1 to 10; plot(i, i^2); pause(0.5); drawnow; end
C. for i = 1:10; plot(i, i^2); pause(0.5); drawnow; end
D. while i <= 10; plot(i, i^2); pause(0.5); drawnow; i++; end
Solution
Step 1: Check MATLAB loop syntax
MATLAB uses for variable = range syntax, so for i = 1:10 is correct.
Step 2: Verify loop body and commands
The commands plot, pause, and drawnow are correctly placed and separated by semicolons.
Final Answer:
for i = 1:10; plot(i, i^2); pause(0.5); drawnow; end -> Option C
Quick Check:
MATLAB for-loop uses '=' and ':' [OK]
Hint: MATLAB loops use 'for var = range' syntax [OK]
Common Mistakes:
Using 'for i = 1 to 10' which is invalid
Using curly braces {} for loops
Using i++ which is not MATLAB syntax
3. What will be the visible effect of running this MATLAB code?
figure;
axis([0 10 0 100]);
for x = 1:10
y = x^2;
plot(x, y, 'ro');
drawnow;
pause(0.3);
end
medium
A. Only the last red dot at (10,100) is visible
B. All red dots appear at once after the loop finishes
C. The plot window stays blank with no dots
D. Red dots appear one by one at points (1,1), (2,4), ..., (10,100)
Solution
Step 1: Understand the loop plotting behavior
Each iteration plots a red circle at (x, x^2) and calls drawnow to update the figure immediately.
Step 2: Effect of axis limits and pause
Axis limits prevent resizing, and pause(0.3) delays so dots appear one by one.
Final Answer:
Red dots appear one by one at points (1,1), (2,4), ..., (10,100) -> Option D