how to plot multiple decaying exponentials in one plot.

3 views (last 30 days)
I would like to plot multiple decaying exponentials with different parameters (time, decay, peak). I was able to create this code to plot only one decaying exp. at t=20
clc,clear,close all
time = [20];
peak = [5];
decay = [2];
t = 1:100;
X = zeros(length(t),1);
for j = 1:length(t)
for i = 1:length(time)
if t(j) > time(i)
X(j) = peak(i)*exp(-(t(j)-time(i))/decay(i));
end
end
end
plot(t,X)
now say I have arrays of parameters like this
time = [20 50 80];
peak = [5 3 2];
decay = [2 4 6];
and I want to plot three consecutive decaying exponentials using each element of these parameters. Any thoughts on how to go about it?
Thanks,

Answers (1)

Alex Mcaulley
Alex Mcaulley on 20 May 2019
Here two options:
time = [20 50 80];
peak = [5 3 2];
decay = [2 4 6];
% Symbolic toolbox needed
figure
syms t
X = peak.*exp(-(t-time)./decay);
fplot(X,[1,100])
%Anonymous functions
figure
t = 1:100;
X = @(t) peak.*exp(-(t-time)./decay);
plot(t,cell2mat(arrayfun(X,1:100,'UniformOutput',false)'))
  2 Comments
Romio
Romio on 20 May 2019
Thanks. But they do not correspond to the parameters (e.g. they should be at 20 50 80 with the spcified amplitudes/decay). Also I need them in a loop fasion so that I can use them as an input to dfferent program.
Alex Mcaulley
Alex Mcaulley on 20 May 2019
Why do you want to do it with loops? It is inefficient, but you can do it easily adding another loop (from 1 to size(time)).
By the way, following my previous code, to shift the exponentials to the desired position:
time = [20 50 80];
peak = [5 3 2];
decay = [2 4 6];
t = cell2mat(arrayfun(@linspace,time,100*ones(size(time)),'UniformOutput',false)');
X = @(t) peak.*exp(-(t)./decay);
plot(t',cell2mat(arrayfun(X,linspace(0,100),'UniformOutput',false)'))

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!