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!