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

1 个评论

The goal of this function is to find the solution of any nonlinear equation by using the bisection method

请先登录,再进行评论。

回答(2 个)

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

1 个评论

Thanks for the answer, but im kinda new to matlab so i dont exactly understand what you are saying. can you explain me how to write a function then call it ? or please give me a link that explains that.
Thanks

请先登录,再进行评论。

Walter Roberson
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 个评论

ok i took this function and i saved it as BisectionRoot.m in a script file, now i want to assign values for a, b, and To1Max and i want to assign an equation to Fun , how can i do that ?
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 的更多信息

标签

提问:

2011-9-23

Community Treasure Hunt

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

Start Hunting!

Translated by