# Integration producd NaN as answer

조회 수: 3 (최근 30일)
Laze 2017년 9월 8일
댓글: Star Strider 2017년 9월 8일
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!

댓글을 달려면 로그인하십시오.

### 답변 (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없음 표시없음 숨기기
Laze 2017년 9월 8일
Thanks for your response! What I wanted to do is to a function 'Integration' that can calculate the above formula. I checked the definition of heaveside, it's kinda different from a unit step function because at x=0, heavside(x)=1/2 while u(x) should equal to 1.
Besides, by trying your revision, the problem of NaN has been solved but I still could not get a numerical result rather than the above formular with t substituted by my input. Could you tell me why this is happening? Thanks a lot!
Star Strider 2017년 9월 8일
My pleasure.
The ‘unit step function’ is defined as the Heaviside function.

댓글을 달려면 로그인하십시오.

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.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by