repeat loop different sets of intructions
古いコメントを表示
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 !
回答 (2 件)
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
2013 年 8 月 2 日
編集済み: aredhel_vlsi
2013 年 8 月 2 日
5 件のコメント
Cedric
2013 年 8 月 2 日
See my update.
aredhel_vlsi
2013 年 8 月 5 日
編集済み: aredhel_vlsi
2013 年 8 月 5 日
Thank you very much Cedric. It seems it works for me. But let's say I want to draw a circle at point center (-15,-15 ) until the time time=100 and then follow a straight line x=-15 until time =200. How can I make that with your way? I tried this:
ERASEMODE = 'normal';
RENDERER = 'painters';
%circles centers
x1=14;
y1=14;
theta = linspace(0,20*pi,100)';
[D,D2,D3,Du] = start_mode(x1,y1,x2,y2,x3,y3,xu,yu, theta)
figure('DoubleBuffer','on', 'Renderer',RENDERER)
plot(D(:,1), D(:,2), 'Color','b', 'LineWidth',0.5)
hold on
plot(D(:,3), 14*r*sin(theta),'Color','b', 'LineWidth',0.5)
xlabel('x'), ylabel('y'), title('Circle Animation')
set(gca, 'DrawMode','fast')
grid on, axis([-20 20 -20 20]), axis square
%moving object
hPointd = line('XData',D(1,1), 'YData',D(1,2), 'EraseMode',ERASEMODE, ...
'Color','r', 'Marker','.', 'MarkerSize',10);
hPoint = line('XData',D(1,1), 'YData',D(1,2), 'EraseMode',ERASEMODE, ...
'Color','r', 'Marker','o', 'MarkerSize',50, 'LineWidth',1);
Where I have defined a function:
function [D,D2,D3,Du] = start_mode(x1,y1,x2,y2,x3,y3,xu,yu,theta)
time=0;
while time<200
if time<=100
p1 = [cos(theta)-x1 -sin(theta)-y1];
elseif (time>100) && (time<200)
for y=-15:1:20
tra(1:100) = - 15;
end
end
time=time+1;
end
D = [ p1 tra'];
It's plotted now and I get the result I want, I know it's not optimal though. But right now I have difficulties in animating it. I have defined a dot that follows the circle, the problem is how to make it understand that after time >200 it should follow the straight line. Any help please?
Cedric
2013 年 8 月 5 日
"But let's say I want to draw a circle at point center (-15,-15 ) until the time time=100 and then follow a straight line x=-15 until time =200. How can I make that with your way?"
The example that I wrote above follows a circle from time 0 to time 1.9, and then a straight line from time 2 to time 4. In what way is it different from what you want to achieve?
For animation, look for example at the demo proposed by Sean in this thread: http://www.mathworks.com/matlabcentral/answers/83271-pause-n-vs-timer-which-is-better
But before talking about animation, I am not sure that you understood my example, because you partly implement it in your function start_mode in a way that mixes two approaches: you have the following in a loop over time:
if time<=100
p1 = [cos(theta)-x1 -sin(theta)-y1];
elseif (time>100) && (time<200)
for y=-15:1:20
tra(1:100) = - 15;
end
end
In this code,
p1 = [cos(theta)-x1 -sin(theta)-y1];
is a vector operation on all values of theta, which means that in one shot you get the array p1. Doing this in the loop actually redefines 101 times the same p1. Then
tra(1:100) = - 15;
is again a vector operation; in one shot you set all elements of tra from 1 to 100 to -15. Placing this in the FOR loop just repeats it 36 times (without using the value of y). And the whole is repeated 99 times because of the outer WHILE loop.
aredhel_vlsi
2013 年 8 月 6 日
編集済み: aredhel_vlsi
2013 年 8 月 6 日
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.
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') ;
カテゴリ
ヘルプ センター および File Exchange で Animation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!