Integration producd NaN as answer
2 次查看(过去 30 天)
显示 更早的评论
I tried to write a function file to implement a simple integration where 't' as input must be positive.
My code:
function y = Integration(t)
%Input a value t larger than 0, it will return an intergral
if t <= 0
disp('Input must be positive');
y = NaN;
else
syms x;
y = int(-(t-x)*exp(-(t-x))*(t-x>=0)*x^2*exp(-x)*sin(x)*(x>=0));
end
end %end of Integration2
But this code keep giving me NaN when I input postive numbers. Could someone kindly explain? Thanks!
0 个评论
回答(2 个)
Star Strider
2017-9-8
First, ‘t’ needs to be defined as a sym object;
Second, use the convenient heaviside function to define the unit step function:
syms x t
t = sym(5); % Example
y = int(-(t-x)*exp(-(t-x))*heaviside(t-x)*x^2*exp(-x)*sin(x)*heaviside(x));
This produces an expression for ‘y’ incorporating heaviside terms, since ‘x’ is not defined numerically.
I am not certain what result you want. At the very least, this solves the NaN problem.
2 个评论
Pal Szabo
2017-9-8
I think you should delete the >=0 parts from line 8. Like this:
y = Integration(2)
function y = Integration(t)
%Input a value t larger than 0, it will return an intergral
if t <= 0
disp('Input must be positive');
y = NaN;
else
syms x
y = int(-(t-x)*exp(-(t-x))*(t-x)*x^2*exp(-x)*sin(x)*(x));
end
end %end of Integration2
If you want numerical result, I think you should specify the value of x.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Install Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!