Taking the integral of a piecewise function

23 ビュー (過去 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)

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by