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!