What is wrong with my code for the bisection method!!?
Ältere Kommentare anzeigen
So I have this code I need too use on my a function.
I have made m. function file with the function I want to calculate.
f=@(x)(x-2.5).*exp(-0.5*(x-2).^2)+0.2;
x=my_bisect(f,[-1,0],1e-5);
m =(a+b)/2;
disp(m);
And in another file I have this code:
function m=my_bisect(funktion, int , tol )
a = int (-1);
b = int (0);
fa =funktion(a) ;
fb =funktion(b) ;
disp(fa)
disp(fb)
msg=sprintf('??? error.');
while b-a > tol
m = (a+b)/2;
fm = funktion(m);
if fm==0
disp(msg)
return
end
if fa * fm < 0
b = m;
fb = fm;
else
a = m;
fa = fm;
end
end
When I try running function file to find m Matlab returns: Attempted to access int(-1); index must be a positive integer or logical.
What is wrong?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!