secant method exceeds the number of array

2 Ansichten (letzte 30 Tage)
Faisal Al-Wazir
Faisal Al-Wazir am 2 Okt. 2022
Bearbeitet: Torsten am 2 Okt. 2022
hi, i'm trying to solve this question but i don't know why i'm getting error every time i run it
here is the qustion part c
clear
clc
fun=@(x) x^3-6*x^2+11*x-6.1;
x=3.5;
c_factor=0.001;
d=@(x) 3*x^2-12*x+11;
for i=1:100
x(i+2)=x(i)-(fun(x(i))*((x(i+1)-x(i))/(fun(x(i+1))-fun(x(i)))));
e(i)=abs((x(i+1)-x(i))/x(i));
if e(i)<c_factor
break
end
end
Index exceeds the number of array elements. Index must not exceed 1.
fprintf('The root of given equation is: %.4f\n', x(i))

Akzeptierte Antwort

Torsten
Torsten am 2 Okt. 2022
Bearbeitet: Torsten am 2 Okt. 2022
x(i+2)=x(i)-(fun(x(i))*((x(i+1)-x(i))/(fun(x(i+1))-fun(x(i)))));
For i = 1, the evaluation of the right-hand side needs x(1) and x(2).
You didn't define them before the loop. Set
x(1) = 2.5;
x(2) = 3.5;
instead of
x=3.5;

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by