Input error - finding roots of functions

조회 수: 6 (최근 30일)
Lemons
Lemons 2013년 3월 22일
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

답변 (1개)

Matt J
Matt J 2013년 3월 22일
편집: Matt J 2013년 3월 22일
You forgot the '@'
root_bisect(@funcB,1,10)
  댓글 수: 2
Lemons
Lemons 2013년 3월 22일
Would've been awesome if the professor explained that to me hahaah. Thanks a bunch!
Matt J
Matt J 2013년 3월 22일
편집: Matt J 2013년 3월 22일
You're welcome. Do you know about Accept-clicking?

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

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by