Why am I getting a parse error trying to put this function in MATLAB?

23 次查看(过去 30 天)
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

回答(2 个)

Walter Roberson
Walter Roberson 2023-9-6
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.

Mrutyunjaya Hiremath
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.

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by