error with functions
Show older comments
Hi i keep tryiing to run this function and it keeps giving me an error
function Xs = BisectionRoot( Fun, a, b, To1Max)
Fa = feval(Fun,a);
Fb = feval(Fun,b);
if Fa*Fb > 0
disp('Error: The function has the same sign at points a and b.')
else
n = ceil((log10(b-a) - log10(To1Max))/log10(2));
for i = 1:n
Xs = (a + b)/2;
FXs=feval(Fun,Xs);;
if FXs == 0
break
end
if F(a)*FXs < 0
b = Xs;
else
a = Xs;
Fa = FXs;
end
end
end
??? function Xs = BisectionRoot( Fun, a, b, To1Max) | Error: Function definitions are not permitted in this context.
Please help me
Thanks
Answers (2)
Image Analyst
on 23 Sep 2011
Most likely you have this futher down in an m-file and the top of the m-file is not another named function, but a simple script. You can't do that. Put the name of your m-file at the top after the word function
function mymfile()
% Stuff
secondFunction();
end
function secondFunction()
% More stuff.
end
Walter Roberson
on 23 Sep 2011
0 votes
Store the code in a file named BisectionRoot.m and remove it from where you have it now.
If that still does not work: please check whether you have some tab characters in the file, especially right before the word "function".
2 Comments
Hussein
on 23 Sep 2011
Walter Roberson
on 23 Sep 2011
At the command line, you invoke
BisectionRoot(Fun, a, b, To1Max)
where you have defined Fun, a, b, and To1max appropriately.
I think you will find that you do not want to assign an equation to Fun: you want to assign an expression to Fun. If you currently have something of the form f(x)=c then use the expression f(x)-c instead of an equation.
There are a variety of choices as to how you define your function. See the documentation for feval() to see what forms it accepts.
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!