MATLAB: Is the (second) if statement in the for loop not working

for loopif statement

Hi, after many tries i still not understand why the second if statement inside the for loop isn't working. The picture shows the result of a simulation which should match with the result of my matlab code.

Best Answer

  • I don't know if this is perfectly correct, but it is certainly closer:
    Where you have
    i_Last(i) = Faktor*(i_0+(i_Amp*(1-exp(-(dt/tau_Last)))));
    I instead put
    i_Last(i) = (i_0+Faktor*(i_Amp*(1-exp(-(dt/tau_Last)))));
    i_Last(i+1) = (i_Last(i)+Faktor*(i_Amp*(1-exp(-(dt/tau_Last)))));
    This does two things:
    1. calculates i_Last forward one step, so that the if statement will calculate against the "corrected" trajectory
    2. does not multiply the current value of i_Last by Faktor (because that's just the current position)