Summing and multiplying matrices of different size

1 view (last 30 days)
So, I have this equation:
The size of C is a 9 by 9 matrice, and X ,Y and sigma parameter is a matrice of size 1 by 9 matrice.
I have this code:
%% Define the function that output classes
% The function will take parameter values, initial condition of the ODE and
% the expected time to run the model.
function [Classes] = model2(para,ICs,maxtime)
opts = odeset('RelTol',1e-4,'AbsTol',1e-3); %tolerance level
tspan = [0:1:maxtime]; %time span
agegroup= 9;
%Shape of the initial condition
All = reshape([ICs.S'; ICs.E'; ICs.I'; ICs.A';ICs.H'; ICs.R'; ICs.D'], 7*agegroup,1);
[t , pop] = ode15s(@(t,y)diff_socialmodel(t,y,para),tspan,All,opts);
%% Define the structure of the output
Classes = struct('S',pop(:,1:1:9),'E',pop(:,10:1:18),'I',pop(:,19:1:27), ...
'A',pop(:,28:1:36),'H',pop(:,37:1:45),'R',pop(:,46:1:54), ...
%% Solving each the ODE at every time step
function dpop = diff_socialmodel(t,pop,para)
S = pop(1:1:9);
E = pop(10:1:18);
I = pop(19:1:27);
A = pop(28:1:36);
H = pop(37:1:45);
R = pop(46:1:54);
D = pop(55:1:63);
%% Store my output here
dpop = zeros(7*para.agegroup,1);
%% High Income population
dpop(1:1:9) = - (para.beta*para.a*para.l*(para.h.*I+A))*S;
dpop(10:1:18) = (para.beta*para.a*para.l*(para.h*I+A))*S- para.sigma.*E;
dpop(19:1:27) = para.psi*para.sigma*E + para.epsilon*para.gamma*A - para.eta*I;
dpop(28:1:36) = (1-para.psi)*para.sigma*E - para.gamma*A;
dpop(37:1:45) = para.phi*para.eta*I*H;
dpop(46:1:54) = (1-para.epsilon)*para.gamma*A + (1-para.phi)*para.eta*I +*para.d*H;
dpop(55:1:63) = (1-para.d)**H;
It appears to work but the result is not good. I think I am doing something wrong.
Maybe my code can give a better perspective.
When I change the matrix l, the output yield the same result, which is strange. Each time I change matrix l, I expect a different output.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 16 Nov 2022
Check that you get what you expect from each product and term separately. Then figure out what you should do to fix your discrepancies. It seems likely that the product between the two factors should be an element-wise product:
dpop(1:1:9) = - ((beta*sigma.*S).*(C*(para.h*I+A)));
But that is mainly guess-work.

More Answers (0)


Find more on Simulink Coder in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by