Why am I getting a parse error trying to put this function in MATLAB?
47 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Walter Roberson
am 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 Kommentare
Mrutyunjaya Hiremath
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!