Index exceeds number of array elements (1)?

1,209 ビュー (過去 30 日間)
Riley Vargas 2018 年 11 月 6 日
コメント済み: Mark Linne 2021 年 7 月 30 日
dt = 0.005; N = 200; %(s)
P3s = 2; P3d = 23.5; %(mmHg)
V2(1) = .475; %(L)
V0 = .06; %(L)
C2 = 0.05; %(L/mmHg)
R1 = 30; R2 = 30; %(mmHg*s/L)
P1 = 11; %(mmHg)
P3(1:120) = P3s;
P3(121:200) = P3d;
t = 0:dt:(N-1)*dt;
plot(t, P3)
xlabel('Time (s)'), ylabel('Pressure (mmHg)')
for i = 1:N
P2(i) = (V2(i) - V0) / C2;
if P2(i) > P3(i); Q3(i) = (P2(i) - P3(i)) / R2;
else Q3(i) = 0;
end
Q1(i) = (P1 - P2(i)) / R1;
Q2(i) = Q1(i) - Q3(i);
P2(i+1) = P2(i) + Q2(i)*dt;
end
tp = 0:dt:N*dt;
figure
plot(tp, P2)
xlabel('Time (s)'), ylabel('P2 (mmHg)')
Error is in line 12 " P2(i) = (V2(i) - V0) / C2;"
What is strange about this problem is that the code runs perfectly on my computer, but when transferred to another and ran, it gives the error message. Any ideas?
1 件のコメント表示非表示 なし
Mark Linne 2021 年 7 月 30 日
I have two nested loops:
for i=1:1:NJ
for f=1:1:NJ
Later in the script I need to do a for loop using:
for L=abs(i-f):1:(i+f)
For obvious reasons, the (i+f) generates the error under discussion. Is there a known way to work around this issue (written by a dinosaur who learned Fortran in 1970, still thinks that way, and would prefer to be able to set array sizes myself at the start of the code)?

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

採用された回答

Aquatris 2018 年 11 月 6 日

You only define V2(1) in your code and do not increase the size of the V2 variable. So I think you are missing a code in your for loop which assigns values to V2(i+1).
It probably runs in your computer because your workspace includes a V2 variable with correct size from another script so the code does not yell at you.
4 件のコメント表示非表示 3 件の古いコメント
Aquatris 2019 年 3 月 13 日
In his code he never defined V2(2) so he added a line in his for loop, something like;
for i = 1:N
...
V2(i+1) = *equation*
end

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

その他の回答 (7 件)

sura naji 2020 年 2 月 18 日
clc
clear
close all
% Problem Statement
Npar = 17;
VarLow=0.1;
VarHigh = 35;
%BBBC parameters
N=50; %number of candidates
MaxIter=100; %number of iterations
% initialize a random value as best value
XBest = rand(1,Npar).* (VarHigh - VarLow) + VarLow;
FBest=fitnessFunc(XBest);
GB=FBest;
t = cputime;
%intialize solutions and memory
X = zeros(N, Npar);
F = zeros(N, 1);
for ii = 1:N
X(ii,:) = rand(1,Npar).* (VarHigh - VarLow) + VarLow;
% calculate the fitness of solutions
F(ii) = fitnessFunc(X(ii,:));
end
%Main Loop
for it=1:MaxIter
%Find the centre of mass
%-----------------------
%numerator term
num=zeros(1,Npar);
for ii=1:N
for jj=1:Npar
num(jj)=num(jj)+(X(ii,jj)/F(ii));
end
end
%denominator term
den=sum(1./F);
%centre of mass
Xc=num/den;
%generate new solutions
%----------------------
for ii=1:N
%new solution from centre of mass
for jj=2:Npar
New=X(ii,:);
New(jj)=Xc(jj)+((VarHigh(jj)*rand)/it^2);
end
%boundary constraints
New=limiter(New,VarHigh,VarLow);
%new fitness
newFit=fitnessFunc(New);
%check whether the solution is better than previous solution
if newFit<F(ii)
X(ii,:)=New;
F(ii)=newFit;
if F(ii)<FBest
XBest=X(ii,:);
FBest=F(ii);
end
end
end
% store the best value in each iteration
GB=[GB FBest];
end
%Main Loop
for it=1:MaxIter
%Find the centre of mass
%-----------------------
%numerator term
num=zeros(1,Npar);
for ii=1:N
for jj=1:Npar
num(jj)=num(jj)+(X(ii,jj)/F(ii));
end
end
%denominator term
den=sum(1./F);
%centre of mass
Xc=num/den;
%generate new solutions
%----------------------
for ii=1:N
%new solution from centre of mass
for jj=2:Npar
New=X(ii,:);
New(jj)=Xc(jj)+((VarHigh(jj)*rand)/it^2);
end
hi please can you help me because l have the same problem and the message related to [ New(jj)=Xc(jj)+((VarHigh(jj)*rand)/it^2);] [index exceeds number of array element(1)]?
1 件のコメント表示非表示 なし
Aquatris 2020 年 5 月 31 日
VarHigh variable is a scalar not vector since you only defined it with "VarHigh = 35;" line.
And in the part that gives you the error, you are trying to reach "VarHigh(2)" which does not exist.

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

BAMIDELE JEGEDE 2020 年 5 月 31 日

I have the issue with my code
clear, clc
x = 0:0.01:pi
y = myfunc(x)
plot(x,y)
avg_y = y(1:length(x)-1) + diff(y)/2;
A = sum(diff(x).*avg_yin )
at
avg_y = y(1:length(x)-1) + diff(y)/2;
I honestly don't know what I am doing wrong and it's becoming frustrating
3 件のコメント表示非表示 2 件の古いコメント
Walter Roberson 2021 年 2 月 12 日
v1(1)=u1(1);
You initialize one v1 value
for i=2:n
x1(i)=h*cos(z1(i-1))*v1(i-1) - memo(x1, c1, i);
When i = 3, you access v1(3-1) -> v1(2) . But that does not exist.
y1(i)=h*sin(z1(i-1))*v1(i-1)-memo(y1, c2, i);
z1(i)=h*w1(i-1)-memo(z1, c3, i) ;
end
You keep extending x1 and y1 and z1 in that loop, but you do not extend v1 or w1

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

Sara Babaie 2021 年 2 月 12 日
I need some help with my code...I am getting the same error:
format long
ep=0.8; %Radiative emissivity of the pipe surface
stfb=5.67e-08; %Stefan-Boltzmann constant
q=500; %heat rate
tair=270; %air temperature
tsurr=270; %surrounding temperature
h=10; %convective heat coefficient
d=0.1; %diameter of the pipe
L=0.5; %length of pipe
p=pi;
ft=@(t84)q-(p*d*L(h*(t84-tair)+(ep*stfb(t84^4-tsurr^4))))
a=ft(334)
3 件のコメント表示非表示 2 件の古いコメント
Walter Roberson 2021 年 2 月 12 日
In this case, Sara's error is in forgetting a multiplication symbol. stfb*(t84^4-tsurr^4)

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

Simon Hudon-Labonté 2021 年 3 月 9 日
function [xsol]=NewtonRaphson(Fon,DerFon,SolEst,Erreur,jmax)
iflag=0;
for j=1:jmax
xsoli=SolEst-(Fon(SolEst)/DerFon(SolEst));
if abs((xsoli-SolEst)/SolEst)<Erreur
xsol=xsoli;
iflag=1;
break
end
SolEst=xsoli;
end
if j==jmax && iflag~=1
fprintf('Aucune solution après %i itérations.\n',jmax)
xsol=('Aucune réponse');
end
Hello i am getting the same error as the others in the comment but even with the different answers I cannot find my error. Thank you!
Simon
4 件のコメント表示非表示 3 件の古いコメント
Walter Roberson 2021 年 6 月 3 日
sum =sum +freq(i);
You do not define freq() in the code.

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

Wu Changjin Wu 2021 年 6 月 4 日
can some one help me, I got the same error !
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Display original grayscale image.
subplot(2, 3, 1);
imshow(grayImage)
axis on;
title('Original Gray Scale Image', 'FontSize', fontSize)
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Perform 2D FFTs
fftOriginal = fft2(double(grayImage));
% Move center from (1,1) to (129, 129) (the middle of the matrix).
shiftedFFT = fftshift(fftOriginal);
subplot(2, 3, 2);
scaledFFTr = 255 * mat2gray(real(shiftedFFT));
imshow(log(scaledFFTr), []);
title('Log of Real Part of Spectrum', 'FontSize', fontSize)
subplot(2, 3, 3);
scaledFFTi = mat2gray(imag(shiftedFFT));
imshow(log(scaledFFTi), []);
axis on;
title('Log of Imaginary Part of Spectrum', 'FontSize', fontSize)
% Display magnitude and phase of 2D FFTs
subplot(2, 3, 4);
shiftedFFTMagnitude = abs(shiftedFFT);
imshow(log(abs(shiftedFFTMagnitude)),[]);
axis on;
colormap gray
title('Log Magnitude of Spectrum', 'FontSize', fontSize)
% Get average
midRow = rows/2+1
midCol = columns/2+1
for col = 1 : columns
for row = 1 : rows
radius = sqrt((row - midRow) ^ 2 + (col - midCol) ^ 2);
count(thisIndex) = count(thisIndex) + 1;
end
end
subplot(2, 3, 5:6);
grid on;
title('Average Radial Profile of Spectrum', 'FontSize', fontSize)
1 件のコメント表示非表示 なし
Walter Roberson 2021 年 6 月 4 日
fontSize = 12;
filename = 'FFT.JPG';
if isunix()
filename = 'flamingos.jpg';
end
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
% Display original grayscale image.
subplot(2, 3, 1);
imshow(grayImage)
axis on;
title('Original Gray Scale Image', 'FontSize', fontSize)
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Perform 2D FFTs
fftOriginal = fft2(double(grayImage));
% Move center from (1,1) to (129, 129) (the middle of the matrix).
shiftedFFT = fftshift(fftOriginal);
subplot(2, 3, 2);
scaledFFTr = 255 * mat2gray(real(shiftedFFT));
imshow(log(scaledFFTr), []);
title('Log of Real Part of Spectrum', 'FontSize', fontSize)
subplot(2, 3, 3);
scaledFFTi = mat2gray(imag(shiftedFFT));
imshow(log(scaledFFTi), []);
axis on;
title('Log of Imaginary Part of Spectrum', 'FontSize', fontSize)
% Display magnitude and phase of 2D FFTs
subplot(2, 3, 4);
shiftedFFTMagnitude = abs(shiftedFFT);
imshow(log(abs(shiftedFFTMagnitude)),[]);
axis on;
colormap gray
title('Log Magnitude of Spectrum', 'FontSize', fontSize)
% Get average
midRow = rows/2+1;
midCol = columns/2+1;
for col = 1 : columns
for row = 1 : rows
radius = sqrt((row - midRow) ^ 2 + (col - midCol) ^ 2);
count(thisIndex) = count(thisIndex) + 1;
end
end
subplot(2, 3, 5:6);
grid on;
title('Average Radial Profile of Spectrum', 'FontSize', fontSize)

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

ELIZABETH ESPARZA 2021 年 6 月 5 日
Hi, i have the same error, i need help :(
clear all
%incremento en x
h=0.1;
%funcion 1
f = @(t,a,b,u,v,w) -5*a*u+2*a-4*b;
%funcion 2
g = @(t,a,b,u,v,w) -a*b*v-a+6*b;
%funcion 3
d = @(t,a,b,u,v,w) b*w-a;
%Condiciones Iniciales
ti = 0;
ai = 0;
bi = 0;
ui = 0;
vi = 0;
wi = 00;
%Iteraciones
n = 5;
%Función
[t,a,b,u,v,w] =RungeFG3(f,g,d,n,h,ti,ai,bi,ui,vi,wi)
%Graficación
plot3(t,a,b);grid on;
legend('Runge K4')
title('Gráfica')
xlabel('t')
ylabel('a')
zlabel('b')
tabla1= table (t',a',b');
tabla1.Properties.VariableNames = {'t','a','b'}
function [t,a,b,u,v,w] =RungeFG3(f,g,d,n,h,t0,a0,b0,u0,v0,w0)
t(1) = t0;
a(1) = a0;
b(1) = b0;
u(1) = u0;
v(1) = v0;
w(1) = w0;
for i=1:1:n
t(i+1) = t(i) + h;
k1 = h*f(t(i),a(i),b(i),u(i),v(i),w(i));
l1 = h*g(t(i),a(i),b(i),u(i),v(i),w(i));
e1 = h*d(t(i),a(i),b(i),u(i),v(i),w(i));
m1 = h*u(i);
n1 = h*v(i);
p1 = h*w(i);
k2 = h*f(t(i)+h/2,a(i)+m1/2,b(i)+n1/2,u(1)+k1/2,v(i)+l1/2,w(i)+l1/2);
l2 = h*g(t(i)+h/2,a(i)+m1/2,b(i)+n1/2,u(1)+k1/2,v(i)+l1/2,w(i)+l1/2);
e2 = h*d(t(i)+h/2,a(i)+m1/2,b(i)m2 = h*(u(i)+k1/2);
n2 = h*(v(i)+l1/2);
p2 = h*(w(i)+p1/2);
k3 = h*f(t(i)+h/2,a(i)+m2/2,b(i)+n2/2,u(1)+k2/2,v(i)+l2/2,w(i)+l2/2);
l3 = h*g(t(i)+h/2,a(i)+m2/2,b(i)+n2/2,u(1)+k2/2,v(i)+l2/2,w(i)+l2/2);
e3 = h*d(t(i)+h/2,a(i)+m2/2,b(i)+n2/2,u(1)+k2/2,v(i)+l2/2,w(i)+l2/2);
m3 = h*(u(i)+k2/2);
n3 = h*(v(i)+l2/2);
p3 = h*(w(i)+p2/2);
k4 = h*f(t(i)+h,a(i)+m3,b(i)+n3,u(1)+k3,v(i)+l3,w(i)+l3);
l4 = h*g(t(i)+h,a(i)+m3,b(i)+n3,u(1)+k3,v(i)+l3,w(i)+l3);
e4 = h*d(t(i)+h,a(i)+m3,b(i)+n3,u(1)+k3,v(i)+l3,w(i)+l3);
m4 = h*(u(i)+k3);
n4 = h*(v(i)+l3);
p4 = h*(w(i)+p3);
u(i+1) = u(i)+(k1+2*k2+2*k3+k4)/6;
v(i+1) = v(i)+(l1+2*l2+2*l3+l4)/6;
p(i+1) = w(i)+(p1+2*p2+2*p3+p4)/6;+n1/2,u(1)+k1/2,v(i)+l1/2,w(i)+l1/2);
a(i+1) = a(i)+(m1+2*m2+2*m3+m4)/6;
b(i+1) = b(i)+(n1+2*n2+2*n3+n4)/6;
end
end
5 件のコメント表示非表示 4 件の古いコメント
Aquatris 2021 年 6 月 6 日
That index problem happens because of variable w. You never assigned values to w(i+1) in your code.

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

emre bahçeci 2021 年 6 月 15 日
Hello,i am trying to face same error.Here is my code:
T=1.5
t=0:0.001:T
m=1;b=10;k=100;
K=[1 0 0 0 0 0;
0 1 0 0 0 0;
0 0 2 0 0 0;
1 T T^2 T^3 T^4 T^5;
0 1 2*T 3*T^2 4*T^3 5*T^4;
0 0 2 6*T 12*T^2 20*T^3]
A=inv(K)*[pi/3;0;0;5*pi/3;0;0]
syms theta(x)
theta_dot=diff(theta,x)
theta_dot_dot=diff(theta_dot,x)
Q=m*theta_dot_dot+b*theta_dot+k*theta
for i=1:length(t)
teta(i)=A(1)+A(2)*t(i)+A(3)*t(i)^2+A(4)*t(i)^3+A(5)*t(i)^4+A(6)*t(i)^5
tetah(i)=A(2)+2*A(3)*t(i)+3*A(4)*t(i)^2+4*A(5)*t(i)^3+5*A(6)*t(i)^4
tetai(i)=2*A(3)+6*A(4)*t(i)+12*A(5)*t(i)^2+20*A(6)*t(i)^3
Q_new(i)=subs(Q,[theta_dot_dot theta_dot theta],[tetai tetah teta])
end
After that i have encountered same error:
Index exceeds the number of array elements (1).
Error in sym/subs>normalize (line 212)
Y{i} = fun2sym(Y{i},argnames(X{i}));
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 154)
Error in symbolic_in_for_loop (line 19)
Q_new(i)=subs(Q,[theta_dot_dot theta_dot theta],[tetai tetah teta])
Can anybody help me?
Thanks
2 件のコメント表示非表示 1 件の古いコメント
emre bahçeci 2021 年 6 月 16 日
Thanks a lot

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

Community Treasure Hunt

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

Start Hunting!

Translated by