Running a loop to generate multiple graphs using the same equation with different parameters
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Functions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
