How to multiply two user input expressions?

How do I multiply two expressions so that I can integrate the outcome after?
My code so far:
function CenterOfMass2
str1 = input('Function f1(x): ','s') ;
str2 = input('Function f2(x): ','s') ;
a = input('Sākuma robeža: ') ;
b = input('Beigu robeža: ') ;
syms x
s=(int(str1,x,a,b)-int(str2,x,a,b));
%k=charpoly(str1,x)*charpoly(str1,x)
%g=charpoly(str2,x)*charpoly(str2,x)
%p=k-g
Mx=int(k,x,a,b)
My=int((x*str2),x,a,b);
coord_x=Mx/s
coord_y=My/s
I understand that charpoly is not correct, but nothing else I've tried has even worked .

 Accepted Answer

Stephen23
Stephen23 on 26 Apr 2017
Edited: Stephen23 on 26 Apr 2017
You could use str2func:
str1 = input('Function f1(x): ','s') ;
str2 = input('Function f2(x): ','s') ;
fun1 = str2func(['@(x)',str1]);
fun2 = str2func(['@(x)',str2]);
funX = @(x)fun1(x)*fun2(x);
And tested:
>>
Function f1(x): sin(x)
Function f2(x): x.^2
>> funX(pi/2)
ans =
2.4674
>> sin(pi/2) * (pi/2).^2
ans =
2.4674

More Answers (0)

Community Treasure Hunt

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

Start Hunting!