Error message when computing integral: first input argument must be a function handle

250 次查看(过去 30 天)
I'm trying to evaluate this integral, but when I enter this code:
syms x;
S= (6*(sin(x)^2)/(3*sin(x)+1));
l= integral(S,0,pi)
it gives me the error message: Error using integral, first input argument must be a function handle.
I'm a beginner Matlab user, so the answer is probably pretty straightforward, but any help is appreciated, thanks in advance!


Josh Meyer
Josh Meyer 2017-9-13
If you want to compute the integral symbolically by keeping the line "syms x;", then you'll need to use the function in Symbolic Math Toolbox for integrating: int.
In that case the code looks like this:
syms x
S = (6*(sin(x)^2)/(3*sin(x)+1));
l = int(S,x,0,pi)
An alternate way to do this is to continue using the integral function. In that case you don't need the line "syms x", since integral integrates function handles. Function handles look like a normal line of code, but the beginning of the line defines the variables in the expression using the @ symbol. In this case the code looks like:
S = @(x) (6*(sin(x).^2)./(3*sin(x)+1));
l = integral(S,0,pi)
(Note the subtle use of element-wise operations .^ and ./ in this second case)

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by