Matlab Bisection Algorithm code
이전 댓글 표시
% 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
Steven Lord
2021년 2월 24일
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
2021년 2월 24일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!