Please check the time series code ,the code is not running where B is a matrix of order 20

1 次查看(过去 30 天)
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);
Function definitions in a script must appear at the end of the file.
Move all statements after the "kau" function definition to before the first local function definition.
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');

回答(1 个)

Walter Roberson
Walter Roberson 2023-8-15
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)
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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)]'
y = 19×1
0 1 1 0 0 0 0 0 0 0
% 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);
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.

Error in solution>kau (line 63)
f = [f; x(i)*(1 - theta*x(i)) - x(i+1) - (x(i)*x(i+1)) / (1 + x(i+1)) + dh*B*y; ...

Error in solution>@(t,x)kau(t,x,B,dh,phi,theta,y) (line 47)
[t, x]= ode45(@(t,x)kau(t, x, B, dh, phi, theta, y),tspan, x0);

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
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

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by