I need to add piecewise polynomials derived from multiple datasets. Is there an easy way to add piecewise polynomials together without interpolating? In other words, given PP1 and PP2, is there a way to generate PP3 (where PP3 remains in a piecewise polynomial form)? e.g...
t1 = linspace(0,1,5);
t2 = linspace(0,1,7);
pp1 = spline(t1,sin(pi*t1));
pp2 = spline(t2,t2.^2);
close all
hold on
tnew = linspace(0,1,50);
h(:,1) = plot(tnew,ppval(pp1,tnew));
plot(t1,ppval(pp1,t1),'bs')
h(:,2) = plot(tnew,ppval(pp2,tnew));
plot(t2,ppval(pp2,t2),'rs')
h(:,3) = plot(tnew,ppval(pp1,tnew)+ppval(pp2,tnew));
legend(h,{'spline of sin(\pi t)','spline of t^2','sin(\pi t)+t^2'},...
'location','northwest')
xlabel('t')
But instead of specifying tnew
explicitly, I would like a new piecewise polynomial pp3
that is effectively pp1
+pp2
.
This is probably the easiest way to get
pp1
+pp2
Adding to the code in the question:Gives the dotted black line:
with
pp3
being in piecewise-polynomial form with segments defined only by the breakpoints of pp1 and pp2. Runningmax(abs(ppval(pp3,tnew) - pp12(tnew)))
yields2.7756e-16
, which is of the order ofeps
.Thanks to @LuisMendo and @TroyHaskin for their suggestions.