How to make successive for loops?!

Hey everybody;
i want to make 3 successive loops that the next loop start from the end of the previous ....can any one help me through that?!
code is attached

 Accepted Answer

If you compare with my code, you'll notice that the use of i2 and i3 in these lines is not correct.
v2(i2)=70.*w1;
u2(i2)=70.*w12;
x2(i2)= y1(i+1);
y2(i2)=y1(i+1);
v2(i3)=30.*w2;
u2(i3)=30.*w22;
x2(i3)=y1(i2+1);
y2(i3)=y1(i2+1);
Furthermore, I never introduced v2,u2,x2,y2.
Best wishes
Torsten.

1 Comment

Yes i got your point and i fixed out but the problem is that once i entered this lines in the loop it takes to long time (doesn't stop till i force close the program...) and if i remove it out of the loop, it warning out asking (what is i2?))
Noting that: i must introduce these parameters to work as an initial values for the second loop...
v2(i2)=50.*w1;
u2(i2)=50.*w12;
x2(i2)= y1(i);
y2(i2)=y1(i);
Thanks a lot for your continuous help

Sign in to comment.

More Answers (1)

It's basically like this. Adapt as needed:
for k1 = 1 : 4
k1 % Echo to command window
end
for k2 = (k1+1): (k1 + 5);
k2 % Echo to command window
end
for k3 = (k2+1): (k2 + 2);
k3 % Echo to command window
end

10 Comments

Thanks for your answer; i appreciate it...
But i don't know when the iteration will stop, i want it to stop according to if condition y(i)=<0 and then start the new loop,...how?!
can you have a look on the code please, it is attached
Does this help ?
for k1=1:1000
if y(k1) < 0
break;
end
end
for k2=k1+1:1000
...
end
Best wishes
Torsten.
Thanks a lot, would you please be patient to and help me...
i do the code as following but it doesn't work... can you tell me what is the problem
if true
% code
% variable values w(1)= 45; % unit in degree u(1)= 100*cosd(w); v(1)= 100*sind(w); x(1)=0; y(1)=0;
% using Euler method;
% first step size h=0.005; t1=1000; % unit in sec w1= 45; % unit in degree u1(1)= 100*cosd(w1); v1(1)= 100*sind(w1); x1(1)=0; y1(1)=0;
for i=1:t1;
% position of the ball
% y-axi
y1(i+1)=y1(i)+(h*v1(i));
%x-axis
x1(i+1)=x1(i)+(h*u1(i));
% air resistance
z1(i)= ((50*R.^2)/m) *(sqrt(u1(i).^2 + v1(i).^2));
% velocity in x and y
v1(i+1)=v1(i)-(h*(g+(z1(i))*v1(i)));
u1(i+1)=u1(i)-(h*z1(i)*(u1(i)));
if y1(i+1)<0
break
end
%%second solution
for i2=i+1:1000;
v2(i2)=0.7*v1(i+1);
u2(i2)=0.7*u1(i+1);
x2(i2)= y1(i+1);
y2(i2)=y1(i+1);
% y-axi
y2(i2+1)=y2(i2)+(h*v2(i2));
%x-axis
x2(i2+1)=x2(i2)+(h*u2(i2));
% air resistance
z2(i2)= ((50*R.^2)/m) *(sqrt(u2(i2).^2 + v2(i2).^2));
% velocity in x and y
v2(i2+1)=v1(i2)-(h*(g+(z2(i2))*v2(i2)));
u2(i2+1)=u1(i2)-(h*z2(i2)*(u2(i2)));
end
end
end
Torsten
Torsten on 1 Apr 2016
Edited: Torsten on 1 Apr 2016
You seem to be a beginner in using MATLAB and programming.
Aren't you allowed to use existing MATLAB tools to solve your ODEs, e.g. ODE45 ? My impression is that it would be much easier for you to implement the model this way.
Best wishes
Torsten.
yes i know but the assignment is to implement it like that not to use the function :) ....
Really really, i am appreciating your kind help Many thanks :)
Undefined function or variable 'R'.
Error in test3 (line 29)
z1(i)= ((50*R.^2)/m) *(sqrt(u1(i).^2 + v1(i).^2));
Something like that ?
for i=1:t1
% position of the ball
% y-axi
y1(i+1)=y1(i)+(h*v1(i));
%x-axis
x1(i+1)=x1(i)+(h*u1(i));
% air resistance
z1(i)= ((50*R.^2)/m) *(sqrt(u1(i).^2 + v1(i).^2));
% velocity in x and y
v1(i+1)=v1(i)-(h*(g+(z1(i))*v1(i)));
u1(i+1)=u1(i)-(h*z1(i)*(u1(i)));
if y1(i+1)<0
break
end
end
w2=30;
u1(i+1)=0.7*100*cosd(w2);
v1(i+1)=0.7*100*sind(w2);
y1(i+1)=y1(i);
x1(i+1)=x1(i);
for k=i+1:1000
% position of the ball
% y-axi
y1(k+1)=y1(k)+h*v1(k);
%x-axis
x1(k+1)=x1(k)+h*u1(k);
% air resistance
z1(k)=((50*R.^2)/m) *(sqrt(u1(k).^2 + v1(k).^2));
% velocity in x and y
v1(k+1)=v1(k)-(h*(g+(z1(k)*v1(k))));
u1(i+1)=u1(i)-(h*z1(i)*(u1(i)));
end
Best wishes
Torsten.
Torsten, post in your own answer so you can get reputation points if this works and she accepts and votes for your answer. You don't want to miss out on any credit you might get.
Torsten;
Great great thanks for your help and your answer, Many thanks..
your help makes my day :)
Hello Tosten;
Look i wrote the code that way as you guided me to but still error appeared with me could you help me out through that...cod is attached... Thanks

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!