MATLAB: Please find error in this 1-line code: I’m using the function for integration.

numerical integration

Please find error in this code: I'm using the int function for integration.
>> syms k
>> int(exp(-k)*cos(k),0,1)
This works, but…
>> syms k
>> int(exp(-k^2)*cos(k),0,1)
this does not work… I don't know what is wrong with this… I'm so frustrated…
Please help me.

Best Answer

The problem is that int(exp(-k^2)) is essentially the ‘error function’. To the best of my knowledge, it has no closed-form analytic solution. Taking the Taylor series of the integrand and then integrating it seems to work:
syms k
f = exp(-k^2)*cos(k);
ft = taylor(f, k, 'Order',11)
ift = int(ft, k, 0, 1)
produces:
ift =
26172299/39916800
Experiment with it until you get the result you want.