I am trying to create a function with Matlab V6.0.0.88 and it is not working.

I tried for example using the following code given in the wikipedia page about the secant method
f=@(x) x^2 - 612;
x(1)=10;
x(2)=30;
for i=3:7
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
end
root=x(7)
And I receive the following error message
??? f=@(x) x^2 - 612;
|
Error: "identifier" expected, "(" found.
I have tried a few things, but I am unable to create a function. Even the following code, who is part of the Matlab help menu, found while searching for 'function', does not work
function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
This piece of code returns the error
??? Strings passed to EVAL cannot contain function declarations.
Can anyone help me with this? Thank you!

2 Comments

Today I've selected your code and press the "{} Code" button to improve the readability of the question. Please do this by your own the next time. Thanks.
Thank you, I did not know I could do that!

Sign in to comment.

 Accepted Answer

Matlab v6.0? R12, released in Nov 2000? This is old.
Anonymous function have been introduced later.
You did not mention, when the error occurs. Note the "Strings passed to EVAL..." sounds like you have passed this string to eval. Perhaps you have selected the code and activated a menu to run the selected code? If so: don't do this. Run the code by saving the function and calling it from the command window.

9 Comments

I have no idea when the error occurs. Merely running the aforementionned code in the command window results in the error message. What do you mean by activating a menu to run the code? I wouldn't even know how to do that. As for the first part, I have found a solution already. Using
f=inline('x^2 - 612');
x(1)=10;
x(2)=30;
for i=3:7
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
end
root=x(7)
Gives the expected result.
You do not have an idea, when the error occurs? Simply explain, what you do before the message appears. Where do you type what or click on which item?
You cannot define functions in the command window.
I do not have such an old Matlab version running, therefore I cannot try, what you are potentially doing. Therefore we need more details from you.
Anonymous functions were introduced in release R14 (note: not release R2014a or R2014b, but a release that predates the R<year><a or b> numbering system.) Release R14 was released in June 2004, so it's a teenager.
If you can't access those release notes, you can see that the ability to use anonymous functions is one of the features called out for that release in the Release History section of the Wikipedia page for MATLAB.
You cannot use anonymous functions in release R12 or in any release prior to release R14. As Jan said you also cannot define a function by typing the definition in the Command Window (or copying and pasting it into the Command Window.) You must define the function by writing it as a file with the .m extension (and whose name without the extension satisfies the isvarname function.) You can then call the function in the Command Window just like you'd call any of the existing MATLAB functions like length, sum, sqrt, etc.
@Jan Simon I was indeed typing that in the command window.
@Steven Lord I see. I did create a .m file and then created a vector ( w in this case) and then typed stat(w), which does work, but it only returns the mean. Can you now help me get the full result? To make sure everything is clear, I copied pasted the code in the .m file, which I named stat
function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
I then typed the following in the command window
w=[3,10,8];
stat(w)
ans =
7
Also why is it that you cannot define a function in the command window? Big time newbie here, I have used a little bit of Matlab in the past but am not really knowledgeable in such details.
When you call a function with no output arguments, and the function can return output arguments, the first output is returned in a variable named ans. Call your function with two output arguments.
[banana, pyramid] = stat(w);
Note that the variable names you specify as output do not need to be the same as the variable names used in the definition of the function. As long as they're valid variable names, you're fine.
Beautiful! I would much rather have the function always return its expected outputs, but you can't have everything in life for free ;) I thank you a lot!
Now the only question that remains to be answered for me is the comment you made about not being able to define a function in the command window. Why is that?
Why? A good question. A function must be stored anywhere, such that it can be called from other functions or from the command line. The contents of the command window is not stored and not accessible from anywhere. Well, there is the history or diary, but this cannot be used to find or run a function.
But this does not answer, why this is not allowed. I claim, that it is not allowed, because Matlab is not the C64 basic interpreter. :-)
Because we did not implement the ability to do that. Let's perform a thought experiment about how MATLAB would have behaved if we had implemented that ability.
You execute this block of text in the Command Window to define a function named myfun. Note this is not in a file named myfun.m but was somehow defined in the Command Window.
function y = myfun(x)
y = 2*x;
  • What would which myfun say about where the myfun function's definition is located?
  • Would it be on the MATLAB search path? [Note: that is the documentation for the most recent release. Some of that information may not be applicable to release R12.] If so, where on the search path would it be located?
  • Where would it fall in the function precedence order [Note: this also has changed since release R12.]
  • Would I be able to edit myfun?
  • Could I modify myfun after it was defined? This is related to but distinct from the edit question.
  • Could I set a breakpoint inside myfun to debug it?
  • Could I have the myfun function persist across MATLAB sessions or would I have to execute that code every time I started a new MATLAB session?
  • What if I pressed Ctrl-C to interrupt entering that function before it was fully entered? Would it be defined and able to be called (at least as much as I had entered up to the interruption), or would that interruption have abandoned the function definition?
Some of those questions we might be able to answer in a way that makes sense. Others probably would have led to functionality like the search path being much more complicated to implement, test, document, and use than they already are.
Your answers have been incredibly precise and well-written. I thank you for taking the time to answer my questions. You have responded to every of my questions and then given me more.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 22 Aug 2017

Commented:

on 24 Aug 2017

Community Treasure Hunt

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

Start Hunting!