Problem with Symbolic Functions
Show older comments
I would like to create a simple "FunctionAnalyzer" in which the user inserts a function expression in general form with some parameters ('f(x)=a*x^2+b*x+c') and the graph is plotted for specific values of parameters chosen from user. And this is done in this way:
syms y(x,a,b,c,...)
y(x,a,b,c,...)=str2sym('a*x^2+b*x+c');
fplot(y(x,1,2,3,...));
My problem raises in the absense of a summation function, so i dediced to create my own function: sumk(f,a,b). Now i can write the function 'sumk(cos(c*k*x)),a,b)' which, in theory, should perform the summation for k=a to b of cos(c*k*x)) with a,b,c parameters. I tryed almost everything but I came to nothing. In particular i wrote:
syms y(x)
a=0;
b=2;
c=2;
y(x)=subs(str2sym('sumk(cos(c*k*x),a,b)'))
str2sym() is the only function i found that allow me to evaluate my local function, but it doesn't work without the actual values of a and b (since they appear in "for k=a:b") so i should substitute them with numbers using subs():
y(x)=subs('sumk(cos(c*k*x),a,b)')
but now the result is y(x)=sumk(cos(2*k*x),0,2) and all I need is evaluate the my local function, maybe using str2sym():
y(x)=str2sym(subs('sumk(cos(c*k*x),a,b)'))
but I get an error since the output of subs() is Sym and str2sym() requires a string.
I tried also to plot directly sumk(cos(2*k*x),0,2) with fplot() but I get an error due to the presence of 2 indipendent variables.
What can i do? How can I tell MatLab to evaluete the function sumk in the symbolic expression sumk(cos(2*k*x),0,2)? Is there a completly different approach i don't know? Please, remenber that this algorithm should be really really fast because I plot many functions with different parameters values in sequence.
Thanks for you help, Federico Spada
Answers (1)
madhan ravi
on 4 Jan 2019
help str2func
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!