Taking the integral of a piecewise function

49 views (last 30 days)
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];

Answers (1)

Dyuman Joshi
Dyuman Joshi on 13 Mar 2023
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 Comments
John D'Errico
John D'Errico on 13 Mar 2023
Edited: John D'Errico on 13 Mar 2023
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 on 13 Mar 2023
Edited: Dyuman Joshi on 14 Mar 2023
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)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!