# how can I vectorize my code when multiple variables are present

조회 수: 5 (최근 30일)
Deepa Maheshvare 2021년 2월 21일
편집: Deepa Maheshvare 2021년 2월 23일
Hi All,
This is a follow up to my previous question here.
I'm trying to figure out how to vectorize my code when multiple variables are present. The following is the toy model with odes for 2 variables (Please note the code may not make much sense, in my real system variables w and u are coupled. Also, in the actual system the number of variables in the model is uder-defined. For this reason, I am looping through the variables in the for-loop).
global mat1 mat2 k nvar var npts
k = 2;
nvar =2;
var = ["w", "x"];
npts=10;
mat1=[
1 -2 1 0 0 0 0 0 0 0;
0 1 -2 1 0 0 0 0 0 0;
0 0 1 -2 1 0 0 0 0 0;
0 0 0 1 -2 1 0 0 0 0;
0 0 0 0 1 -2 1 0 0 0;
0 0 0 0 0 1 -2 1 0 0;
0 0 0 0 0 0 1 -2 1 0;
0 0 0 0 0 0 0 1 -2 1;
];
mat2 = [
1 -1 0 0 0 0 0 0 0 0;
0 1 -1 0 0 0 0 0 0 0;
0 0 1 -1 0 0 0 0 0 0;
0 0 0 1 -1 0 0 0 0 0;
0 0 0 0 1 -1 0 0 0 0;
0 0 0 0 0 1 -1 0 0 0;
0 0 0 0 0 0 1 -1 0 0;
0 0 0 0 0 0 0 1 -1 0;
];
w0 = [2 0 0 0 0 0 0 0 0 0]';
x0 = [1 0 0 0 0 0 0 0 0 0]';
z0 = [w0; x0];
tspan = 0:0.01:0.5;
f0 = fun(0, z0);
joptions = struct('diffvar', 2, 'vectvars', 2, 'thresh', 1e-8, 'fac', []);
J = odenumjac(@fun,{0 z0}, f0, joptions);
sparsity_pattern = sparse(J~=0.);
options = odeset('Stats', 'on', 'Vectorized', 'on'); %, 'JPattern', sparsity_pattern);
ttic = tic();
[t, sol] = ode15s(@(t,z) fun(t,z), tspan , z0, options);
ttoc = toc(ttic)
fprintf('runtime %f seconds ...\n', ttoc)
plot(t, sol)
function Zstruct = get_Zstruct(z)
global nvar var npts
temp = reshape(z, npts, []);
for i = 1:nvar
Zstruct.(var(i)) = temp(:,i);
end
end
function dz = fun(t,z)
global nvar var mat1 mat2
fprintf('size of z %d %d...\n', size(z))
dz = zeros(size(z), 'like', z);
Zstruct = get_Zstruct(z);
F =[];
for i = 1:nvar
x = Zstruct.(var(i));
f = zeros(size(x), 'like', x);
f(1,:) = 0;
f(2:9,:) = mat1*x + mat2*x;
f(10,:) = 2*(x(end-1) - x(end));
end
F(:,:) = vertcat(F,f);
end
I tried to check the size of z and it inconsistent for different function calls.
size of z 20 1...
size of z 20 20...
size of z 20 1...
size of z 20 1...
:
size of z 20 1...
size of z 20 1...
size of z 20 1...
size of z 20 20...
I'm sure the mistake is in the function Zstruct = get_Zstruct(z). I'm not sure how to vectorize here. Could someone please help me with this?
##### 댓글 수: 3이전 댓글 1개 표시이전 댓글 1개 숨기기
Walter Roberson 2021년 2월 23일
for i = 1:nvar
Zstruct.(var(i)) = temp(:,i);
end
cell2struct() instead of the loop.
Deepa Maheshvare 2021년 2월 23일
편집: Deepa Maheshvare 2021년 2월 23일
@Walter Roberson I'm sorry, I didn't understand your comment completely .
temp is a double, sorry, I'm a bit confused how to use cell2struct() here. Also, I couldn't understand why the size of `z` varies from 20 x 1 to 20 x 20.

댓글을 달려면 로그인하십시오.

### 답변 (1개)

darova 2021년 2월 22일
Try to remove parenthesis
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Deepa Maheshvare 2021년 2월 23일
편집: Deepa Maheshvare 2021년 2월 23일
when I try removing the paranthesis
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in cse_2_21_21>get_Zstruct (line 50)
Zstruct.var(i) = temp(:,i);
Error in cse_2_21_21>fun (line 58)
Zstruct = get_Zstruct(z);
Error in cse_2_21_21 (line 34)
f0 = fun(0, z0);

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by