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.
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.
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.
It does seem that way, but this is only a particular case (d=0) of a more sophisticated function. I believe that all the components of the symbolic expression are needed in the general case.
One might suggest, for the purposes of generating a plot, that your simplified expression suffices. Maybe so, but I'm interested in understanding the reason for the wiggles. I feel that I must have misformulated the symbolic expression (or the plot command).
By the way, the plots (not shown here) for the cases d>0 look just fine.
It looks like you are trying to reinvent spline interpolation. That is unwise, since there are already stable tools in Matlab for doing spline interpolation, as well as for integrating splines (e.g. fnint).
Regardless, the truncated power spline basis that you are pursuing is known to be unstable numerically. The gold standard is De Boor's algorithm.
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.
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.
Thank you for pursuing this issue. I'm definitely learning from it. Still, some mysteries remain.
Firstly, I now realize that the definition of B(t,d) in my original post was simply incorrect. I suspect that you caught on to that right away. Not much else to say about it.
The code below generates four graphs. Mathematically, as far as I can tell, they should all be identical (and correct). Obviously, they're not.
The graphs in the second and fourth figures look correct.
The graph in the third figure looks correct, except for the wiggle. As explained by Torsten in a comment to this post, the difference has to do with the implementation that Matlab employs for a certain numerical routine that is employed by the fplot function. The graph in figure 4 employs a fix that Torsten recommended.
The graph in the first figure certainly looks very wrong, but the only difference between the code used to specify the first figure and the second figure is that between the cutoff1 and cutoff2 functions. These are mathematically identical, so it must be the case that Matlab is treating them differently under the hood. You probably already know about this because you pointed out that I was mistaken in my specification of that function in your latest post.
In the hopes of avoiding a similar pitfall in the future, I'd like to know:
Why does Matlab treat the cutoff1 and cutoff2 so differently?
These are mathematically identical, so it must be the case that Matlab is treating them differently under the hood
No, they are mathematically identical only for d>0. For d=0, cutoff1(t,d) equals 1 everywhere, whereas cutoff2(t,d) is the unit step that you really want.
syms t real;
syms k integer;
syms d integer;
syms tau real;
cutoff1(t,d) = (piecewise(t < 0, 0, t))^d;
cutoff2(t,d) = piecewise(t < 0, 0, t^d);
fplot(cutoff1(t,0),[-5,5],'r-'); hold on
fplot(cutoff2(t,0),[-5,5],'bo--'); hold off ;axis padded
No, they are mathematically identical only for d>0. For d=0, cutoff1(t,d) equals 1 everywhere, whereas cutoff2(t,d) is the unit step that you really want.
Yes. I see now.
Thanks again for all the insightful comments.
In summary:
cutoff1 is not the same as cutoff2 and the latter should be used to specify the "correct" function, IB0_2.
The expression in IB0_4 is also valid mathematically, but requires a special Waypoint-adjusted specification of the definite integral in order to avoid wiggle artifacts in the Matlab plot.
The expression in IB0_4 is also valid mathematically, but requires a special Waypoint-adjusted specification of the definite integral in order to avoid wiggle artifacts in the Matlab plot.
Valid in its current form for d=0. For general d, I believe that you would need,
f3(t,k,d) = f(t,k,d)*cutoff2(t-k,d);
B3(t,d) = symsum(f3,k,0,floor(t));
and I believe you would need waypoints at all k=1,2,...,d+1.
floor(t) is not needed, and note that my answer does not propose it. It is being considered here only because @Dan was seeking to compare my answer to several other proposals.
Avoiding floor(t) is better, because then the symbolic engine is then able to do the integral analytically, rather than numerically. However, this then prevents us from exploring how to accomplish the thing with vpaintegral.
If floor(t) is used, then use of the cutoff function can be avoided. There are advantages to this. For the Matlab implementation, it seems to me that Matt J's solution is the cleanest and quickest for Matlab. His solution avoids the floor(t) function but uses the cutoff function.
For instructional purposes, the following code generates two different figures in three different ways. Perhaps one solution appeals to you more than the other two.
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:
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.
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Translated by
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.