Wierd wiggle in plot of symbolic expression

1 次查看(过去 30 天)
Apart from the small wiggles (the biggest near t=8) the plot of the symbolic expression below looks correct. How can I remove the wiggles?
clear; clc; close all;
syms t real;
syms k integer;
syms d integer;
cutoff(t) = piecewise(t < 0, 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));
syms tau real; % dummy variable of integration just for the case d=0
IB0(t) = int(B(tau,0),tau,0,t);
figure
hold on
fplot(IB0(t),[0,9], 'LineWidth', 2);
xlim([-1,10]);
ylim([-0.2,1.2]);
  1 个评论
Dan
Dan 2025-7-14
移动:Matt J 2025-7-14
Although this code uses Matlab's symbolic functionality, it seems (according to a comment of Matt J) that that some floating point computations are being performed under the hood and inaccuracy in these computations are causing the wiggles.
(In that case, I would guess that the function cutoff(t-k)^d might be the culprit.)
It was suggested that I use a different (more stable) algorithm to perform the computation, but I really only interested in the cause of the wiggles. Specifically, I'd like to know that I didn't misuse the symbolic functions.
Thanks Matt.

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2025-7-14
编辑:Torsten 2025-7-14
Using
IB0(t) = vpaintegral(@(tau)B(tau,0),tau,0,t,'Waypoints',[1]);
instead of
IB0(t) = int(B(tau,0),tau,0,t);
makes the wiggles disappear.
The reason for the wiggles is that your function B is discontinuous at tau = 1. Using the points of discontinuity as fixed grid points (Waypoints) in the integration gives exact results.
  3 个评论
Torsten
Torsten 2025-7-14
编辑:Torsten 2025-7-14
The problem indeed stemmed from my incorrect Matlab code.
Your MATLAB code is not incorrect, but MATLAB does not return an analytical expression for the function
IB0(t) = int(B(tau,0),tau,0,t)
Thus when you try to plot the function IB0(t), MATLAB has to use a numerical method to approximate IB0(t). This numerical method is based on the evaluation of B on a grid of tau-values. If the points of discontinuity of B are not part of these grid values for tau where B is evaluated, results can become slightly imprecise. One way to overcome this is to force the grid to contain the tau-values where B is discontinuous using the "Waypoints" option.
Dan
Dan 2025-7-14
I understand. Thank you again for the clear explanation. It's even more useful than your solution!

请先登录,再进行评论。

更多回答(2 个)

Matt J
Matt J 2025-7-14
编辑:Matt J 2025-7-14
Seems like a lot of unnecessary symbolic math gymnastics for such a simple function.
IB0=@(t) min(t,1);
fplot(IB0,[0,9], 'LineWidth', 2);
xlim([-1,10]);
ylim([-0.2,1.2]);
ylim([-0.2,1.2])
  4 个评论
Dan
Dan 2025-7-14
Do you think that this "numerical instability" causes the wiggles?
Matt J
Matt J 2025-7-14
编辑:Matt J 2025-7-15
Not in this case. The wiggles can be removed here by fixing some errors in your symbolic expressions (see my other answer). However, I expect you could run into numerical problems for sufficiently large d, one of several reasons not to do spline analysis this way.

请先登录,再进行评论。


Matt J
Matt J 2025-7-14
编辑:Matt J 2025-7-14
There appears to be a mistake in your expression for cutoff(). Also, using floor(t) for the upper limit of the symsum makes it harder on the symbolic engine than it needs to be.
syms t real;
syms k integer;
syms d integer;
cutoff(t,d) = piecewise(t < 0, 0, t^d); %<--- Matt J fixed
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(t,k,d) ,k,0,d+1); %<--- Matt J use d+1 as upper limit of sum
syms tau real; % dummy variable of integration just for the case d=0
IB0(t) = int(B(tau,0),tau,0,t);
figure
hold on
fplot(IB0,[0,9], 'LineWidth', 2);
xlim([-1,10]);
ylim([-0.2,1.2]);
  12 个评论
Matt J
Matt J 2025-7-18
编辑:Matt J 2025-7-18
OK, I see. However, since you're willing to accept a numerical solution from vpaintegral, I don't know why you don't just do the whole thing numerically, which is a fair bit faster:
timeit(@()methodSym)
ans = 0.0758
timeit(@()methodNum)
ans = 0.0013
fplot(methodSym,[0,9]); hold on
[T,IB]=methodNum;
plot(T,IB,'o')
legend Sym Num; hold off
function IB=methodSym
syms t real;
syms k integer;
syms d integer;
syms tau real; % dummy variable of integration
parity(k) = (-1)^k;
f(t,d,k) = 1/factorial(d)*parity(k)*nchoosek(d+1,k);
F(t,d,k) = f(t,d,k)*(t-k)^d;
B1(t,d) = symsum(F,k,0,floor(t));
W = [1]; %W = [1,2];
IB(t) = vpaintegral(@(tau)B1(tau,2),tau,0,t,'Waypoints',W);
end
function [T,IB]=methodNum
d=2;
parity = @(k) (-1).^k;
f = @(t,k) parity(k).*(d+1)./factorial(d+1-k)./factorial(k);
F= @(t,k) f(t,k).*(t-k).^d;
B = @(t) arrayfun( @(tau) sum( F(tau,0:min(floor(tau),d+1 ) ) ) ,t);
T=linspace(0,9,1e2);
IB=cumtrapz(T,B(T));
end
Dan
Dan 2025-7-18
编辑:Dan 2025-7-18
why you don't just do the whole thing numerically
My ignorance.
I care more about clarity and simplicity than speed (although some of the mixed symbolic/numeric solutions I tried took much too long).
I don't know how to determine (a priori) Matlab's capability to handle mathematical expressions symbolically. I also don't know how to determine appropriate 'Waypoints'. Unless and until I become more proficient at those, I'm hesitant to trust Matlabs symbolic capabilities.
Now that I am aware of the purely numeric solution (works for both the formula with the floor and the one with the cutoff) then I'll certainly stick with that. It seems much more straightforward.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by