Kindly help me integrate this function.

I am trying to integrate the following expression using 'quad' fuction but MATLAB is giving errors, kindly help me with the code. Code starts below:
f= @(x) ((sin(x))^2)/(((sin(x))^2)+(7));
% a, the lower limit
a= 0 ;
% b, the upper limit
b= pi/2 ;
answer=quad(f,a,b);
%%% Code ends
MATLAB gives following error:
??? Error using ==> mpower Inputs must be a scalar and a square matrix.
Error in ==> @(theta)((sin(theta))^2)/(((sin(theta))^2)+(7))
Error in ==> quad at 77 y = f(x, varargin{:});
Kindly help.

 Respuesta aceptada

Star Strider
Star Strider el 11 de Mayo de 2014
You need to vectorize your code.
This works:
f= @(x) ((sin(x)).^2)./(((sin(x)).^2)+(7));
a= 0 ;
b= pi/2 ;
answer=quad(f,a,b);
produces:
answer =
101.4509e-003

4 comentarios

Furqan Haider
Furqan Haider el 11 de Mayo de 2014
Bundle of thanks Star Strider, i am facing an other issue. In the function statement, instead of '7' I have a vector h=1:0.02:10 i.e.
f= @(x) ((sin(x)).^2)./(((sin(x)).^2)+(h));
I wish to integrate my function for all the values of vector 'h'. Can u help me out ?
My pleasure!
Here is your integral over your vector h:
h=1:0.02:10;
a= 0 ;
b= pi/2 ;
for k1 = 1:length(h)
H = h(k1);
f= @(x) ((sin(x)).^2)./(((sin(x)).^2)+H);
answer(k1)=quad(f,a,b);
end
Your answer variable is now a vector of length(h). When I plotted it, it looked like a smooth decaying exponential.
Furqan Haider
Furqan Haider el 11 de Mayo de 2014
Thank u again, I got what was required.
Best regards.
Star Strider
Star Strider el 11 de Mayo de 2014
My pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Mayo de 2014

Comentada:

el 11 de Mayo de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by