Running a loop to generate multiple graphs using the same equation with different parameters

8 visualizzazioni (ultimi 30 giorni)
Hi All,
Below represents a model for exponential growth or decay. Any help much appreciated.
clear all; clc
t=linspace(0,5);
a=.75;
b=1;
c=1.5;
R0=1
R=R0*exp(a*t);
R1=R0*exp(b*t);
R2=R0*exp(c*t);
subplot(2,2,1)
xlabel('R(t)');
ylabel('Time');
plot(t, R,'g--'), title('Solution to dR/dt with R0>0');
hold on
plot(t, R1,'g--'), title('Solution to dR/dt with R0>0');
hold on
plot(t, R2,'g--'), title('Solution to dR/dt with R0>0');
hold on
xlim([0 5])
ylim([0 100])
hold off
grid;
I would like to figure out how to run a loop so that I can run both the variables for the equation and also a,b and c. The reason is because I want to add more plots like I have done below, but that is becoming inefficient. Any help much appreciated.
Final result should look like this but with a loop for R and also a, b and c.
clear all; clc
t=linspace(0,5);
a=.75;
b=1;
c=1.5;
d=-.5;
e=-1;
f=-2;
R0=1;
Ra=-1;
Rb=100;
Rc=-100;
R=R0*exp(a*t);
R1=R0*exp(b*t);
R2=R0*exp(c*t);
subplot(2,2,1)
xlabel('R(t)');
ylabel('Time');
plot(t, R,'g--'), title('Solution to dR/dt with R0>0');
hold on
plot(t, R1,'g--'), title('Solution to dR/dt with R0>0');
hold on
plot(t, R2,'g--'), title('Solution to dR/dt with R0>0');
hold on
xlim([0 5])
ylim([0 100])
hold off
grid;
R3=Ra*exp(a*t);
R4=Ra*exp(b*t);
R5=Ra*exp(c*t);
subplot(2,2,2)
xlabel('R(t)');
ylabel('Time');
plot(t, R3,'g--'), title('Solution to dR/dt with R0<0');
hold on
plot(t, R4,'g--'), title('Solution to dR/dt with R0<0');
hold on
plot(t, R5,'g--'), title('Solution to dR/dt with R0<0');
hold on
xlim([0 5])
ylim([-100 0])
hold off
grid;
R6=Rb*exp(d*t);
R7=Rb*exp(e*t);
R8=Rb*exp(f*t);
subplot(2,2,3)
xlabel('R(t)');
ylabel('Time');
plot(t, R6,'g--'), title('Solution to dR/dt with R0>0');
hold on
plot(t, R7,'g--'), title('Solution to dR/dt with R0>0');
hold on
plot(t, R8,'g--'), title('Solution to dR/dt with R0>0');
hold on
xlim([0 5])
ylim([0 100])
hold off
grid;
R9=Rc*exp(d*t);
R10=Rc*exp(e*t);
R11=Rc*exp(f*t);
subplot(2,2,4)
xlabel('R(t)');
ylabel('Time');
plot(t, R9,'g--'), title('Solution to dR/dt with R0<0');
hold on
plot(t, R10,'g--'), title('Solution to dR/dt with R0<0');
hold on
plot(t, R11,'g--'), title('Solution to dR/dt with R0<0');
hold on
xlim([0 5])
ylim([-100 0])
hold off
grid;
However I am unhappy with the constant reuse of a, b, c, and the reuse of the equations
Any help much appreciated.

Risposta accettata

Mathieu NOE
Mathieu NOE il 12 Ott 2021
hello
yes your code could be made more compact and flexible
see my suggestion below
clc
clearvars
t = linspace(0,5);
% organize aa = [a b c]' as a array of 4 columns corresponding to the 4
% scenarios (subplots)
aa = [.75 .75 -.5 -.5; % formerly a
1 1 -1 -1; % formerly b
1.5 1.5 -2 -2]; % formerly c
% organize R0 as a array of 4 columns corresponding to the 4 scenarios (subplots)
R0 = [1 -1 100 -100];
[m,n] = size(aa);
figure(1);
for ci = 1:n % number of scenarios (subplots)
for ck = 1:m % number of curves per scenario (subplots)
R(ck,:)=R0(ci)*exp(aa(ck,ci)*t);
end
subplot(2,2,ci)
plot(t, R,'g--'),grid on;
xlabel('R(t)');
ylabel('Time');
xlim([0 5]);
if R(1,1) <0
ylim([-100 0])
else
ylim([0 100])
end
end
  2 Commenti
Kevin Holly
Kevin Holly il 12 Ott 2021
I had a similar thought process.
clear all; clc
t=linspace(0,5);
%Inputs
a=[.75 1 1.5; -.5 -1 -2];
R=[1 -1 100 -100];
titles = ["Solution to dR/dt with R0>0" "Solution to dR/dt with R0<0"];
for j = 1:4
%Plots
nexttile %I used nexttile in case you wanted to add more scenarios
for i = 1:3
plot(t, R(j)*exp(a(ceil(j/2),i)*t),'g--')
hold on
end
title(titles(ceil(j/2)))
xlabel('R(t)');
ylabel('Time');
xlim([0 5])
if rem(j,2) == 0
ylim([-100 0])
else
ylim([0 100])
end
hold off
grid;
end

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Functions in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by