Matlab Bisection Algorithm code
Ältere Kommentare anzeigen
% I'm trying to create a proper bisection code, and would like some advice on whether this code will...
%run properly. I am not sure how to test it, since every time I write a function on the
%command window, it tells me "x" is an unrecognized variable
function[r,resarray] = bisect(f,a,b,tol,N)
%f is my anonymous function, a and b are my guesses for where the root
%might be, tol if the tolerance(in this case 10^-6) and N is the
%maximum number of iterations
f = f(a);
k = 1;
while (k<= N && abs(f)>tol)
c = 0.5*(a+b);
f = f(c);
if f *f(a) <0
b = c;
else
a = c;
end
k = k+1;
end
r = c;
end
2 Kommentare
Steven Lord
am 24 Feb. 2021
Can you show us how you're trying to call it and the full and exact text of any warning and/or error messages you receive?
I'm guessing you're doing this as part of a homework assignment. Does your textbook have any worked examples that you can try to run using your code to check that you receive the same results?
Nowhere do you assign a value to resarray so if you ever call your function with two outputs it will error.
Aaron Millan
am 24 Feb. 2021
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Matrix Indexing 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!