Als Beispiel möchte ich hier die Funktion und ihre Ableitung benutzen:

- a, b: Intervall
- eta: Genauigkeit
- format long: 15 Nachkommastellen
//update: GitHub
//update: Nullstellenberechnung in Lua
1. Bisektion (Wikipedia)
function [nullstelle,i]=bisektion(a,b)
format long;
eta = 0.00001;
i=0;
if(f(b)*f(a)<0)
while(abs(b-a)>eta)
if(f((a+b)/2)*f(a)>0)
a=(a+b)/2;
else
b=(a+b)/2;
end
i=i+1;
end
nullstelle=(a+b)/2;
end
function f_x=f(x)
f_x=x.^6-x-1;
2. Newtown-Verfahren
function [nullstelle,i]=newton_verfahren(x)
format long;
eta=0.00001;
diff=1;
i=0;
while(diff>eta)
nullstelle = x;
[f_x,fp_x]=f(x);
x=x-f_x/fp_x;
i=i+1;
diff=abs(x-nullstelle);
end
nullstelle=x;
function [f_x,fp_x]=f(x)
f_x = x.^6-x-1;
fp_x = 6*x.^5-1;
3. Sekanten-Verfahren
function [nullstelle,i]=sekanten_verfahren(a,b)
format long;
eta=0.00001;
i=0;
while(abs(b-a)>eta)
nullstelle=b;
b=b-f(b)*(b-a)/(f(b)-f(a));
a=nullstelle;
i=i+1;
end
nullstelle=(a+b)/2;
function f_x=f(x)
f_x=x.^6-x-1;
4. Regular-Falsi
function [nullstelle,i]=regula_falsi(a,b)
format long;
eta=0.00001;
i=0;
diff=1;
if(f(b)*f(a)<0)
nullstelle = a;
while(diff>eta)
x=(a*f(b)-b*f(a))/(f(b)-f(a));
if(f(x)<0)
a=x;
else
b=x;
end
diff=abs(x-nullstelle);
nullstelle = x;
i=i+1;
end
end
function f_x=f(x)
f_x=x.^6-x-1;
