Undefined function 'BisectionRoot' for input arguments of type 'function_handle'.

조회 수: 8 (최근 30일)
Jaycie Bishop
Jaycie Bishop 2020년 9월 15일
댓글: Jaycie Bishop 2020년 9월 15일
Code from part 1 of problem (works fine):
%% From 3.2 %%
% Bisection Method
high = 0; %given initial value
low = 1; %given initial value
f = @(x) x - 2*exp(-x); %function
mid = (high + low)/2; % guess
it = 1; %iteration
tol = 0.001; %tolerance
it_max = 5; %max iterations
x1 = mid;
x2 = 0;
x3 = 0;
x4 = 0;
while (mid > tol && it < it_max)
f_mid = f(mid);
if f_mid < 0
high = mid;
else
low = mid;
end
mid = (high + low) / 2;
if it == 1
x2 = mid;
elseif it == 2
x3 = mid;
elseif it == 3
x4 = mid;
elseif it == 4
x5 = mid;
end
it = it + 1;
end
x_bisection = [x1 x2 x3 x4];
%%Code from Current Problem%%
%% Probelm 3.16 %%
function Xs = BisectionRoot(Fun, a, b, tol) %% <---WHERE THE ERROR IS
fa = Fun(a);
fb = Fun(b);
if fa*fb > 0
Xs = ('Error')
else
n = ceil((log10(b-a)-log10(tol))/log10(2)); %calculating # of iterations
for i = 1:n
Xs = (a+b)/2;
f_Xs = Fun(Xs);
if f_Xs == 0
break
end
if fa*f_xs < 0
b = Xs;
else
a = Xs;
fa = f_Xs;
end
end
end
end
  댓글 수: 4
Jaycie Bishop
Jaycie Bishop 2020년 9월 15일
This is being done on "MatlabGrader" and there is no where (that I see) that I can be inputting a file in the path like I would if I was regularly coding on matlab. So I think I need to adjust the code in order to not call a file.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by