Int vs Integral; Int giving wrong results

5 ビュー (過去 30 日間)
James Mathews
James Mathews 2016 年 1 月 4 日
回答済み: Christopher Creutzig 2018 年 3 月 21 日
Suppose I have the function
p=@(x) (heaviside(x-0.2)-heaviside(x-0.3)).*(x.^3+x.^2+x+1)+(heaviside(x-0.3)-heaviside(x-0.8)).*(-3*x.^3-2*x.^2+3*x+0.77)
which basically comes from using cubic splines to interpolate functions. I want to calculate the integral of p(r)^2/r between 1 and some number S, ideally symbolically.
If I do integral then I get:
integral( @(s)(p(s)).^2./s,1,0.3)=-1.601
If instead I use int:
syms S
g=matlabFunction(int( @(s)(p(s)).^2./s,1,S),'Vars', {S});
g(0.3)=0
Clearly the int is giving the wrong result, but I don't know why. This example comes from a much more complicated example where integral gives the right result, and in the other case int gives non zero results but they are very wrong.
I would like to use int but I need to understand why it is giving such wrong results!
Thanks,
James

回答 (2 件)

Christopher Creutzig
Christopher Creutzig 2018 年 3 月 21 日
As the documentation says down in the “Tips” section, int(f,1,S) assumes that 1 ≤ S. In that region, your input function is 0, so int returning 0 is correct. Call int(f,S,1) to get a result valid for S ≤ 1.

Walter Roberson
Walter Roberson 2016 年 1 月 4 日
int() does not take a function handle as an argument.
syms s
g = matlabFunction( int((p(s)).^2./s,1,S), 'vars', S);

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by