Why is the (second) if statement in the for loop not working?

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.

6 Comments

The code doesn't run. In this line:
i_Last(i) = 0;
what is (the badly-named) "i"?
That's a mistake. I'm going to edit the post and delete it.
HI pm96,
the second if statement doesn't work because for positive i_Amp and positive tau_Last which you have, the function
i_Last = i_0+i_Amp*(1-exp(-(t/tau_Last)));
is an increasing function of t. In the second if statement you are checking for a decreasing function. So it always fails.
If you want to reproduce the curve it looks like you will have to cycle between
-A1*exp(-(t/tau_Last)) + b1; % increasing
and
A2*exp(-(t/tau_Last)) + b2; % decreasing
with appropriate A1 A2 b1 b2.
Hi David,
thx for your explanation. Meanwhile i made some changes and i tried to implement your improvements. But got stuck again. Now my signal i_Last just starts to swing after it reaches i_og for the first time.
I see two problems:
First: If you zoom in to the early part of your plot (and plot your data with '.-' to show the individual points), you'll see that your first "correction" (when i==5) to prevent going past the upper bound actually puts the new point below the lower bound.
Second: You always do your conditional tests against the "uncorrected" trajectory. After i==5, every point is above the upper bound, so that is the clause that is entered.
I haven't looked how to correct these flaws.
Hi cyclist,
thx. I actually recognized that and i'm still trying to find a solution for that. So far without success. Hope to find a solution soon or maybe one of you know how to solve that.

Sign in to comment.

 Accepted 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)

3 Comments

I tried to implement your idea with i_Last(i + 1) but i got the error, that i can't plot (t,i_Last) because the vectors of t and i_Last aren't the same length (i_Last is 1x202 and t is 1x201). Tried to fixed that but still not working.
ok, i solved that. it was the wrong condition for the length of the for-loop. it should be T1*SR not T1*SR+1. And SR should be > 10000 so the signal gets more clear. Thx all for your help.
Oh, right, I forgot that I had also adjusted the for loop. Glad it worked out.

Sign in to comment.

More Answers (0)

Categories

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

Asked:

on 31 Aug 2020

Edited:

on 1 Sep 2020

Community Treasure Hunt

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

Start Hunting!