How to get a function from user without knowing its symbols

조회 수: 1 (최근 30일)
Soheil Khoshboo
Soheil Khoshboo 2020년 11월 23일
댓글: Jan 2020년 11월 25일
Hi everyone I want to create a general program which users insert their desired function and my program do a mathematic operation (like integration) on it and gives the answer. The problem is that i don't know what is the function and its variable, it might be a function of x, or y or z, or there might be some constant variables. For example users may want to solve an integration of A1 + (x/L)*A2, respect to "x" base on A1, A2 and L. Or they want to solve integral of Ax+by respect to y. The problem is I don't know which character user will use, so that i can define it as symbol. How could I do this?

채택된 답변

Steven Lord
Steven Lord 2020년 11월 23일
The symvar and str2sym functions listed (along with many others) on this documentation page may be of interest to you.
  댓글 수: 7
Walter Roberson
Walter Roberson 2020년 11월 24일
When you use str2func() then you get out a function handle for numeric evaluation, and you need to use integral() for numeric functions.
Soheil Khoshboo
Soheil Khoshboo 2020년 11월 25일
I found my mistakes thank you.

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

추가 답변 (1개)

Jan
Jan 2020년 11월 24일
See how e.g. fzero and ode45 solves this problem. Here the function has 1 oder 2 variables which must be provided in a specific order. Constants are delivered to the function using anonymous functions. How the variables are called internally does not matter. The user simply provides the function handle and only the order of variables matter.
  댓글 수: 2
Soheil Khoshboo
Soheil Khoshboo 2020년 11월 24일
편집: Soheil Khoshboo 2020년 11월 24일
can you please write an example, or give me a link?
Jan
Jan 2020년 11월 25일
You finde the explanations in the documentation:
doc fzero
doc ode45

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

카테고리

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

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by