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

124 次查看(过去 30 天)
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;

采纳的回答

Cris LaPierre
Cris LaPierre 2024-4-15
编辑:Cris LaPierre 2024-4-15
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 个评论
Voss
Voss 2024-4-15
@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;

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by