Filter löschen
Filter löschen

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

127 Ansichten (letzte 30 Tage)
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;

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 15 Apr. 2024
Bearbeitet: Cris LaPierre am 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 Kommentare
Cris LaPierre
Cris LaPierre am 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 am 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;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by