Index exceeds the number of array elements (1). Please help :(
Show older comments
%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
G A
on 21 Sep 2019
before for loop
eabs(i)=0;
eabs(i+1) is not defined anywhere in your code
dpb
on 21 Sep 2019
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
Mariana Guzmán Ramírez
on 21 Sep 2019
dpb
on 22 Sep 2019
But you initialized to zero which would be perfect convergence for starters...
Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!