Please check the time series code ,the code is not running where B is a matrix of order 20
1 view (last 30 days)
Show older comments
clc;
clear all;
n = 20;
x=accumarray([2 3 n-1 n]',1);
B = toeplitz(x,x);
% G=graph(B);
% plot(G)
% Parameters
% matrixSize = 10; % Size of the matrix
numEdgesToRewire = 2; % Number of edges to rewire
% Create an initial matrix
% Y = B;
matrixSize=20;
% Randomly select two edges to rewire
edgesToRewire = randperm(matrixSize, numEdgesToRewire);
% Iterate through the edges to rewire
for i = 1:numEdgesToRewire
node1 = edgesToRewire(i);
node2 = randi([1, matrixSize]);
while node2 == node1 || B(node1, node2) == 1
node2 = randi([1, matrixSize]);
end
B(node1, node2) = 1;
B(node2, node1) = 1;
end
disp(B)
theta = 0.3;
dh = 2^-9;
dp = 2^-7;
phi = 3;
ita = 1;
y=[x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10),x(11),x(12),x(13),x(14),x(15),x(16),x(18),x(19),x(20)]'
% x0 = rand(1,20);
% H=x(1)+x(3)+x(5)+x(7)+x(9)+x(11)+x(13)+x(15)+x(17)+x(19);
% P=x(2)+x(4)+x(6)+x(8)+x(10)+x(12)+x(14)+x(16)+x(18)+x(20);
x0 = rand(1,20);
i = 1;
f = [];
function xprime = kau(t, x, B)
nn = length(x);
while i <= nn
f = [f; x(i)*(1 - theta*x(i)) - x(i+1) - (x(i)*x(i+1)) / (1 + x(i+1)) + dh*B*y; ...
(phi*x(i)*x(i+1)) / (1 + x(i+1)) - ita*x(i+1) + dp*B*y];
i = i + 1;
end
xprime = f;
end
[t, x]= ode45(@(kau(t, x, B),tspan, x0);
H=x(1)+x(3)+x(5)+x(7)+x(9)+x(11)+x(13)+x(15)+x(17)+x(19);
P=x(2)+x(4)+x(6)+x(8)+x(10)+x(12)+x(14)+x(16)+x(18)+x(20);
% % P=sum(x(i+1));
plot(t, H, 'b');
hold on
plot(t, P, 'r');
0 Comments
Answers (1)
Walter Roberson
on 15 Aug 2023
Several different things needed to be fixed.
But why are you excluding x(17) in your y? That makes y 19 tall when your B is 20 tall, and that prevents you from doing matrix multiplication between y and B.
n = 20;
x=accumarray([2 3 n-1 n]',1);
B = toeplitz(x,x);
% G=graph(B);
% plot(G)
% Parameters
% matrixSize = 10; % Size of the matrix
numEdgesToRewire = 2; % Number of edges to rewire
% Create an initial matrix
% Y = B;
matrixSize=20;
% Randomly select two edges to rewire
edgesToRewire = randperm(matrixSize, numEdgesToRewire);
% Iterate through the edges to rewire
B = zeros(matrixSize, matrixSize);
for i = 1:numEdgesToRewire
node1 = edgesToRewire(i);
node2 = randi([1, matrixSize]);
while node2 == node1 || B(node1, node2) == 1
node2 = randi([1, matrixSize]);
end
B(node1, node2) = 1;
B(node2, node1) = 1;
end
disp(B)
theta = 0.3;
dh = 2^-9;
dp = 2^-7;
phi = 3;
ita = 1;
y=[x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10),x(11),x(12),x(13),x(14),x(15),x(16),x(18),x(19),x(20)]'
% x0 = rand(1,20);
% H=x(1)+x(3)+x(5)+x(7)+x(9)+x(11)+x(13)+x(15)+x(17)+x(19);
% P=x(2)+x(4)+x(6)+x(8)+x(10)+x(12)+x(14)+x(16)+x(18)+x(20);
x0 = rand(1,20);
tspan = [0 10]; %ADDED
[t, x]= ode45(@(t,x)kau(t, x, B, dh, phi, theta, y),tspan, x0);
H=x(1)+x(3)+x(5)+x(7)+x(9)+x(11)+x(13)+x(15)+x(17)+x(19);
P=x(2)+x(4)+x(6)+x(8)+x(10)+x(12)+x(14)+x(16)+x(18)+x(20);
% % P=sum(x(i+1));
plot(t, H, 'b');
hold on
plot(t, P, 'r');
function xprime = kau(t, x, B, dh, phi, theta, y)
nn = length(x);
i = 1;
f = [];
while i <= nn
f = [f; x(i)*(1 - theta*x(i)) - x(i+1) - (x(i)*x(i+1)) / (1 + x(i+1)) + dh*B*y; ...
(phi*x(i)*x(i+1)) / (1 + x(i+1)) - ita*x(i+1) + dp*B*y];
i = i + 1;
end
xprime = f;
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!