Definite integral of a constant

1 view (last 30 days)
I'm building a code to calculate the equal area criterion, the problem is that in one part I need to integrate a constant and matlab gives me an error when trying to integrate it:
For example:
P = 1;
limit_1 = 0;
limit_2 = 10;
Area = int (P,limit_1, limit_2);
is there a way around this? if P is a function of x or something it computes it OK, but if I input a constant like in the example it doesn't work.

Accepted Answer

John D'Errico
John D'Errico on 20 May 2017
Edited: John D'Errico on 20 May 2017
Note the difference in what I did.
P = sym(1);
limit_1 = 0;
limit_2 = 10;
Area = int (P,limit_1, limit_2)
Area =
10.0
If instead, you try something like this:
Area = int (1,limit_1, limit_2)
Undefined function 'int' for input arguments of type 'double'.
it throws an error, because int works on symbolic inputs, NOT on doubles. At least one argument must be symbolic. So this also works:
Area = int (1,0,sym(10))
Area =
10

More Answers (2)

Star Strider
Star Strider on 20 May 2017
Numerically:
P = @(x) 1;
limit_1 = 0;
limit_2 = 10;
Area = integral(P,limit_1, limit_2, 'ArrayValued',true)
  1 Comment
Walter Roberson
Walter Roberson on 20 May 2017
Or, better:
P = @(x) 1 * ones(size(x));
limit_1 = 0;
limit_2 = 10;
Area = integral(P,limit_1, limit_2)

Sign in to comment.


Walter Roberson
Walter Roberson on 20 May 2017
Area = int( sym(P), limit_1, limit_2);

Products

Community Treasure Hunt

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

Start Hunting!