Integration of real variable function on arbitrary interval.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
My symbolic variable is defined to be real, but when I integrate my function over a predefined rbitrary interval the integrate take it as complex variable.
x = sym("x","real");
int((x)^(1/3),-1,1)
ans =
I expected to receive 3/4 but received complex number.
Is there any way to get 3/4?
Note the integration must be done analytically and can not be changed to numerical and also the integral interval can not be changed to [-1,0]&[0,1], since in my original problem it is not easy to find this seperation point (0 here).
채택된 답변
Paul
2023년 12월 1일
Why would you expect the result to be 3/4?
Plotting the function, assuming we want the real cube root
plot(-1:.01:1,nthroot(-1:.01:1,3))

It looks like the integral should be 0, which is the obtained result when changing the integrand to use nthroot.
syms x real
int(nthroot(x,3),-1,1)
ans =
0
Witht the original integrand
int(x^(1/3),-1,1)
ans =

ans =

the result isn't necessarily complex. I've run into this before and I'm pretty sure there's a way to subsitute nthroot for the cube root using mapSymType. See this Question
댓글 수: 11
Why would you expect the result to be 3/4?
As I said the variable x is defined real which the function x^(1/3) can only be defined in [0,1] so the integral must equal to 3/4.
The number -1 is a real number. It has multiple cube roots:
N1 = sym(-1)
N1 = 
R0 = N1
R0 = 
R1 = N1^(1/3)
R1 = 
R2 = -(N1^(2/3))
R2 = 

R0^3
ans = 
R1^3
ans = 
R2^3
ans = 
The function x^(1/3) can be defined outside of [0, 1]
Even if you were to define that you wanted the real cube root, which MATLAB implements as nthroot(), @Paul shows that the integral would be 0.
If you want x^(1/3) to somehow be defined only in [0 1] but you want to integrate over [-1 1] then what do you expect MATLAB to do when it tries to integrate x^(1/3) for negative x ? If the result were NaN (that is, the result is not defined) then the integral would be (a bunch of NaNs added together) plus the integral over the positive part, which would be NaN plus a real value... which would give you a NaN result. The result of integrating "undefined" is "undefined".
If you expect MATLAB to substitute 0 for x^(1/3) for negative x, then you would have defined x^(1/3) over negative x to be 0 -- which is a contradiction to your statement that x^(1/3) can only be defined over [0 1]
syms x real
f = piecewise(x < 0, 0, x^(1/3))
f =
result = int(f, x, [-1 1])
result =
but that is different than integrating x^(1/3) over the same interval.
I was pleasantly surprised that int worked that way with nthroot
Let me clear my problem. I have a function that is build in a loop and must be integrated over [-1,1]. Some times the function has not been defined in -1<x<0 (like x^(1/3)), which results complex value for intergration that is not acceptible in my problem. On the other hand, in general it is not easy to make the function piecewise (finding boundaries is time consuming). It seems that matlab int functions do not have such capability that automatically integrate only over the interval that the integrand is defined correctly, doesnt matter what the integration interval is.
Substituting 0 for an undefined value is possible but it is mathematically meaningless, and can lead to incorrect numeric integrations.
fun = @(x) x.^(1/3);
integral(fun, -1, 1)
ans = 1.1250 + 0.6495i
Rfun = @(x) RealizeIt(fun(x))
Rfun = function_handle with value:
@(x)RealizeIt(fun(x))
integral(Rfun, -1, 1)
Warning: Inf or NaN value encountered.
ans = NaN
fakefun = @(x) FakeIt(fun(x))
fakefun = function_handle with value:
@(x)FakeIt(fun(x))
integral(fakefun, -1, 1)
ans = 0.7500
function y = RealizeIt(y)
y(~isfinite(y) | imag(y) ~= 0) = NaN;
end
function y = FakeIt(y)
y(~isfinite(y) | imag(y) ~= 0) = 0;
end
It seems that matlab int functions do not have such capability that automatically integrate only over the interval that the integrand is defined correctly, doesnt matter what the integration interval is.
How should "int" know what you consider as "defined correctly" ? x^(1/3) can be defined correctly by returning complex values for x<0, and it is also possible to integrate it for -1<x<0 once you have chosen the correct branch.
I have a function that is build in a loop and must be integrated over [-1,1].
That statement implies that the interval [-1,1] is included in the domain of the function.
Because we are talking about a function, each element in the domain, X, has to map to a single element in the codomain, call it Y. Therefore, each value of x in the interval [-1 1] must have a defined mapping to one value.
Yet you then say:
the function has not been defined in -1<x<0
The two statements in italics contradict each other, hence the problem statement is unclear.
Furthermore, the function y(x) = x^(1/3) can be defined on the interval -1 < x < 0, and, in fact, it must be defined in order to compute the integral of y(x).
I suspect that the issue here is that the term "defined" means something different to you than to me; maybe the problem can be explained without using "defined" so everyone in this thread can get on the same page.
Note by the way that the workaround functions I posted will sometimes fail. Suppose you two sub expressions where the respective expressions are somehow not defined, but when evaluated at the location produced finite intermediate results. There could be cases where the imaginary component of one might happen to be the negative of the imaginary component of the other. Add the two together and you get a real value, which the work-around functions would leave alone. But logically the fact that two undefined values happen to cancel doesn't make the result defined at that location. So you have to apply the fix up functions to every sub expression that might potentially be somehow undefined.
Hi @Mehdi
I believe @Walter Roberson's workaround may be effective if you can identify a mathematical property in the function that allows for complex-valued solutions. Even better, if you could share the actual mathematical function with us, we could test its functionality more accurately.
for loop
if property == true
fun = piecewise();
out = int(fun, x, interval)
else
out = int(fun, x, interval)
end
end
You have to be clearer about what it means to be undefined.
syms x real
fun = x^(1/3) + x^2
fun = 

If that is to be "undefined" for negative x, then is it only the x^(1/3) part that should be undefined, or is it the entire expression that should be undefined ? Like if you had int(sin(fun(x)), x, -1, 1) then is it the entire fun that is undefined for negative ?
fun2 = RealizeIt(x^(1/3)) + x^2
fun2 =

fun3 = RealizeIt(fun)
fun3 =

int(fun, x, -1, 1)
ans =

int(fun, x, 0, 1)
ans =
The original conception took 0 for the range below 0 as the x^(1/3) was considered undefined there -- so in the original, the integral was considered to be clipped below 0, with only the part above 0 to be evaluated.
But this slightly modified function has components that can be meaningfully evaluated below 0. Should those components be considered, or should the fact that one of the components is undefined mean that the whole expression should be considered undefined?
int(fun2, x, -1, 1)
ans =
The above is when you let the x^2 be considered valid below 0 and only zero out the x^(1/3)
int(fun2, x, 0, 1)
ans =
int(fun3, x, -1, 1)
ans =
int(fun3, x, 0, 1)
ans =
function y = RealizeIt(y)
y = piecewise(isfinite(y) & imag(y) == 0, y, 0);
end
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
