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
0 个评论
回答(2 个)
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.
0 个评论
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!