Why is the (second) if statement in the for loop not working?
Show older comments
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
Image Analyst
on 31 Aug 2020
The code doesn't run. In this line:
i_Last(i) = 0;
what is (the badly-named) "i"?
David Goodmanson
on 31 Aug 2020
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.
the cyclist
on 31 Aug 2020
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.
pm96
on 31 Aug 2020
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!