MATLAB Answers

Index exceeds number of array elements (1)?

2.542 views (last 30 days)
Riley Vargas
Riley Vargas on 6 Nov 2018
Edited: Aquatris on 31 May 2020
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?

  0 Comments

Sign in to comment.

Accepted Answer

Aquatris
Aquatris on 6 Nov 2018
Edited: Aquatris on 6 Nov 2018
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 Comments

Show 1 older comment
Lykha ahmed
Lykha ahmed on 30 Jan 2019
could you tell me how you added?
Aquatris
Aquatris on 13 Mar 2019
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

Sign in to comment.

More Answers (2)

sura naji
sura naji on 18 Feb 2020
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 Comment

Aquatris
Aquatris on 31 May 2020
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.

Sign in to comment.


BAMIDELE JEGEDE
BAMIDELE JEGEDE on 31 May 2020
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

  1 Comment

Aquatris
Aquatris on 31 May 2020
I replaced myfunc with sin function and it does not give any error at line "avg_y = y(1:length(x)-1) + diff(y)/2;"
However "A = sum(diff(x).*avg_yin )" this one gives error since avg_yin is not defined in the code you gave.
What is the exact error message you are getting?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by