how to plot decaying exponential

13 Ansichten (letzte 30 Tage)
Romio
Romio am 6 Mai 2019
Kommentiert: gonzalo Mier am 6 Mai 2019
Hello,
I'd like to plot a decaying exponential function similar to this plot, while being able to vary the spike time and rate of decay as well as the amplitude.
decaying.gif
I tried to use the following script but it does not produce a similar plot
t=0:100;
spike_time = 20;
spike_peak =0.2;
spike_decay =5;
g = zeros(length(t),1);
for k = 1:length(t)
if t(k) <= spike_time
g = 0;
else
g = spike_peak*exp(-(t-spike_time)/spike_decay);
end
end
plot(t,g)

Akzeptierte Antwort

gonzalo Mier
gonzalo Mier am 6 Mai 2019
Congratulation! You have your algorithm almost working, but you are using vector when you should be using scalars (As you have your code implemented. Try with this:
t=0:100;
spike_time = 20;
spike_peak =0.2;
spike_decay =5;
g = zeros(length(t),1);
for k = 1:length(t)
if t(k) <= spike_time
g(k) = 0;
else
g(k) = spike_peak*exp(-(t(k)-spike_time)/spike_decay);
end
end
plot(t,g)
As I said before, good job
  2 Kommentare
Romio
Romio am 6 Mai 2019
Thank you!
gonzalo Mier
gonzalo Mier am 6 Mai 2019
Don't forget to accept the answer if it worked for you ;)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by