I am editing code which was developed to model a mass spring damper system in the time domain using the equations of motion of the system. One damping ratio in particular,cpto, was constant originally but I am trying to vary it according to the if statement below.(adsbygoogle = window.adsbygoogle || []).push({});

if k3*(x2(i)-x1(i))>=0 && k3*(x2(i)-x1(i))<=rel

cpto(i+1)=inf;

elseif k3*(x2(i)-x1(i))<=0

cpto(i)=0;

else cpto(i)=2.1*sqrt(k3*mc);

end

The if statement is inside a larger for loop which determines the output of the system over 10000 iterations. The problem is that x2 and xc are used to determine cpto. For this reason I want to either use past values of x2(i-1) and xc(i-1) to determine a current value of cpto(i) (at the start of the for loop) or else use current values of x2(i) and xc(i) to determine a future value of cpto(i+1) at the end of the loop.

The if statement works if put at the end of the for loop, if it calculates cpto for current values of x2 and xc but the problem is it's too late for the loop to use cpto.

Any ideas why it wont work when I use future values of cpto or why i cant use past values of x2 and xc, they are in use elsewhere in the for loop anyway. Thanks for your time,

Kieran.

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# IF loop within a FOR loop problem

Loading...

Similar Threads for loop within loop |
---|

Mathematica Looping through vectorized functions for a piecewise solution |

Mathematica While Loop in Mathematica |

**Physics Forums | Science Articles, Homework Help, Discussion**