Why am I not able to use decimal numbers in my code? (Index in position 1 is invalid. Array indices must be positive integers or logical values)

26 ビュー (過去 30 日間)
I'm getting the error "Index in position 1 is invalid. Array indices must be positive integers or logical values", meaning that my numbers with decimals won't work (which I need), as in the rho, and some others I need to change, and I'm kind of unsure of what to change.
clc
clear all;
close all;
loop=1;
Nx=50;
Ny=50;
M=4000;
%--------------------------------------------------------------------------
%--Initialize V-matrix and Rho-matrix--------------------------------------
%---V(i,j)=potential inside the 2-D box------------------------------------
%---rho(i,j)=charge density inside the 2-D box-----------------------------
%---i is along X-axis and j is along Y-axis--------------------------------
%--------------------------------------------------------------------------
V(1:Nx,1:Ny)=0.0;
rho(1:Nx,1:Ny)=0.0;
rho(0.05,0.025)=0; % charge at the center of the box
%--------------------------------------------------------------------------
% Boundary conditions
V(1,:)=0;
V(Nx,:)=1;
V(:,1)=0;
V(:,Ny)=0;
X=1:Nx;
Y=1:Ny;
contour(X,Y,V,0:1:50,'linewidth',2)
h=gca;
get(h,'FontSize')
set(h,'FontSize',12)
colorbar('location','eastoutside','fontsize',12);
axis([1 Nx 1 Ny])
xlabel('X','fontSize',12);
ylabel('Y','fontSize',12);
title('Electric Potential Distribution, V(X,Y)','fontsize',12);
fh = figure(1);
set(fh, 'color', 'white');
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 2 日
Are you sure you want to go as far out as rho(x=50, y=50) = 0.0 when you are also working at the scale of rho(x=0.05, y=0.025) = 0 ? In order to place everything at grid coordinates, you would have to use indexes rho(x,y) -> Rho(row = 40*x, column = 40*y) which would take you out to row 2000 and column 2000, and rho(x=0.05, y=0.025) would then map to Rho(row=2, column=1) .
But your comment puts the charge (0) "at the center of the box", implying that the box would have to extend from about x=-50 to x=+50 and y=-50 to y=+50, which would require using grid coordinates such as Rho(row=80*x+4001, column=80*y+4001) for a total of 8001 rows and columns.... are you sure that is what you want?
Nicholas Gillam
Nicholas Gillam 2019 年 4 月 2 日
I haven't changed that part yet since I'm stuck trying to get things to non integer values, but I need the values of the boundaries to be x=y=0.1.

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

回答 (2 件)

Star Strider
Star Strider 2019 年 4 月 2 日
This is the assignment that throws the error:
rho(0.05,0.025)=0; % charge at the center of the box
Note that 0.05 and 0.025 are not integers.
Since ‘rho’ is a (50x50) double array, perhaps you intend:
rho(25, 25)=0; % charge at the center of the box
With that change, your code runs without error, although the result is likely less than what you want.
  2 件のコメント
Nicholas Gillam
Nicholas Gillam 2019 年 4 月 2 日
編集済み: Nicholas Gillam 2019 年 4 月 2 日
I actually need non integer values (hence the 0.05, 0.025). I know I have been able to do it before on similar code (which I so convientiently don't have anymore), and can't remember how to do it.
Walter Roberson
Walter Roberson 2019 年 4 月 2 日
The only way it could have worked is if you had defined a custom storage class that permitted non-integer indices.

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


Walter Roberson
Walter Roberson 2019 年 4 月 2 日
factor = 40;
loop=1;
Nx=50;
Ny=50;
M=4000;
%--------------------------------------------------------------------------
%--Initialize V-matrix and Rho-matrix--------------------------------------
%---V(i,j)=potential inside the 2-D box------------------------------------
%---rho(i,j)=charge density inside the 2-D box-----------------------------
%---i is along X-axis and j is along Y-axis--------------------------------
%--------------------------------------------------------------------------
V(1:Nx*factor,1:Ny*factor)=0.0;
rho(1:Nx*factor,1:Ny*factor)=0.0;
rho(0.05*factor,0.025*factor)=0; % charge at the center of the box
%--------------------------------------------------------------------------
% Boundary conditions
V(1,:)=0;
V(Nx*factor,:)=1;
V(:,1)=0;
V(:,Ny*factor)=0;
X=1:Nx*factor;
Y=1:Ny*factor;
contour(X,Y,V,0:1:50,'linewidth',2)
h=gca;
get(h,'FontSize')
set(h,'FontSize',12)
colorbar('location','eastoutside','fontsize',12);
axis([1 Nx 1 Ny])
xlabel('X','fontSize',12);
ylabel('Y','fontSize',12);
title('Electric Potential Distribution, V(X,Y)','fontsize',12);
fh = figure(1);
set(fh, 'color', 'white');
The contour will be empty because almost everything is 0 and the exceptions that are 1 are only directly against the edges. But at least it will not give invalid index.
  13 件のコメント
Walter Roberson
Walter Roberson 2019 年 4 月 3 日
Attached.
But are you sure that you want to put a charge of 0 at the center point? Why bother, considering that the array starts all 0 (except the edges) ?
Nicholas Gillam
Nicholas Gillam 2019 年 4 月 3 日
編集済み: Nicholas Gillam 2019 年 4 月 3 日
I just put it in the middle cause I wasn't too sure about how the FDM would handle it. Solving the problem by doing normal methods for PDEs, I didn't really need to specify, so you're right by saying why bother. While the graph shown is showing more of what I need, it's still not it, so I'm going to see what I can do with it. Though I'm kind of stumped onto why I'm getting almost no data for x>0.05. Thanks for helping and being patient with me.

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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by