Borrar filtros
Borrar filtros

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

54 visualizaciones (últimos 30 días)
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

Respuestas (2)

Walter Roberson
Walter Roberson el 6 de Sept. de 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.

Mrutyunjaya Hiremath
Mrutyunjaya Hiremath el 6 de Sept. de 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.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by