symbolic evaluation where function is obtained from user input text

2 visualizaciones (últimos 30 días)
Hello,
I'm an undergrad student taking a numerical methods course. The material is fairly dry so i try make it more interesting by implementing each method as a script in matlab - i am a matlab novice. (I'm also lazy so i want to save the effort doing this by hand!). I'm currently trying to code the trapezoidal integration method but i've run into a small problem. So far my code prompts the user to input the function, the number of subintervals to use, the upper and lower bounds of the integral and the number of significant digits the question asks to round to.
prompt = 'input function (of x): ';
f = input(prompt,'s');
prompt = 'input n: ';
n = input(prompt);
prompt ='input upper bound b: ';
b = input(prompt);
prompt = 'input lower bound a: ';
a = input(prompt);
prompt = 'enter significant digits required: ';
sig = input(prompt);
display('h is: ')
h = round(((b-a)/(n)),sig);
display(h)
syms x;
actual = double(int(f,x,a,b));
actual = round(actual,sig);
Next i want to calculate the approximation using the Trapezoidal method. I first thought of doing something like this:
T = symsum(0.5(f(xk-1)+f(xk))*h, k, 1, n);
but the user has entered the function as text so f isn't actually a function. I'm looking for some advice on what would be the best way to implement this in matlab? Is there a way for me to turn the user inputted function (eg) x*exp(x)) into a symbolic function? Perhaps a better way would be to create a vector of values for the subintervals and then use a for loop? Any advice would be much appreciated.
  2 comentarios
Walter Roberson
Walter Roberson el 3 de Sept. de 2017
Note: you forgot the multiply between the 0.5 and what follows.
Stephen23
Stephen23 el 26 de Sept. de 2017
Editada: Stephen23 el 26 de Sept. de 2017
"I'm an undergrad student taking a numerical methods course..."
It seems strange to use symbolic operations during a numeric methods course.

Iniciar sesión para comentar.

Respuesta aceptada

Karan Gill
Karan Gill el 25 de Sept. de 2017
Editada: Karan Gill el 17 de Oct. de 2017
Starting R2017b, use str2sym.
>> prompt = 'input function (of x): ';
f = input(prompt,'s');
input function (of x): sin(x)
>> syms myfun(x)
>> myfun(x) = str2sym(f)
myfun(x) =
sin(x)
>> fplot(myfun) % for example

Más respuestas (1)

Walter Roberson
Walter Roberson el 3 de Sept. de 2017
In current versions of MATLAB with the Symbolic Toolbox, it is possible to use
temp = sym(f);
v = symvar(temp);
Fsym = symfun( temp, v)
Now Fsym is a symbolic function
This will give you a warning:
Warning: Support of character vectors will be removed in a future release. Character vectors can be used only for variable names and numbers. Instead, to create
symbolic expressions first create symbolic variables using 'syms'. To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.
> In sym>convertExpression (line 1558)
In sym>convertChar (line 1463)
In sym>tomupad (line 1213)
In sym (line 210)
You can disable that with
oldstate = warning('off', 'symbolic:sym:sym:DeprecateExpressions');
temp = sym(f);
warning(oldstate);
v = symvar(temp);
Fsym = symfun( temp, v);
The more general approach is
varlist = strjoin(symvar(f),',');
Fhandle = str2func( ['@(', varlist, ') ', f ]);
Now Fhandle is the function handle of a numeric (non-symbolic) function. You can, if need be,
Fsym = sym(Fhandle);
to produce a symbolic function.
With regards to
T = symsum(0.5(f(xk-1)+f(xk))*h, k, 1, n);
Even if you fix the multiplication and switch to the symbolic function,
T = symsum(0.5*(Fsym(xk-1)+Fsym(xk))*h, k, 1, n);
that will not work. The variable xk does not exist, and if it did exist then because all of the terms would be the same, this would be equivalent to
n * (0.5*(Fsym(xk-1)+Fsym(xk))*h)
You are probably thinking of something like,
syms k
T = symsum(0.5*(Fsym(x(k-1))+Fsym(x(k)))*h, k, 2, n); %(Notice the change to start k from 2)
However, in that expression, k would be a symbolic variable, and it is never permitted to index by a symbolic variable. You need to instead construct the individual terms and then sum() them
Fxk = Fsym(x);
terms = 0.5 * (Fxk(1:end-1) + Fxk(2:end)) * h;
T = sym(terms);
... but if you had stayed with the numeric function handle, you could instead have used
T = trapz(x, arrayfun(Fhandle, x) );

Categorías

Más información sobre Formula Manipulation and Simplification en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by