# Problem in formatting a function

1 view (last 30 days)
Sergio Manzetti on 24 Jan 2018
Commented: Sergio Manzetti on 25 Jan 2018
Hi I am trying to solve this intergral:
syms x h
h = 1.0545718E-34
f = exp.^(5.06E6.*i.*x+(1./h))+3/4.*exp.^(1./h)+i.*5.06E6./2.*exp.^(1./h)
integral(f, -pi, pi)
But it appears wrong. Can't get a proper indication on what is wrong in MATLAB online. Thanks!

Steven Lord on 24 Jan 2018
One problem specific to how you've written your code is that you're calling exp incorrectly. To compute the exponential of a variable z you need to use exp(z) instead of exp.^z.
In general:
• To integrate a function that returns numeric values, use the integral function.
f1 = @(x) x.^2;
result1 = integral(f1, 0, 1)
• To integrate a symbolic expression symbolically, use the int function.
syms x
f2 = x^2;
result2a = int(f2, x)
result2b = int(f2, x, 0, 1)
result2b1 = vpa(result2b)
result2b2 = double(result2b)
• To integrate a symbolic expression numerically, use one of two approaches. Either use vpaintegral or convert the symbolic expression into a function that returns numeric values using the matlabFunction function and then use the integral function.
syms x
helper = x^2;
f3 = matlabFunction(helper);
result3a = vpaintegral(helper, 0, 1)
result3b = integral(f3, 0, 1)
Sergio Manzetti on 25 Jan 2018
Thanks Steven!