MATLAB Answers

Why my graph only get one line?

1 ビュー (過去 30 日間)
Meor Hasan Meor Jumat
Meor Hasan Meor Jumat 2021 年 6 月 29 日
コメント済み: KSSV 2021 年 6 月 29 日
clear all
clc
clf
% Initial Value and time step
a = 0; % Lower limit of Integration from 21 Oct 2020
b = 700; % Upper limit of Integration at 20 Oct 2021
h = 0.5; % h = delta_x
n = ((b-a)/h); % no. of points
T = 70;
TT = 100;
tau0 = 2*10^-9;
tau1 = 1.41*10^-9;
alpha = 0.0005;
gamma1 = 1/10;%7.31;
gamma2 = 1/15;%0.11;
mu = 1/50;%1.26;
y10 = 8.18*10^7;
y20 = 5.0604*10^3;
y30 = 4.4876*10^3;
y40 = 4590;
y = zeros(n, 4);
x = zeros(n, 1);
y(1, :) = [y10; y20; y30; y40];
x(1) = a;
tau(1) = tau0;
fcn = @(x, y) [-tau(1) * y(1) * (y(2) + y(3)),...
alpha * tau(1) * y(1) * (y(2) + y(3)) - gamma1 * y(2),...
(1-alpha) * tau(1) * y(1) * (y(2) + y(3)) - ((gamma2 + mu) * y(3)),...
gamma1 * y(2) + gamma2 * y(3) ]; % system function
for i = 1:n-1
x( i + 1 ) = x(i) + h;
if (i >= 1) && (i < T);
tau(i) = tau0;
elseif (i >= T) && (i <= TT);
tau(i) = tau0*exp(-alpha*i);
else(i > TT);
tau(i) = tau1;
end
d1 = h * fcn(x(i), y(i, :));
d2 = h * fcn(x(i), y(i, :) + d1 / 2);
d3 = h * fcn(x(i), y(i, :) + d2 / 2);
d4 = h + fcn(x(i), y(i, :) + d3);
y(i+1, :) = y(i, :) + ((d1 + d2 + d3 + d4)* (h / 6));
end
figure(1)
plot(x,y(:,3),'blue')
hold on
%%%%%
clear all
clc
clf
% Initial Value and time step
a = 0; % Lower limit of Integration from 21 Oct 2020
b = 700; % Upper limit of Integration at 20 Oct 2021
h = 0.5; % h = delta_x
n = ((b-a)/h); % no. of points
T = 70;
TT = 100;
tau0 = 2*10^-9;
tau1 = 1.41*10^-9;
alpha = 0.01;
gamma1 = 1/10;
gamma2 = 1/15;
mu = 1/50;
y10 = 8.18*10^7;
y20 = 5.0604*10^3;
y30 = 4.4876*10^3;
y40 = 4590;
y = zeros(n, 4);
x = zeros(n, 1);
y(1, :) = [y10; y20; y30; y40];
x(1) = a;
tau(1) = tau0;
fcn = @(x, y) [-tau(1) * y(1) * (y(2) + y(3)),...
alpha * tau(1) * y(1) * (y(2) + y(3)) - gamma1 * y(2),...
((1-alpha) * tau(1) * y(1) * (y(2) + y(3))) - ((gamma2 + mu) * y(3)),...
gamma1 * y(2) + gamma2 * y(3) ]; % system function
for i = 1:n-1
x( i + 1 ) = x(i) + h;
if (i >= 1) && (i < T);
tau(i) = tau0;
elseif (i >= T) && (i <= TT);
tau(i) = tau0*exp(-alpha*i);
else(i > TT);
tau(i) = tau1;
end
d1 = h * fcn(x(i), y(i, :));
d2 = h * fcn(x(i), y(i, :) + d1 / 2);
d3 = h * fcn(x(i), y(i, :) + d2 / 2);
d4 = h + fcn(x(i), y(i, :) + d3);
y(i+1, :) = y(i, :) + ((d1 + d2 + d3 + d4)* (h / 6));
end
figure(1)
plot(x,y(:,3),'red')
hold on
%%%%%%%%%%%%%%%%%%
clear all
clc
clf
% Initial Value and time step
a = 0; % Lower limit of Integration from 21 Oct 2020
b = 700; % Upper limit of Integration at 20 Oct 2021
h = 0.5; % h = delta_x
n = ((b-a)/h); % no. of points
T = 70;
TT = 100;
tau0 = 2*10^-9;
tau1 = 1.41*10^-9;
alpha = 0.1;
gamma1 = 1/10;
gamma2 = 1/15;
mu = 1/50;
y10 = 8.18*10^7;
y20 = 5.0604*10^3;
y30 = 4.4876*10^3;
y40 = 4590;
y = zeros(n, 4);
x = zeros(n, 1);
y(1, :) = [y10; y20; y30; y40];
x(1) = a;
tau(1) = tau0;
fcn = @(x, y) [-tau(1) * y(1) * (y(2) + y(3)),...
alpha * tau(1) * y(1) * (y(2) + y(3)) - gamma1 * y(2),...
((1-alpha) * tau(1) * y(1) * (y(2) + y(3))) - ((gamma2 + mu) * y(3)),...
gamma1 * y(2) + gamma2 * y(3) ]; % system function
for i = 1:n-1
x( i + 1 ) = x(i) + h;
if (i >= 1) && (i < T);
tau(i) = tau0;
elseif (i >= T) && (i <= TT);
tau(i) = tau0*exp(-alpha*i);
else(i > TT);
tau(i) = tau1;
end
d1 = h * fcn(x(i), y(i, :));
d2 = h * fcn(x(i), y(i, :) + d1 / 2);
d3 = h * fcn(x(i), y(i, :) + d2 / 2);
d4 = h + fcn(x(i), y(i, :) + d3);
y(i+1, :) = y(i, :) + ((d1 + d2 + d3 + d4)* (h / 6));
end
figure(1)
plot(x,y(:,3),'cyan')
hold on
clear all
clc
clf
% Initial Value and time step
a = 0; % Lower limit of Integration from 21 Oct 2020
b = 700; % Upper limit of Integration at 20 Oct 2021
h = 0.5; % h = delta_x
n = ((b-a)/h); % no. of points
T = 70;
TT = 100;
tau0 = 2*10^-9;
tau1 = 1.41*10^-9;
alpha = 0.45;
gamma1 = 1/10;%7.31;
gamma2 = 1/15;%0.11;
mu = 1/50;%1.26;
y10 = 8.18*10^7;
y20 = 5.0604*10^3;
y30 = 4.4876*10^3;
y40 = 4590;
y = zeros(n, 4);
x = zeros(n, 1);
y(1, :) = [y10; y20; y30; y40];
x(1) = a;
tau(1) = tau0;
fcn = @(x, y) [-tau(1) * y(1) * (y(2) + y(3)),...
alpha * tau(1) * y(1) * (y(2) + y(3)) - gamma1 * y(2),...
((1-alpha) * tau(1) * y(1) * (y(2) + y(3))) - ((gamma2 + mu) * y(3)),...
gamma1 * y(2) + gamma2 * y(3) ]; % system function
for i = 1:n-1
x( i + 1 ) = x(i) + h;
if (i >= 1) && (i < T);
tau(i) = tau0;
elseif (i >= T) && (i <= TT);
tau(i) = tau0*exp(-alpha*i);
else(i > TT);
tau(i) = tau1;
end
d1 = h * fcn(x(i), y(i, :));
d2 = h * fcn(x(i), y(i, :) + d1 / 2);
d3 = h * fcn(x(i), y(i, :) + d2 / 2);
d4 = h + fcn(x(i), y(i, :) + d3);
y(i+1, :) = y(i, :) + ((d1 + d2 + d3 + d4)* (h / 6));
end
figure(1)
plot(x,y(:,3),'green')
hold on
clear all
clc
clf
% Initial Value and time step
a = 0; % Lower limit of Integration from 21 Oct 2020
b = 700; % Upper limit of Integration at 20 Oct 2021
h = 0.5; % h = delta_x
n = ((b-a)/h); % no. of points
T = 70;
TT = 100;
tau0 = 2*10^-9;
tau1 = 1.41*10^-9;
alpha = 0.5;
gamma1 = 1/10;%7.31;
gamma2 = 1/15;%0.11;
mu = 1/50;%1.26;
y10 = 8.18*10^7;
y20 = 5.0604*10^3;
y30 = 4.4876*10^3;
y40 = 4590;
y = zeros(n, 4);
x = zeros(n, 1);
y(1, :) = [y10; y20; y30; y40]; %
x(1) = a;
tau(1) = tau0;
fcn = @(x, y) [-tau(1) * y(1) * (y(2) + y(3)),...
alpha * tau(1) * y(1) * (y(2) + y(3)) - gamma1 * y(2),...
((1-alpha) * tau(1) * y(1) * (y(2) + y(3))) - ((gamma2 + mu) * y(3)),...
gamma1 * y(2) + gamma2 * y(3) ]; % system function
for i = 1:n-1
x( i + 1 ) = x(i) + h;
if (i >= 1) && (i < T);
tau(i) = tau0;
elseif (i >= T) && (i <= TT);
tau(i) = tau0*exp(-alpha*i);
else(i > TT);
tau(i) = tau1;
end
d1 = h * fcn(x(i), y(i, :));
d2 = h * fcn(x(i), y(i, :) + d1 / 2);
d3 = h * fcn(x(i), y(i, :) + d2 / 2);
d4 = h + fcn(x(i), y(i, :) + d3);
y(i+1, :) = y(i, :) + ((d1 + d2 + d3 + d4)* (h / 6));
end
figure(1)
plot(x,y(:,3),'yellow')
hold on
clear all
clc
clf
% Initial Value and time step
a = 0; % Lower limit of Integration from 21 Oct 2020
b = 700; % Upper limit of Integration at 20 Oct 2021
h = 0.5; % h = delta_x
n = ((b-a)/h); % no. of points
T = 70;
TT = 100;
tau0 = 2*10^-9;
tau1 = 1.41*10^-9;
alpha = 0.95;
gamma1 = 1/10;%7.31;
gamma2 = 1/15;%0.11;
mu = 1/50;%1.26;
y10 = 8.18*10^7;
y20 = 5.0604*10^3;
y30 = 4.4876*10^3;
y40 = 4590;
y = zeros(n, 4);
x = zeros(n, 1);
y(1, :) = [y10; y20; y30; y40];
x(1) = a;
tau(1) = tau0;
fcn = @(x, y) [-tau(1) * y(1) * (y(2) + y(3)),...
alpha * tau(1) * y(1) * (y(2) + y(3)) - gamma1 * y(2),...
((1-alpha) * tau(1) * y(1) * (y(2) + y(3))) - ((gamma2 + mu) * y(3)),...
gamma1 * y(2) + gamma2 * y(3) ]; % system function
for i = 1:n-1
x( i + 1 ) = x(i) + h; % Not: "x(i+1) = 1*h", but maybe: "i * h"
if (i >= 1) && (i < T);
tau(i) = tau0;
elseif (i >= T) && (i <= TT);
tau(i) = tau0*exp(-alpha*i);
else(i > TT);
tau(i) = tau1;
end
d1 = h * fcn(x(i), y(i, :));
d2 = h * fcn(x(i), y(i, :) + d1 / 2);
d3 = h * fcn(x(i), y(i, :) + d2 / 2);
d4 = h + fcn(x(i), y(i, :) + d3);
y(i+1, :) = y(i, :) + ((d1 + d2 + d3 + d4)* (h / 6));
end
figure(1)
plot(x,y(:,3),'magenta')
hold off

回答 (1 件)

KSSV
KSSV 2021 年 6 月 29 日
You have used clf....remove this
  2 件のコメント
KSSV
KSSV 2021 年 6 月 29 日
clf - clears the figures .
If you are running each function seperately.....as there is clear all your hold on gets invalid.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by