Undefined function 'BisectionRoot' for input arguments of type 'function_handle'.
Afficher commentaires plus anciens
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 commentaires
James Tursa
le 15 Sep 2020
Please show us the code you are using for calling BisectionRoot. Do you have your BisectionRoot code in a file called BisectionRoot.m somewhere on the MATLAB path?
Jaycie Bishop
le 15 Sep 2020
Jaycie Bishop
le 15 Sep 2020
Jaycie Bishop
le 15 Sep 2020
Réponses (0)
Catégories
En savoir plus sur Debugging and Analysis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

