I Get an error: Subscript indices must either be real positive integers or logicals
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
The code is:
clear all %variables from memory
close all %opened plots
v0=100; % potetial value in the boundary edges of the inner square
% ************* start Inputs ********************************************
a=input('Enter size of a - [outer square size]:') ;
b=input('Enter size of b Inner rhombus size (a>b]:') ;
dx=input('Input dx - Number of samples (an even number): ');
% Number of Iteration
iteration=input('Input number of iteration: ');
da=-a/2:a/dx:a/2; % vector da - length of the outer square with steps
db=-b/2:a/dx:b/2; % vector db - length of the inner rhombus with steps
aa=[1:1:length(da)]; % vector aa - contains the nuber of steps in vector da
bb=[1:1:length(da)]; % vector bb - contains the nuber of steps in vector db
[x,y] = meshgrid(aa,bb);
% transforms the domain specified by vectors aa and bb into arrays % x and y. The rows of the output array x are copies of the vector
% aa and the columns of the output array y are copies of the vector bb. % This array will be use as the coordinte system
% and will be fill by the boundary condition and
% the potential results
boundary=zeros(size(x));
% Filling the boundary conditon to the array (the name of the array which contains the boundary condition is "boundary")
for i=1:((length(db)+1)/2)
boundary((length(da)+1)/2+i-1,(length(da)+1)/2-(length(db)-1)/2+i-1:(length(da)+1 )/2+(length(db)-1)/2-i+1)=v0;
boundary((length(da)+1)/2-i+1,(length(da)+1)/2-(length(db)-1)/2+i-1:(length(da)+1 )/2+(length(db)-1)/2-i+1)=v0;
end
% P is the array which will contains the potential results a the first of
% the calculation procedure P is equal to the boundary array
P = boundary;
% calculation of the potential as an average of 8 neighbors points and the
% point itself total of 9 points).
for j = 1: iteration
% outer loop - the calculation will be performed several times (iteration number).
for m= 2 : length(da)-1
% middle loop - changing y coordinate so the inner loop is now calculating for
% diffrent coulom
for n = 2 : length(da)-1
% inner loop - x coordinate is varible while y coordinte is fixed
if (boundary(m,n) ~=v0) % if condition inorder to insure that the calculation will be made only at the space between the 2 squars and not insidethe inner rhombus
P(m,n) = (P(m,n)+P(m-1,n)+P(m+1,n)+P(m,n-1)+P(m,n+1)+P(m-1,n-1)+P(m+1,n-1)+P(m-1,n+1)+P(m+1,n+1))/9;
% Potential calculation
end
end
end
end
% ******************* Output graphs *************************************
figure(1)
imagesc(da,da,boundary);
colorbar;
grid;
xlabel('Axis x');ylabel('Axis Y');
title('image plot of the boundary condition vs. x,YT')
axis([-a/2 a/2 -a/2 a/2]);
figure(2)
imagesc(da,da,P);
colorbar;
grid;
xlabel('Axis x');ylabel('Axis Y');
title('image plot of the potential vs. x,Y1');
axis([-a/2 a/2 -a/2 a/2]);
[P_x,P_y] = gradient(-P,dx,dx);
figure(3);
quiver(da,da,P_x,P_y,1);
grid;
xlabel('Axis x');ylabel('Axis Y');
title('Electric field vector vs. xof');
axis([-a/2 a/2 -a/2 a/2]);
figure(4);
contour(da,da,P,dx)
grid;
colorbar;
xlabel('Axis X');ylabel('Axis Y');
title('contour plot of the potential vsm x,Y1');
axis([-a/2 a/2 -a/2 a/2]);
The error is:
Warning: Integer operands are required for colon operator when used as index
> In Untitled2 at 41
??? Subscript indices must either be real positive integers or logicals.
Error in ==> Untitled2 at 41
boundary((length(da)+1)/2+i-1,(length(da)+1)/2-(length(db)-1)/2+i-1:(length(da)+1
)/2+(length(db)-1)/2-i+1)=v0;
2 Kommentare
Oleg Komarov
am 21 Mai 2012
Please, format your code as shown in http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup (especially code)
Jan
am 22 Mai 2012
As usual I mention that "clear all" does *not* only remove all vbariables from the workspace, but even all loaded functions including their persistently stored variables. Reading and initializing them again wastes a lot of time. Therefore "clear all" is useful only, if you have modified *all* M-files dynamically during a program runs and you need to reload the modified versions. Because this is needed hardly ever, I'd like to see an MLint warning for "clear all" and a suggestion to use "clear variables" instead. An even better method to keep the workspace clean is to use a function.
Antworten (1)
Walter Roberson
am 21 Mai 2012
At the command line, give the command
dbstop if error
and then run your program. When the program stops running at the line that has the error, examine the value of the expressions you are using to index "boundary". You will probably find that the expression in the second index position, the one with the ":", has non-integer values.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!