# Index exceeds matrix dimensions.

2 ビュー (過去 30 日間)
Shikhnazar Ismailov 2022 年 3 月 4 日

function []=diagonal()
n=41; ns=(n+1)/2; h=1/(n-1); nt=360;
x=(0:n-1)*h; y=x; p0=ones(n); tau=0.01;
for jt=1:nt
t(jt)=jt*tau;
pt = exp(-2 * t(jt) * x.' .* y);
end
tic
for jt=1:nt % Vaqt bo`yicha tsikl}
spmd
aa1(1)=0; bb1(1)=1;
a=1; c=1; b=2+2*h^2/tau;
for j=2:ns % Tenglamalar koeffitsientini parallel hisoblash
for i=2:n-1
ff=h^2*(-2*(2*t(jt)^2*(x(i)^2+y(j)^2)+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d=2*h^2/tau*p0(i,j)+(p0(i,j-1)-2*p0(i,j)+p0(i,j+1))+ff;
aa1(i)=c/(b-aa1(i-1)*a);
bb1(i)=(d+bb1(i-1)*a)/(b-aa1(i-1)*a);
end
p(n,j)=exp(-2*y(j)*t(jt));
for i=n-1:-1:1
p(i,j)=aa1(i)*p(i+1,j)+bb1(i);
end
end %j
for j=n-1:-1:ns-1 % Tenglamalar koeffitsientini parallel hisoblash
for i=2:n-1
ff=h^2*(-2*(2*t(jt)^2*(x(i)^2+y(j)^2)+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d=2*h^2/tau*p0(i,j)+(p0(i,j-1)-2*p0(i,j)+p0(i,j+1))+ff;
aa1(i)=c/(b-aa1(i-1)*a);
bb1(i)=(d+bb1(i-1)*a)/(b-aa1(i-1)*a);
end
p(n,j)=exp(-2*y(j)*t(jt));
for i=n-1:-1:1
p(i,j)=aa1(i)*p(i+1,j)+bb1(i);
end
end %j
for i=1:n
p(i,n)=(4.0*p(i,n-1)-p(i,n-2))/3;
p(i,1)=(4.0*p(i,2)-p(i,3))/3;
end
p0=p;
end
spmd
aa1(1)=0; bb1(1)=1;
a=1; c=1; b=2+2*h^2/tau;
for i=2:ns % Tenglamalar koeffitsientini parallel hisoblash
for j=2:n-1
ff=h^2*(-2*(2*t(jt)^2*(x(i)^2+y(j)^2)+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d=2*h^2/tau*p0(i,j)+(p0(i-1,j)-2*p0(i,j)+p0(i+1,j))+ff;
aa1(j)=c/(b-aa1(j-1)*a);
bb1(j)=(d+bb1(j-1)*a)/(b-aa1(j-1)*a);
end
p(i,n)=exp(-2*x(i)*t(jt));
for j=n-1:-1:1
p(i,j)=aa1(j)*p(i,j+1)+bb1(j);
end
end %i
for i=n-1:-1:ns-1 % Tenglamalar koeffitsientini parallel hisoblash
for j=2:n-1
ff=h^2*(-2*(2*t(jt)^2*(x(i)^2+y(j)^2)+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d=2*h^2/tau*p0(i,j)+(p0(i-1,j)-2*p0(i,j)+p0(i+1,j))+ff;
aa1(j)=c/(b-aa1(j-1)*a);
bb1(j)=(d+bb1(j-1)*a)/(b-aa1(j-1)*a);
end
p(i,n)=exp(-2*x(i)*t(jt));
for j=n-1:-1:1
p(i,j)=aa1(j)*p(i,j+1)+bb1(j);
end
end %i
for j=1:n
p(n,j)=(4.0*p(n-1,j)-p(n-2,j))/3;
p(1,j)=(4.0*p(2,j)-p(3,j))/3;
end
p0=p;
end
end % jt
toc
px = p0(21,:); pxt = pt(21,:);
x=0:h:1; y=0:h:1;
figure ('Position',[600 60 700 600]); %Natijalarni figuraga chiqarish oynasi: 750-eni, 650-boyi, 700-chapdan masofa, 75-yuqoridan masofa
subplot(3,2,1);
meshc(x,y,pt) %Uch o'lchovli 3D grafika
title('АНИК ЕЧИМ 3D ГРАФИГИ')
subplot(3,2,2);
meshc(x,y,p) %Uch o'lchovli 3D grafika
title('CОНЛИ ЕЧИМ 3D ГРАФИГИ')
subplot(3,2,3);
contour(x,y,pt,'ShowText','on','LineWidth',2); %kontur chizish
title('АНИК ЕЧИМ КОНТУР ГРАФИГИ')
subplot(3,2,4);
contour(x,y,p,'ShowText','on','LineWidth',2); %kontur chizish
title('CОНЛИ ЕЧИМ КОНТУР ГРАФИГИ')
subplot(3,2,5);
plot(x,px,x,pxt); %Ikki o'lchovli grafika
title('КЕСИМДА АНИК ВА CОНЛИ ЕЧИМ ЎЗГАРИШИ X БЎЙИЧА')
end
##### 2 件のコメント表示非表示 1 件の古いコメント
Shikhnazar Ismailov 2022 年 3 月 4 日

Error in program
px = p0(21,:);
Caused by:
Index exceeds matrix dimensions.

サインインしてコメントする。

### 回答 (1 件)

Cris LaPierre 2022 年 3 月 4 日

the complete error is
Error using indexing
An invalid indexing request was made.
Caused by:
Index in position 1 exceeds array bounds. Index must not exceed 1.
Whe I run your code, I discover that, because your invoke spmd, the code is run in a parallel pool. The resulting value of p0 from that pool is 1x4 composite
p0 =
Worker 1: class = double, size = [41 41]
Worker 2: class = double, size = [41 41]
Worker 3: class = double, size = [41 41]
Worker 4: class = double, size = [41 41]
You would access the values using notation similar to
p01=p0{1};
px = p01(21,:)
Incidentally, all of your worker results are the same, which suggest to me this calculation is likely not one that is suitable for a parallel pool. It also runs much quicker without it, as you do not need to start up the pool.
n=41; ns=(n+1)/2; h=1/(n-1); nt=360;
x=(0:n-1)*h; y=x; p0=ones(n); tau=0.01;
for jt=1:nt
t(jt)=jt*tau;
pt = exp(-2 * t(jt) * x.' .* y);
end
tic
for jt=1:nt % Vaqt bo`yicha tsikl}
aa1(1)=0; bb1(1)=1;
a=1; c=1; b=2+2*h^2/tau;
for j=2:ns % Tenglamalar koeffitsientini parallel hisoblash
for i=2:n-1
ff=h^2*(-2*(2*t(jt)^2*(x(i)^2+y(j)^2)+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d=2*h^2/tau*p0(i,j)+(p0(i,j-1)-2*p0(i,j)+p0(i,j+1))+ff;
aa1(i)=c/(b-aa1(i-1)*a);
bb1(i)=(d+bb1(i-1)*a)/(b-aa1(i-1)*a);
end
p(n,j)=exp(-2*y(j)*t(jt));
for i=n-1:-1:1
p(i,j)=aa1(i)*p(i+1,j)+bb1(i);
end
end %j
for j=n-1:-1:ns-1 % Tenglamalar koeffitsientini parallel hisoblash
for i=2:n-1
ff=h^2*(-2*(2*t(jt)^2*(x(i)^2+y(j)^2)+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d=2*h^2/tau*p0(i,j)+(p0(i,j-1)-2*p0(i,j)+p0(i,j+1))+ff;
aa1(i)=c/(b-aa1(i-1)*a);
bb1(i)=(d+bb1(i-1)*a)/(b-aa1(i-1)*a);
end
p(n,j)=exp(-2*y(j)*t(jt));
for i=n-1:-1:1
p(i,j)=aa1(i)*p(i+1,j)+bb1(i);
end
end %j
for i=1:n
p(i,n)=(4.0*p(i,n-1)-p(i,n-2))/3;
p(i,1)=(4.0*p(i,2)-p(i,3))/3;
end
p0=p;
aa1(1)=0; bb1(1)=1;
a=1; c=1; b=2+2*h^2/tau;
for i=2:ns % Tenglamalar koeffitsientini parallel hisoblash
for j=2:n-1
ff=h^2*(-2*(2*t(jt)^2*(x(i)^2+y(j)^2)+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d=2*h^2/tau*p0(i,j)+(p0(i-1,j)-2*p0(i,j)+p0(i+1,j))+ff;
aa1(j)=c/(b-aa1(j-1)*a);
bb1(j)=(d+bb1(j-1)*a)/(b-aa1(j-1)*a);
end
p(i,n)=exp(-2*x(i)*t(jt));
for j=n-1:-1:1
p(i,j)=aa1(j)*p(i,j+1)+bb1(j);
end
end %i
for i=n-1:-1:ns-1 % Tenglamalar koeffitsientini parallel hisoblash
for j=2:n-1
ff=h^2*(-2*(2*t(jt)^2*(x(i)^2+y(j)^2)+x(i)*y(j))*exp(-2*x(i)*y(j)*t(jt)));
d=2*h^2/tau*p0(i,j)+(p0(i-1,j)-2*p0(i,j)+p0(i+1,j))+ff;
aa1(j)=c/(b-aa1(j-1)*a);
bb1(j)=(d+bb1(j-1)*a)/(b-aa1(j-1)*a);
end
p(i,n)=exp(-2*x(i)*t(jt));
for j=n-1:-1:1
p(i,j)=aa1(j)*p(i,j+1)+bb1(j);
end
end %i
for j=1:n
p(n,j)=(4.0*p(n-1,j)-p(n-2,j))/3;
p(1,j)=(4.0*p(2,j)-p(3,j))/3;
end
p0=p;
end % jt
toc
Elapsed time is 0.149321 seconds.
px = p0(21,:); pxt = pt(21,:);
x=0:h:1; y=0:h:1;
figure ('Position',[600 60 700 600]); %Natijalarni figuraga chiqarish oynasi: 750-eni, 650-boyi, 700-chapdan masofa, 75-yuqoridan masofa
subplot(3,2,1);
meshc(x,y,pt) %Uch o'lchovli 3D grafika
title('АНИК ЕЧИМ 3D ГРАФИГИ')
subplot(3,2,2);
meshc(x,y,p) %Uch o'lchovli 3D grafika
title('CОНЛИ ЕЧИМ 3D ГРАФИГИ')
subplot(3,2,3);
contour(x,y,pt,'ShowText','on','LineWidth',2); %kontur chizish
title('АНИК ЕЧИМ КОНТУР ГРАФИГИ')
subplot(3,2,4);
contour(x,y,p,'ShowText','on','LineWidth',2); %kontur chizish
title('CОНЛИ ЕЧИМ КОНТУР ГРАФИГИ')
subplot(3,2,5);
plot(x,px,x,pxt); %Ikki o'lchovli grafika
title('КЕСИМДА АНИК ВА CОНЛИ ЕЧИМ ЎЗГАРИШИ X БЎЙИЧА')
##### 3 件のコメント表示非表示 2 件の古いコメント
Cris LaPierre 2022 年 3 月 4 日
On my system, with n=101 and nt=1080, it took 4 seconds to run without spmd. On my system, anythign using spmd takes much more time, even for the simple example above.
I'm definitely not an expert on parallelizing code, but perhaps this example can help you.

サインインしてコメントする。

### カテゴリ

Find more on Simulink Environment Customization in Help Center and File Exchange

R2017b

### Community Treasure Hunt

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

Start Hunting!

Translated by