error with functions
显示 更早的评论
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
回答(2 个)
Image Analyst
2011-9-23
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
2011-9-23
0 个投票
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 个评论
Hussein
2011-9-23
Walter Roberson
2011-9-23
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!