How to get defined integral?

Hi, I have a small project due in a couple of hours. Currently I'm using the following code:
x = linspace(-3,6,1000); % Define x-interval as -3 to 6 with 1000 points defined in vector
y = x.^3 - 5*x.^2 - 4*x + 20; % Define y as equation
plot(x,y)
Now I have to find the defined integral over the interval -2,5. A minute ago I put the following into the command window:
int(x.^3 - 5*x.^2 - 4*x + 20,-2,5)
and got the right answer. But now suddenly it doesn't work. I get the error:
Undefined function 'int' for input arguments of type 'double'.
What's the problem here? Also, once it works, how do I get an output when written in the editor and run (will it show a window like the axes plotted or do I have to tell it to show the output somewhere?)
Thanks

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2012 年 10 月 15 日
編集済み: Andrei Bobrov 2012 年 10 月 15 日

1 投票

x = linspace(-3,6,1000);
y = x.^3 - 5*x.^2 - 4*x + 20;
out = quad(@(x)x.^3 - 5*x.^2 - 4*x + 20,-2,5);
or
out2 = diff(polyval(polyint([1 -5 -4 20]),[-2,5]));
or your case
syms x
out3 = int(x.^3 - 5*x.^2 - 4*x + 20,-2,5)
or if you use MATLAB R2012a or R2012b
out4 = integral(@(x)x.^3 - 5*x.^2 - 4*x + 20,-2,5);
Walter Roberson
Walter Roberson 2012 年 10 月 15 日

1 投票

The "int" call would have worked if you had used
syms x
before you made the int() call, and you had not assigned any numeric value to x between the "syms" command and the int() call.

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2012 年 10 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by