how to plot multiple decaying exponentials in one plot.

1 次查看(过去 30 天)
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,

回答(1 个)

Alex Mcaulley
Alex Mcaulley 2019-5-20
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 个评论
Romio
Romio 2019-5-20
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 2019-5-20
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)'))

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by