How to get a function from user without knowing its symbols

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?

 Accepted Answer

The symvar and str2sym functions listed (along with many others) on this documentation page may be of interest to you.

7 Comments

Thanks for help. But I am looking for a way to ask users insert their symbols and function. For example: Insert your symbols: User will insert --> a x y
Insert your function: User will insert ---> ax+y Then my program calculates integral of above function respect to x in symbolic. How to do that?
list_of_variables = sym( regexp(UserVariableResponse, '[\s,;]+', 'split'))
wow cool, it works like a charm. but how could I use these variables as an input for a function? sorry I am newbie 😅
user_symbol_str = input('Insert your symbols: ', 's');
list_of_variables = sym( regexp(user_symbol_str, '[\s,;]+', 'split'));
user_function_str = input('Insert your function: ', 's');
user_function_sym = str2sym(user_function_str);
But where is your interface to indicate which variable to integrate with respect to, and where is your interface to indicate the bounds of integration ?
Also, if you have
user_function_str = input('Insert your function: ', 's');
user_function_sym = str2sym(user_function_str);
list_of_variables = symvar(user_function_sym);
The difference between this and the first one is that the first one permits the user to input symbols that are not present in the function, such as symbols that will be used as the bounds of integration. But then you still have the question of what your interface is for indicating the bounds of integration.
it's ok, I can also use str2func for my function, right? but how can I do integration? because when I run this and then
Integration = int(user_function_str,x)
it says that x is undefined, while user insert it, it stores in list of variables
When you use str2func() then you get out a function handle for numeric evaluation, and you need to use integral() for numeric functions.

Sign in to comment.

More Answers (1)

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 Comments

can you please write an example, or give me a link?
You finde the explanations in the documentation:
doc fzero
doc ode45

Sign in to comment.

Categories

Products

Release

R2015b

Asked:

on 23 Nov 2020

Commented:

Jan
on 25 Nov 2020

Community Treasure Hunt

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

Start Hunting!