Clear Filters
Clear Filters

Error: Index exceeds the number of array elements. Index must not exceed 1.

115 views (last 30 days)
Index exceeds the number of array elements. Index must not exceed 1.
Error in bisectioniterations (line 35)
xr(i)=(xu(i)+xl(i))/2;
Unsure why this error is coming up in my code for bisection method of iteration.
clc;
clear all;
close all;
%Mini Project
%Bisection Method
syms f(x);
f(x)=(-4.868e-7)*(4*x^3)+(1.496e-4)*(3*x^2)+(-0.0094)*(2*x)+(-0.3324);
i=1;
xl(i)=-200;
xu(i)=200;
xr(i)=(xl(i)+xu(i))/2;
if f(xr(i))~=0
while i<100
i=i+1;
if f(xl(i-1))*f(xr(i-1))<0
xu(i)=xr(i-1);
fu(i)=f(xr(i-1));
else
xl(i)=xr(i-1);
fl(i)=f(xr(i-1));
end
xr(i)=(xu(i)+xl(i))/2;
if i>1
ea=abs((xr(i)-xr(i-1))/xr(i))*100;
if ea<0.05
break;
end
end
i=i+1;
end
end
Index exceeds the number of array elements. Index must not exceed 1.
figure(1)
plot(i,ea)
hold on
grid on
xlabel('Iteration')
ylabel('Percent Approximation Error')
y=-200:10:200;
figure(2)
hold on
grid on
box on
for i=1:1:length(y)
plot(y,f(y),'LineWidth',2)
end
plot(xr,f(xr),'*','MarkerSize',10);
xlabel('Temperature (K)')
ylabel('Derivative of f(T)')
yline(0);
r_bisection=(-4.868e-7)*(xr^4)+(1.496e-4)*(xr^3)+(-0.0094)*(xr^2)+(-0.3324*xr)+14.55;
et=abs((-13.2493-xr)/-13.2493)*100;

Accepted Answer

Cris LaPierre
Cris LaPierre on 15 Apr 2024
Edited: Cris LaPierre on 15 Apr 2024
The error means your are trying to index an element of your array that does not exist. The error is telling you that your array only contains 1 element, so your index is >1.
Here, xu and xl both start as scalars. Your if statement adds an ith value to only one of the vectors, so at any given time, one will have i elements, and the other can only have at most i-1 elements. However, the line of code throwing the error assumes they both have i elements.
You also index i twice - once at the top of your while loop, and again at the bottom.
I had to add som '.^' to your r_bisection equation to address a new error.
You will need to update your code to fix these issues. Here's a first pass:
clc;
clear all;
close all;
%Mini Project
%Bisection Method
syms f(x);
f(x)=(-4.868e-7)*(4*x^3)+(1.496e-4)*(3*x^2)+(-0.0094)*(2*x)+(-0.3324);
i=1;
xl(i)=-200;
xu(i)=200;
xr(i)=(xl(i)+xu(i))/2;
if f(xr(i))~=0
while i<100
i=i+1;
if f(xl(i-1))*f(xr(i-1))<0
xu(i)=xr(i-1);
fu(i)=f(xr(i-1));
xl(i)=0;
fl(i)=0;
else
xl(i)=xr(i-1);
fl(i)=f(xr(i-1));
xu(i)=0;
fu(i)=0;
end
xr(i)=(xu(i)+xl(i))/2;
if i>1
ea=abs((xr(i)-xr(i-1))/xr(i))*100;
if ea<0.05
break;
end
end
% i=i+1;
end
end
figure(1)
plot(i,ea)
hold on
grid on
xlabel('Iteration')
ylabel('Percent Approximation Error')
y=-200:10:200;
figure(2)
hold on
grid on
box on
for i=1:1:length(y)
plot(y,f(y),'LineWidth',2)
end
plot(xr,f(xr),'*','MarkerSize',10);
xlabel('Temperature (K)')
ylabel('Derivative of f(T)')
yline(0);
r_bisection=(-4.868e-7)*(xr.^4)+(1.496e-4)*(xr.^3)+(-0.0094)*(xr.^2)+(-0.3324*xr)+14.55;
et=abs((-13.2493-xr)/-13.2493)*100;
  3 Comments
Cris LaPierre
Cris LaPierre on 15 Apr 2024
xr(i) and xr(i-1) are 0, and 0/0 returns NaN.
i=2;
xr = [0 0];
ea=abs((xr(i)-xr(i-1))/xr(i))*100
ea = NaN
Voss
Voss on 15 Apr 2024
@Lily: I imagine you should make xl(i) equal to xl(i-1) (not zero) where needed, similarly for xu, and make ea a vector (i.e., store ea(i) each iteration), and it doesn't seem like you need fu or fl at all. Something like this:
clc;
clear all;
close all;
%Mini Project
%Bisection Method
syms f(x);
f(x)=(-4.868e-7)*(4*x^3)+(1.496e-4)*(3*x^2)+(-0.0094)*(2*x)+(-0.3324);
i=1;
xl(i)=-200;
xu(i)=200;
xr(i)=(xl(i)+xu(i))/2;
if f(xr(i))~=0
while i<100
i=i+1;
if f(xl(i-1))*f(xr(i-1))<0
xu(i)=xr(i-1);
xl(i)=xl(i-1); % <-- use previous xl, not 0
else
xl(i)=xr(i-1);
xu(i)=xu(i-1); % <-- use previous xu, not 0
end
xr(i)=(xu(i)+xl(i))/2;
% if i>1 % <-- i is always > 1 by this point, no need to check it
ea(i)=abs((xr(i)-xr(i-1))/xr(i))*100; % <-- make ea a vector so you can plot it properly
if ea(i)<0.05 % <-- use ea(i) here
break;
end
% end
% i=i+1;
end
end
figure(1)
plot(ea) % <-- plots ea against 1:numel(ea)
hold on
grid on
xlabel('Iteration')
ylabel('Percent Approximation Error')
y=-200:10:200;
figure(2)
hold on
grid on
box on
for i=1:1:length(y)
plot(y,f(y),'LineWidth',2)
end
plot(xr,f(xr),'*','MarkerSize',10);
xlabel('Temperature (K)')
ylabel('Derivative of f(T)')
yline(0);
r_bisection=(-4.868e-7)*(xr.^4)+(1.496e-4)*(xr.^3)+(-0.0094)*(xr.^2)+(-0.3324*xr)+14.55;
et=abs((-13.2493-xr)/-13.2493)*100;

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!