Hello everyone,
I know this is simple but I just can't figure it out by myself now.
I'm trying to create a function integrator, which asks the user to type the function to be integrated. I tried to use
g = input ('Enter function to be integrated: ')
f = @(x) g;
but MATLAB refuses it with Undefined function or variable 'x'. But how can we define x before so that f wouldn't get immediately evaluated?
Thanks a lot in adavnce!

Antworten (2)

per isakson
per isakson am 12 Mai 2012

1 Stimme

Try
string_input_by_user = input( .....
string_input_by_user = 'sin';
foo = str2func( ['@(x)', string_input_by_user, '(x)' ] );
or better
foo = str2func( ['@', string_input_by_user ] );
these two variants work
>> foo(4)
ans =
-0.7568
--- 2010-05-13 ---
Divide and conquer. One thing at a time. What exactly does the user input? Assume the user inputs the tree letter string "sin". Thus, the value of
string_input_by_user
is
'sin'
Then you can create a function_handle
foo = str2func( ['@', string_input_by_user ] );
and make a test
>> foo(pi/2)
ans =
1
>> foo([ 0 : pi/4 : pi+eps] )
ans =
0 0.7071 1.0000 0.7071 0.0000
If the user inputs the six letter string "sin(x)" it's a bit more tricky. You can create a function_handle
foo = str2func( ['@(x)', string_input_by_user ] );
The problem is that the user might input "sin(y)".
Expression
In Matlab "47*x" is an expression not a function. Make your code work with functions first. Then return to expressions.
The Question, Increasing of speed in numerical calculation, shows how to use expressions. However, there is some noise.
btw. Do you step through the code with the debugger and inspect the value of the variable?

4 Kommentare

Nina
Nina am 12 Mai 2012
Thank you! Unfortunately, I don't know why, when I tried this, I still got the error message Undefined function or variable 'x'. Did you define x somehow before?
Walter Roberson
Walter Roberson am 12 Mai 2012
Are you using a very old MATLAB, before there was support for anonymous functions?
Please show your current code.
Nina
Nina am 12 Mai 2012
I'm using R2012a...
But I get the error message after I try
string_input_by_user = input( .....)
after entering the function, no matter is sin(x), 47*x, or anything, I get
Error using input
Undefined function or variable 'x'.
If it did not happen, I would continue with my code with
f = str2func( ['@', string_input_by_user, (x)]);
But it can't get to that point yet
Walter Roberson
Walter Roberson am 12 Mai 2012
string_input_by_user = input( ....., 's')

Melden Sie sich an, um zu kommentieren.

mohammad reza
mohammad reza am 12 Jul. 2016

0 Stimmen

now that you have got a user defined function, how can u integrate the function in the script? (with out limit)

1 Kommentar

Walter Roberson
Walter Roberson am 16 Jul. 2016
Indefinite integration requires the Symbolic Toolbox int() function

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 12 Mai 2012

Kommentiert:

am 16 Jul. 2016

Community Treasure Hunt

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

Start Hunting!

Translated by