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

조회 수: 54 (최근 30일)
Isaac Smith
Isaac Smith 2023년 9월 6일
답변: Mrutyunjaya Hiremath 2023년 9월 6일
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
Mrutyunjaya Hiremath 2023년 9월 6일
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에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by