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 commentaire

Hussein
Hussein le 23 Sep 2011
The goal of this function is to find the solution of any nonlinear equation by using the bisection method

Connectez-vous pour commenter.

Réponses (2)

Image Analyst
Image Analyst le 23 Sep 2011

0 votes

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 commentaire

Hussein
Hussein le 23 Sep 2011
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

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 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 commentaires

Hussein
Hussein le 23 Sep 2011
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 ?
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Centre d'aide et File Exchange

Question posée :

le 23 Sep 2011

Community Treasure Hunt

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

Start Hunting!

Translated by