Why am I getting a parse error trying to put this function in MATLAB?
47 views (last 30 days)
Show older comments
I'm trying to put this function into MATLAB: e^x + x^2 − x − 4
When I do this I get a parse error at exp and I don't understand why: fx = @x exp(x) + x^2 - x - 4
0 Comments
Answers (2)
Walter Roberson
on 6 Sep 2023
fx = @x exp(x) + x^2 - x - 4
In MATLAB, @ followed by a name is a request to create a function handle to a function with the given name. So @x is a request to create a function handle to a function named x
After that, you do not have an kind of operator or separator before you encounter the next term, exp(x) .
I suspect that what you wanted was
fx = @(x) exp(x) + x^2 - x - 4
The () are an important part of the syntax.
0 Comments
Mrutyunjaya Hiremath
on 6 Sep 2023
The issue you're encountering is because you have a syntax error in your MATLAB function declaration. You should define your function using an anonymous function handle properly.
Here's the correct way to define your function:
fx = @(x) exp(x) + x^2 - x - 4;
In MATLAB, you create an anonymous function handle using @ and then specify the variable (x in this case) inside the parentheses. This allows you to use x as the input to your function.
0 Comments
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!