Handle graphics and animation HELLLP!
1 view (last 30 days)
Show older comments
The plot function plots a line and returns a handle to that line. This handle can be used to get or set the line’s properties after it has been created. Two of a line’s properties are XData and YData, which contain the x- and y-values currently plotted. Write a program that plots the function
x(t) = cos(2*pi*t – θ)
between the limits -1.0 ≤ t ≤ 1.0 and saves the handle of the resulting line. Remember, the smaller the increment, the smoother the plot. The angle θ is initially 0 radians.
Then, re-plot the line over and over with θ=p/10 rad, up to θ=2p rad. Again, the smaller the increment, the smoother the transition. To re-plot the lines, use a for loop to calculate new values for x and update the line’s XData property with the set command.
I have this so far
t = -1:0.1:1;
theta=0;
a = cos(2*pi*t-theta);
So I realize that during the for loop theta is going to increase by pi/10
theta=0;
n=0;
for theta<=2pi;
n=(n-1)+1
theta=n*(pi/10)
a = cos(2*pi*t-theta);
hndl=plot(t,a)
and thats all I have
1 Comment
Walter Roberson
on 21 Nov 2013
n=(n-1)+1 is the same as n=n except if n is 0 and n is an unsigned integer (in which case n-1 is 0-1 which is clipped to 0 in unsigned calculations, and then the +1 would make it 1 instead of leaving n at 0.)
for theta<=2pi; is not valid syntax. "for" is used when you know how many times you are going to loop; to loop until a condition is satisfied use "while"
Answers (1)
Image Analyst
on 21 Nov 2013
Edited: Image Analyst
on 21 Nov 2013
Not too bad. Here, x is y (controlled by YData), and t ix x (controlled by XData). Try
% First do a plot to get hPlot.
for number_of_t_steps = 10 : 10 : 100
t = linspace(-1, 1, number_of_t_steps)
theta = 0; % or pi/10 or 2*pi
xt = cos(2*pi*t - theta);
set(hPlot, 'XDATA', t); % Adjust horizontal axis
drawnow; % Force it to draw immediately.
pause(1); % Wait a second for you to see it.
end
Realize that this only updates the horizontal axis, not the vertical.
0 Comments
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!