Input error - finding roots of functions
6 次查看(过去 30 天)
显示 更早的评论
I'm fairly new to matlab (only used it about 5-6 times) and am having trouble with a specific program I set up. On Wednesday it seemed to be working fine but now every time I try and use it I keep on getting the same error for every function I link it to.
Here is the text to the root finder (using the bisection method)
function xr = root_bisect (func, xl, xu)
%This program is designed to find the approximate roots of any function
% using the Bisection Method
%Variables
% xl = lower bound guess
% xu = upper bound guess
% xr = approximate root
% xrold = previous value of xr
% yl = value of the function at the lower limit
% yu = value of the function at the upper limit
% Ea = error value
% Es = target error (1x10^-6)
%Prevent user from putting in two x values resulting in y values of the
% same sign
if (sign(feval(func,xl)) == sign(feval(func,xu)))
error('no root inside bracket')
end
%Setting target error
Es = (10^-6);
%Setting the initial xr
xr = xl;
%set initial error value to please Matlab...
Ea = (10^12);
%counting number of loops
counter = 0;
%Begin the while loop to run until Ea <= Es
while (Ea > Es);
%set matlab to record the previous value of xr for error
% calculations
xrold = xr;
%set the value of xr to bisect the interval
xr = ((xl + xu)/2);
%evaluate the functions at both guesses
y1 = feval (func, xl);
y2 = feval (func, xu);
y3 = feval (func, xr);
if ((y1)*(y3) < 0);
%guess is too high
xu = xr;
elseif ((y1)*(y3) > 0);
%guess is too low
xl = xr;
else ((y1)*(y2) == 0);
%root found
fprintf ('xr = %g \n');
break
end
%Defining error function
Ea = abs((xr - xrold)/xr);
%counting number of loops
counter = counter + 1 ;
end
%printing number of loops to screen
fprintf ('number of loops needed to complete calculation counter = %d \n', counter)
end
But the error isn't showing up in there, it shows up when I link it to a function such as
function y = funcB (x)
y = -2+6*x-(4*(x^2))+(0.5*(x^3));
end
When I do this I get the error
>> root_bisect (funcB,1,10)
Error using funcB (line 2)
Not enough input arguments.
Any help/advice would be appreciated! Thanks
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!