Effacer les filtres
Effacer les filtres

Unable to perform assignment because the left and right sides have a different number of elements.

2 vues (au cours des 30 derniers jours)
I am trying to solve delay logistic equation with multiple delay terms and after getting solution i am taking derivative of this function but for i am getting error and my error is
"Unable to perform assignment because the left and right sides have a different number of elements."
g = @(t, y, Z, par) par(1) * y * (1 - sum(par(2:end) .* Z));
tau = [1, 1.5,2,2.5,3]; % Array of different delays
par = [1.5, 0.1,0.2,0.3,0.4,0.5];
this is my equation
x_t = deval(sol, t_t);
%% Add noise to state terms
eps = 0;
x_tn= x_t + eps * randn(size(x_t)) .* x_t;
%% Calculate delayed states for multiple delays
x_d = cell(length(tau), 1);
for k = 1:length(tau)
x_d{k} = deval(sol, t_t - tau(k));
end
x_dn = cell(size(x_d));
for k = 1:length(x_d)
x_dn{k} = x_d{k} + eps * randn(size(x_d{k})) .* x_d{k};
end
and I am getting error here in this part
%% Compute derivative
DX = zeros(1, length(t_t));
for i = 1:length(t_t)
Z = cellfun(@(zd) zd(:,i), x_d); % Collect all delayed states at t_t(i)
DX(i) = g(t_t(i), x_t(i), Z, par);
end
DX=DX';
  4 commentaires
Dyuman Joshi
Dyuman Joshi le 28 Nov 2023
phi and tspan are not defined. Please share their values too.

Connectez-vous pour commenter.

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 28 Nov 2023
The output of g(....) is a 5x1 numerical array and DX(i) is a 1x1 numerical element. And you can not assign 5x1 numerical array into 1x1 numerical placeholder.
You can either define DX as a cell array (as you have done for x_d and x_dn) or preallocate a numeric array based on the size of x_d. I've chosen the latter option here -
g = @(t, y, Z, par) par(1) * y * (1 - sum(par(2:end) .* Z));
tau = [1, 1.5,2,2.5,3]; % Array of different delays
par = [1.5, 0.1,0.2,0.3,0.4,0.5];
phi = @(x) cos(x);
tspan=[0 20];
sol = dde23(@(t,y,Z) g(t,y,Z,par), tau, phi, tspan);
%% Split into training and testing
tr_r = 0.8;
t_all = sol.x;
max_tau = max(tau); % Find the maximum value in tau
t_all = t_all(t_all >= max_tau);
% t_all = t_all(t_all >= tau);
n_all = length(t_all);
n_train = round(tr_r * n_all);
t_t = t_all(1:n_train);
x_t = deval(sol, t_t);
%% Add noise to state terms
eps = 0;
x_tn= x_t + eps * randn(size(x_t)) .* x_t;
%% Calculate delayed states for multiple delays
[x_d, x_dn] = deal(cell(length(tau), 1));
%Club the for loops together
for k = 1:length(tau)
arr = deval(sol, t_t - tau(k));
x_d{k} = arr;
x_dn{k} = arr + eps * randn(size(arr)) .* arr;
end
%% Compute derivative
DX = zeros(size(x_d,1), length(t_t));
for i = 1:length(t_t)
Z = cellfun(@(zd) zd(:,i), x_d);
DX(:,i) = g(t_t(i), x_t(i), Z, par);
end
DX=DX.'
DX = 86×5
-0.0860 -0.2220 -0.3579 -0.4938 -0.6297 -0.0537 -0.1382 -0.2226 -0.3070 -0.3914 -0.0342 -0.0876 -0.1411 -0.1946 -0.2481 -0.0222 -0.0569 -0.0916 -0.1263 -0.1610 -0.0147 -0.0376 -0.0605 -0.0834 -0.1063 -0.0098 -0.0251 -0.0404 -0.0557 -0.0710 -0.0066 -0.0169 -0.0272 -0.0375 -0.0478 -0.0045 -0.0115 -0.0185 -0.0255 -0.0325 -0.0030 -0.0078 -0.0126 -0.0174 -0.0221 -0.0021 -0.0053 -0.0086 -0.0119 -0.0151

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by