MATLAB: Error: “Not enough input arguments. Error in f (line 2) y = (x.^3) – 2.” I also want to verify that this code is using the bisection method to estimate a zero until two successive estimates have a diff. less than 10-6.

bisectionbisection method

function y = f(x)
y = (x^3) - 2;
format long
eps_abs = 1e-6;
eps_step = 1e-6;
a = 1.0;
b = 2.0;
while (b - a >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs ) )
c = (a + b)/2;
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
end
[a b]
abs(f(a))
abs(f(b))

Best Answer

You need to have two files for this. One for your function that you intend to find the root of. And another for the script to do the bisection by calling that function. So, first create a file called f.m and have it contain your function:
% File f.m
function y = f(x)
y = (x^3) - 2;
end
Then delete those lines from the top of your script file. E.g., have another file called bisection.m that contains your other code. E.g.,
% File bisection.m
format long
eps_abs = 1e-6;
eps_step = 1e-6;
a = 1.0;
b = 2.0;
while (b - a >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs ) )
c = (a + b)/2;
if ( f(c) == 0 )
break;
elseif ( f(a)*f(c) < 0 )
b = c;
else
a = c;
end
end
[a b]
abs(f(a))
abs(f(b))
Then type the following at the command line to run your script (which will call your function f):
bisection