How to evaluate a user input function at a given point?

조회 수: 3 (최근 30일)
Maryam Ansari
Maryam Ansari 2016년 2월 15일
댓글: Walter Roberson 2016년 2월 15일
I'm unsure how to evaluate a given function at a given point X. Hypothetically, the user can input any function using any letter as a desired variable. How can I evaluate the function at the initial point given. I was thinking I could use the syms command. However, I'm unsure of what letter variable they would use. For instance, if the function was x+1 vs y+1, how can I ensure that the "initialX" is evaluated in place of x or y in the given function?
function y = randFunc(initialX, func)
y = func(initialX);
display(y);

채택된 답변

Matt J
Matt J 2016년 2월 15일
편집: Matt J 2016년 2월 15일
If func is a Function Handle, then the code you've shown should do exactly what you want.
  댓글 수: 3
Matt J
Matt J 2016년 2월 15일
편집: Matt J 2016년 2월 15일
There are various ways, and they are explained at the link I gave you.
Walter Roberson
Walter Roberson 2016년 2월 15일
In this case if func is given in the form of a symbolic expression, then use matlabFunction() to turn it into a function handle.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by