Unable to perform assignment because the left and right sides have a different number of elements.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
Réponse acceptée
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.'
2 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!