Result of symbolic definite integral is clearly wrong

6 visualizzazioni (ultimi 30 giorni)
Dan
Dan il 12 Lug 2025
Commentato: Paul il 15 Lug 2025
The plot of the rather complicated function B(t,d) seems correct (see figure 1).
The plot of its definite integral from 0 to t (in the case d=0) is incorrect (see figure 2). It should have constant value 1 on the interval [1,2], but the plot indicates a constant value of 0 on that interval.
What is going on? Did I misuse the symbolic math functions?
PS Both plots look correct for integers d>1.
syms t k d;
cutoff(t) = piecewise(t < 0, 0, t >= 0, t);
parity(k) = 1-2*mod(k,2);
f(t,k,d) = 1/factorial(d)*parity(k)*nchoosek(d+1, k)*cutoff(t-k)^d;
B(t,d) = symsum(f,k,0,floor(t));
IB(t,d) = int(B(t,d),0,t);
Warning: Unable to check whether the integrand exists everywhere on the integration interval.
Warning: Unable to check whether the integrand exists everywhere on the integration interval.
bad_d=0;
figure(1)
fplot(B(t,bad_d), [-1,2], 'LineWidth', 2);
figure(2)
fplot(IB(t,bad_d), [-1,2], 'LineWidth', 2);

Risposte (2)

Paul
Paul il 13 Lug 2025
Modificato: Paul il 13 Lug 2025
Hi Dan,
Add assumptions on variables, might help with simplifications.
syms t real
syms d integer
syms k integer
cutoff(t) = piecewise(t < 0, 0, t >= 0, t);
parity(k) = 1-2*mod(k,2);
f(t,k,d) = 1/factorial(d)*parity(k)*nchoosek(d+1, k)*cutoff(t-k)^d;
B(t,d) = simplify(symsum(f,k,0,floor(t)))
B(t, d) = 
bad_d=0;
B(t,bad_d)
ans = 
figure
fplot(B(t,bad_d), [-1,2], 'LineWidth', 2);
I'm not sure what fplot is doing for t < 0, because it seems like B(t,0) is not well-defined for that situation.
I know that Matlab allows us to use the same variable as the variable of integration and in the limits of integration, but I always think it's more clear to integrate wrt to a different dummy variable.
syms tau real
IB(t,d) = simplify(int(B(tau,d),tau,0,t))
Warning: Unable to check whether the integrand exists everywhere on the integration interval.
Warning: Unable to check whether the integrand exists everywhere on the integration interval.
IB(t, d) = 
However, tau is still included in IB(t,d), which is a problem (and is troubling as a result).
Instead of treating d as a parameter in the integral, perhaps it will be better to enter it as a value and then integrate
figure
fplot(int(B(tau,bad_d),tau,0,t),[-1,2])
Is it sensible to fplot the integral for t < 0?
  4 Commenti
Paul
Paul il 13 Lug 2025
As an alternative, instead of taking the integral of the sum, try taking the sum of the integrals.
sympref('default'); % Sometimes needed on Answers
syms t real;
syms k integer;
syms d integer;
cutoff(t) = piecewise(t < 0, 0, t >= 0, t);
parity(k) = 1-2*mod(k,2);
f(t,k,d) = simplify(1/factorial(d)*parity(k)*nchoosek(d+1, k)*cutoff(t-k)^d);
syms tau real
IF(t,k,d) = simplify(int(f(tau,k,d),tau,0,t))
Warning: Unable to check whether the integrand exists everywhere on the integration interval.
Warning: Unable to check whether the integrand exists everywhere on the integration interval.
IF(t, k, d) = 
Not sure what to think of that warning.
Continuing
%B(t,d) = simplify(symsum(f,k,0,floor(t)))
IB(t,d) = symsum(IF(t,k,d),k,0,floor(t))
IB(t, d) = 
IB(t,d) = simplify(IB(t,d))
IB(t, d) = 
figure
fplot(IB(t,[0,1,2]),[0,5])
ylim([0,1.1])
Don't know why IB(t,0) doesn't fplot.
It seems to be well defined
IB(t,0)
ans = 
And evaluates and plot with numerical values
tval = 0:.01:2;
figure
plot(tval,double(IB(tval,0)))
Paul
Paul il 15 Lug 2025
Using
the second approach
redefining cutoff as suggested by @Matt J in this answer
using d+1 for the upper limit of the symsum as suggeste by Matt J in that same answer
and assuming k >= 0 and d >= 0
and NOT simplifying so that the nchoosek term isn't simplified to factorials
sympref('default'); % Sometimes needed on Answers
syms t real;
syms k integer;
syms d integer;
cutoff(t) = piecewise(t < 0, 0, t >= 0, t^d);
parity(k) = 1-2*mod(k,2);
f(t,k,d) = (1/factorial(d)*parity(k)*nchoosek(d+1, k)*cutoff(t-k))
f(t, k, d) = 
syms tau real
assumeAlso([k,d] >= 0)
IF(t,k,d) = (int(f(tau,k,d),tau,0,t))
IF(t, k, d) = 
IB(t,d) = symsum(IF(t,k,d),k,0,d+1)
IB(t, d) = 
figure
fplot(IB(t,[0:4]),[0,10])

Accedi per commentare.


Dan
Dan il 13 Lug 2025
Modificato: Torsten il 13 Lug 2025
The following code does what I wanted. Unfortunately,
1) It's a bit of a hack (why the special treatment for the case d=0?)
2) The plot for the curve d=0 (in blue) has two small wiggles (near t=5 and t=8) that I can't explain.
clear; clc; clear all; close all;
syms t real;
syms k integer;
syms d integer;
syms tau real; % used as a dummy variable of integration
cutoff(t) = piecewise(t < 0, 0, t);
parity(k) = 1-2*mod(k,2);
f0(t,k,d) = 1/factorial(d)*parity(k)*nchoosek(d+1, k);
f(t,k,d) = 1/factorial(d)*parity(k)*nchoosek(d+1, k)*cutoff(t-k)^d;
B(t,d) = symsum(f,k,0,floor(t));
IB(t,d) = int(B(t,d),t,0,t); % plotting this causes an error when d=0
IB0(t) = int(B(tau,0),tau,0,t); % plot this function for the case d=0
figure
hold on
fplot(IB0(t),[0,9], 'LineWidth', 2);
fplot(IB(t,1),[0,9]);
fplot(IB(t,2),[0,9]);
fplot(IB(t,4),[0,9]);
fplot(IB(t,8),[0,9]);
xlim([-1,10]);

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by