repeat loop different sets of intructions

2 views (last 30 days)
Hello, I would like to do the following in Matlab:
I want to count up to a number, and follow a trajectory, (traj1) and then follow a different one (traj2)
ie. I was thinking of something like that (this is a simple example):
t=0; while t<1000
if t<100
p = 2*x;
else
p = 2*x+10
end
t=t+1; end
Then I would like to plot all these values as a sequence and animate it. What am I doing wrong?
thank you !

Answers (2)

Cedric
Cedric on 2 Aug 2013
Edited: Cedric on 2 Aug 2013
As you know the lower and upper boundaries for t, you could base your approach on a FOR loop. With both WHILE or FOR, you should store values that you compute if you want to use them later, e.g. use a loop index k and use it as an index in p, which would become an array (you would have p(k)= ..).
However, you can do it in a vector way, which would be more efficient and simpler:
p = [2*(0:99), 2*(100:1000)+10] ;
In this solution, two vectors are concatenate: the first 2*(0:99) which corresponds to the first part of your "trajectory" and the second 2*(100:1000)+10 that corresponds to the second part.
If you don't understand, try to develop it step by step with smaller numbers so you can easily display the outcome of computations, e.g.
>> p = 1:5
p =
1 2 3 4 5
>> p = 2*(1:5)
p =
2 4 6 8 10
>> p = [2*(1:5), 100] % Can I concatenating something?
p =
2 4 6 8 10 100
>> p = [2*(1:5), 2*(6:10)] % Yes, so let's try with another array.
p =
2 4 6 8 10 12 14 16 18 20
>> p = [2*(1:5), 2*(6:10)+10]
p =
2 4 6 8 10 22 24 26 28 30
UPDATE based on your 2nd post:
You are in a context of parametric curve where you define x and y based on a parameter t, so you have to generate the two vectors x and y separately. Say you want x=cos(t)+3 for t in [0,2[ and x=t for t in [2,4], and y=sin(t) for t in [0,2[ and y=0.5*t for t in [2,4]: let's take a step of e.g 0.1 on t:
t1 = 0:0.1:1.9 ; % Range 1.
t2 = 2:0.2:4 ; % Range 2.
x = [cos(t1)+3, t2] ;
y = [sin(t1), 0.5*t2] ;
plot(x, y) ;

aredhel_vlsi
aredhel_vlsi on 2 Aug 2013
Edited: aredhel_vlsi on 2 Aug 2013
thank you for your quick reply !
However, I am not sure how this can be applied... Let's say at this interval t=[0,100] I want to draw a circle: p = [cos(t)-x1 -sin(t)-y1];
Then, at t= [100, 200] I want to follow a straight line or anything. Until now I could do it with linspace, but I can't use it to switch trajectories.
How could this be?
t=0;
while t<1000
if t<100
p = cos(t)-18 -sin(t)-16;
m=p
else
p = 10*t+15;
m1=p
end
t=t+1;
end
M=[m m1]
plot (t, M)
Of course the above doesn't work. Any idea? Then after I do this, I will show a dot that follows the total trajectory and plot it.
For one simple circle I could write M= cos(t)-18 -sin(t)-16 and do the following( I found this at the internet) :
figure('DoubleBuffer','on', 'Renderer',RENDERER)
plot(M(:,1), M(:,2), 'Color','b', 'LineWidth',0.5)
grid on, axis([-20 20 -20 20]), axis square
xlabel('x'), ylabel('y'), title('Circle Animation')
set(gca, 'DrawMode','fast')
  5 Comments
aredhel_vlsi
aredhel_vlsi on 6 Aug 2013
Edited: aredhel_vlsi on 6 Aug 2013
Thank you for your help Cedric. Well I was confused, because you follow a line y=0.5t, and I want x=-15. Actually, I have two variables, time and theta. I use theta because they are parametric equations, while you only use t. But I want it to make a circle at time : [t0-t1], and then follow a straight line from [t1-t2]. That's why I put this counter "time", since it is time based. That means I have to define more vectors, right? One for theta, and two vectors for time, to make it switch from move one to move two.
t1 = 0:0.1:1.9 ;
t2 = 2:0.2:4 ;
theta = 0:2*pi:100*pi ;
Then how should it be expressed x=-15 for y belonging to [-15, 15]?
x = [cos(theta)+3, ...] ;
y = [sin(theta), ...] ;
plot(x, y) ;
Thank you for all the help.
Cedric
Cedric on 6 Aug 2013
Edited: Cedric on 6 Aug 2013
Actually you need to generate an array of numbers that will be the parameter. How you call it doesn't matter for MATLAB (but it does for the person who reads the code). The point is that if theta depends on t through some formula, you should create a vector of t and compute theta based on this vector, e.g.
t = 0:0.1:4 ;
theta = t.^2 ;
Now if you want to plot a circle from one angle to another, you just need values for theta (there is no need to make it depend on another parameter t), e.g.
theta = 0:0.1:3*pi/2 ;
And then, whichever solution you chose for defining theta, you can plot the circle, e.g. with a radius of 3:
figure(1) ; clf ; hold on ;
x = 3 * cos(theta) ;
y = 3 * sin(theta) ;
plot(x, y, 'b') ;
Similarly, if you want to build a vertical line x=-15 based on another vector t (that might be relevant in another computation), you can define e.g.
x = -15 * ones(size(t)) ;
y = t ;
but if all you want is to display a line from a starting point to an ending point, basically you just need these two points:
x = [-15, -15] ;
y = [-4, 4] ;
plot(x, y, 'r') ;

Sign in to comment.

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!