Index exceeds the number of array elements (1). Please help :(

%Método de la regla falsa
clear all;
close all;
clc;
syms x
prompt='Ingresa una función x: ';
y=input(prompt);
ai=input('Ingresa a: ');
bi=input('Ingresa b: ');
tole=input('Ingresa la tolerancia: ');
tol=double(tole);
ans1=subs(y,x,ai);
fa=double(ans1);
ans2=subs(y,x,bi);
fb=double(ans2);
if(fa*fb<1)
a(1)=ai;
ans1=subs(y,x,ai);
fa=double(ans1);
b(1)=bi;
ans2=subs(y,x,bi);
fb=double(ans2);
xn(1)=b(1)-((fb*(a(1)-b(1)))/(fa-fb));
f1=subs(y,x,xn(1));
fx0=double(f1);
i=1;
eabs(i)=0;
for i=0:1:eabs(i+1)<=tol
if fx0<0
a(i+1)=xn(i);
ansa=subs(y,x,a(i+1));
fa=double(ansa);
b(i+1)=b(i);
ansb=subs(y,x,b(i+1));
fb=double(ansb);
else
a(i+1)=a(i);
ansa=subs(y,x,a(i+1));
fa=double(ansa);
b(i+1)=xn(i);
ansb=subs(y,x,b(i+1));
fb=double(ansb);
end
xn(i+1)=b(i+1)-((fb*(a(i+1)-b(i+1)))/(fa-fb))
eabs(i+1)=abs(xn(i+1)-xn(i))
i=i+1
end
else
fprintf('Ingresa un nuevo intervalo');
end
I have the error:
Ingresa una función x: sin(x)+1-x^2
Ingresa a: 1
Ingresa b: 2
Ingresa la tolerancia: 0.001
Index exceeds the number of array elements (1).
Error in mtl02 (line 30)
for i=0:1:eabs(i+1)<=tol
And idk how to solve it, that's my condition, and if i put "eabs(i)<=tol" i can´t have more than two iterations :(

4 Comments

before for loop
eabs(i)=0;
eabs(i+1) is not defined anywhere in your code
You'll need to arrange the test to work on the present and preceding (if needed) eabs value after the new one has been computed.
for i=0:1:eabs(i+1)<=tol
...
eabs(i+1)=abs(xn(i+1)-xn(i))
the for loop is set up to run in integer increments by 1 to eabs(i+1)<=tol
But, even if eabs(i+1) were defined, the logical condition will be either F (0) or T (1) so the maximum the loop would run would be 0:1:1 or twice.
It looks like you need a while loop here instead of counted for
I already used "while", but it's the same, I only have two iterations but with eabs(i)
%Método de la regla falsa
clear all;
close all;
clc;
syms x
prompt='Ingresa una función x: ';
y=input(prompt);
ai=input('Ingresa a: ');
bi=input('Ingresa b: ');
tole=input('Ingresa la tolerancia: ');
tol=double(tole);
ans1=subs(y,x,ai);
fa=double(ans1);
ans2=subs(y,x,bi);
fb=double(ans2);
i=1;
eabs(i)=0;
eabs(i+1)=0;
if(fa*fb<1)
a(1)=ai;
ans1=subs(y,x,ai);
fa=double(ans1);
b(1)=bi;
ans2=subs(y,x,bi);
fb=double(ans2);
xn(1)=b(1)-((fb*(a(1)-b(1)))/(fa-fb));
f1=subs(y,x,xn(1));
fx0=double(f1);
fprintf('It. a b xn Error abs \n');
fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \n',i,a(i),b(i),xn(i));
while eabs(i+1)>tol
if fa*fx0<0
a(i+1)=xn(i);
ansa=subs(y,x,a(i+1));
fa=double(ansa);
b(i+1)=b(i);
ansb=subs(y,x,b(i+1));
fb=double(ansb);
else
a(i+1)=a(i);
ansa=subs(y,x,a(i+1));
fa=double(ansa);
b(i+1)=xn(i);
ansb=subs(y,x,b(i+1));
fb=double(ansb);
end
xn(i+1)=b(i+1)-((fb*(a(i+1)-b(i+1)))/(fa-fb));
eabs(i+1)=abs(xn(i+1)-xn(i));
fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \t %7.3f \n',i+1,a(i+1),b(i+1),xn(i+1),eabs(i+1));
i=i+1;
end
else
fprintf('Ingresa un nuevo intervalo');
end
And now i have only one interation (the frist one):
Ingresa una función x: sin(x)+1-x^2
Ingresa a: 1
Ingresa b: 2
Ingresa la tolerancia: 0.001
It. a b xn Error aprox
1 1.0000000 2.0000000 1.2869786
But you initialized to zero which would be perfect convergence for starters...

Sign in to comment.

Answers (0)

Products

Release

R2019a

Commented:

dpb
on 22 Sep 2019

Community Treasure Hunt

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

Start Hunting!