MATLAB: How Do I Find the Integral of a Function from One Variable to Another

functionintintegralintegrationintervalvaribale

I need to make create a function that will find the integral of a function from the variables x1 and x2 and I cannot figure out how to do this. First, I wrote the code like this:
function b=integrfn(A,x1,x2)
y= @(x)A*exp(-x^2)*cos(2*x)/(1+x^2)
b=integral(y,x1,x2)
end
That didn't work, so I wrote
function b=integral(A,x1,x2)
int(A*exp(-x^2)*cos(2*x)/(1+x^2),x1,x2)
end
That still didn't work and I have no idea what else to try or how to fix. Any help would be greatly appreciated!

Best Answer

>> integrfn(4,-9,17)
ans =
2.9018
here
function b=integrfn(A,x1,x2)
y= @(x)A*exp(-x.^2).*cos(2*x)./(1+x.^2);
b=integral(y,x1,x2);
end
Related Question