Int vs Integral; Int giving wrong results

5 次查看(过去 30 天)
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
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 CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by