How can I loop for Exponential Decay?

4 views (last 30 days)
Muhammed Said Gören
Muhammed Said Gören on 24 May 2022
Commented: Walter Roberson on 24 May 2022
I tried to write Exponential Decay code. I have calculated into the function for each k value but I want to loop these codes. How can I do that?
clear;
clc;
c_0=10.2;
k=[0.13;0.28;0.51;0.72;0.89];
t_half=log(2)./k;
t=linspace(0,max(t_half)*2,100);
c=c_0*exp(-k(1)*t);
plot(t,c,'-g');
hold on;
c=c_0*exp(-k(2)*t);
plot(t,c,'-k');
hold on;
c=c_0*exp(-k(3)*t);
plot(t,c,'-b');
hold on;
c=c_0*exp(-k(4)*t);
plot(t,c,'-m');
hold on;
c=c_0*exp(-k(5)*t);
plot(t,c,'-c');
hold on;
e=ones(1,100);
plot(t,5.1*e,'-r');
grid on;
title("Exponential Decay");
xlabel('Time');
ylabel('Concentration');

Answers (1)

Walter Roberson
Walter Roberson on 24 May 2022
c=c_0*exp(-k(3)*t);
if t increases by 1 then the next value is exp(-k(3)) times the previous one. So in loop form, for each step multiply the previous result by a constant factor exp(-k)
  2 Comments
Muhammed Said Gören
Muhammed Said Gören on 24 May 2022
How can I write a for loop to adapt if I change the value of k?
Walter Roberson
Walter Roberson on 24 May 2022
for iter = 1:50
k = AdjustK(k, iter);
c = c.*exp(-k);
allc(:, end+1) = c;
end
where AdjustK changes k values under whatever circumstances might be appropriate (including possibly updating based on user sliders)

Sign in to comment.

Categories

Find more on Particle & Nuclear Physics 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!