Taking the integral of a piecewise function

조회 수: 13 (최근 30일)
Mary Jean Savitsky
Mary Jean Savitsky 2023년 3월 13일
편집: Dyuman Joshi 2023년 3월 14일
I am very new to MATLAB but I am trying to solve for the zeroth first and second order moments using the defined piecewise function. m_k=Integral from 0 to infinity of C1T*T^k dT where k=0,1,2. Co, K, Ko are all constants for which I do not have numerical values for.
I keep getting the following error for the lines computing m0,m1 and m2. I appreciate any help!
Warning: Unable to check whether the integrand exists everywhere on the integration interval.
> In symengine
In sym/int (line 167)
In Equation17 (line 7)
clear
clc
syms Co K Ko T
% Define piecewise function for C(1,T)
C1T = piecewise(T<1, Co, 1<T<(1+1/K), Co*K/Ko, T>(1+1/K), 0);
% Compute moments m0, m1, m2 using symbolic math toolbox
m0 = int(C1T, T, 0, Inf);
m1 = int(C1T*T, T, 0, Inf);
m2 = int(C1T*T^2, T, 0, Inf);
% Evaluate moments numerically
y = [m0, m1, m2];

답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 3월 13일
You need to specify that K is a positive number, otherwise conditions might overlap
syms Co K Ko T
assume(K, 'positive')
% Define piecewise function for C(1,T)
C1T = piecewise(T<1, Co, 1<T<(1+1/K), Co*K/Ko, T>(1+1/K), 0)
C1T = 
% Compute moments m0, m1, m2 using symbolic math toolbox
m0 = int(C1T, T, 0, Inf)
m0 = 
m1 = int(C1T*T, T, 0, Inf)
m1 = 
m2 = int(C1T*T^2, T, 0, Inf)
m2 = 
% Evaluate moments numerically
y = [m0, m1, m2];
  댓글 수: 2
John D'Errico
John D'Errico 2023년 3월 13일
편집: John D'Errico 2023년 3월 13일
I'm surprised. It seemed to me the error message was telling us that C1T was undefined at T == 1. It is a set of measure zero, but still, that was what the error seemed to say. That was going to be my initial guess. But it worked for you, without repairing the hole in the real line.
Dyuman Joshi
Dyuman Joshi 2023년 3월 13일
편집: Dyuman Joshi 2023년 3월 14일
Yes, the function is not defined and would output NaN for T==1.
However, Limits of Integration are treated to be inclusive in nature. Thus, even though the boundaries are discountinuous for the function; for the integration they are treated as inclusive limits i.e. [0,1], [1,1+1/K] and [1+1/K, Inf) (except for Inf of course)

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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by