# 381 - A9 - Piecewise Hermite Interpolation

## 296 days ago by Professor381

# Consider two quadratics that meet at a middle point x1 = -2 y1 = 3 x2 = -1 y2 = 1 x3 = 0 y3 = 2 x4 = 3 y4 = -1 x5 = 4 y5 = 0 p1 = y1*(x-x2)*(x-x3)/((x1-x2)*(x1-x3)) + y2*(x-x1)*(x-x3)/((x2-x1)*(x2-x3)) + y3*(x-x1)*(x-x2)/((x3-x1)*(x3-x2)) # using Basis approach p2 = y3*(x-x4)*(x-x5)/((x3-x4)*(x3-x5)) + y4*(x-x3)*(x-x5)/((x4-x3)*(x4-x5)) + y5*(x-x3)*(x-x4)/((x5-x3)*(x5-x4)) # using Basis approach G = point([(x1,y1),(x2,y2),(x3,y3),(x4,y4),(x5,y5)],size=20) G += plot(p1,(x,x1,x3),color='red') + plot(p2,(x,x3,x5),color='orange') show(G)

It would be nice if where the curves met that the slopes also met.  Unfortunately, there is no more "freedom" in choosing the quadratics.  Need more coefficients to pick.

# This cell in Sage mode var('u') H0 = (1-u)^2*(1+2*u) H1 = (1-u)^2*u H2 = (u-1)*u^2 H3 = (3-2*u)*u^2 G = plot(H0,(u,0,1),color='black')+plot(H1,(u,0,1),color='red')+plot(H2,(u,0,1),color='green')+plot(H3,(u,0,1),color='blue') show(G,aspect_ratio=1) show(H0) show(H1) show(H2) show(H3)

The rest of this is in octave mode

function y = H0(u) y = (1-u) .^2 .* (1+2*u); end function y = H1(u) y = (1-u) .^2 .*u; end function y = H2(u) y = (u-1) .* u .^2; end function y = H3(u) y = (3-2 .*u) .* u .^2; end n = 4; x = [-1 2 4 7 13]; y = [5 4.5 2 3 0]; m = [-1 -1 -1 -1 -2]; for k = 2:n+1 xx = x(k-1):0.05:x(k); delx = x(k)-x(k-1); yy = H0( (xx-x(k-1))/delx )*y(k-1) + H1( (xx-x(k-1))/delx )*m(k-1)*delx + H2( (xx-x(k-1))/delx )*m(k)*delx + H3( (xx-x(k-1))/delx )*y(k); plot(xx,yy) hold on end plot(x,y,'rd')
  6 |-----------------------------------------------------------------------\ | | ***** + + | | *** ** | 5 |-+ L% ** +-| | %%%%%%%%%% | | %% *L@ | | *@ | 4 |-+ ##@ +-| | # @ $$| | # @@$$ && | 3 |-+ ## @@ $$&&&&L= +-| | # @@ && == | | ## @@ && ===== = | 2 |-+ #$$ L& && =========== +-| | &&& == | | == | | = | 1 |-+ = +-| | == | | + + + = | 0 |-----------------------------------------------------------------------\ | -5 0 5 10 15   6 |-----------------------------------------------------------------------| | ***** + + | | *** ** | 5 |-+ L% ** +-| | %%%%%%%%%% | | %% *L@ | | *@ | 4 |-+ ##@ +-| | # @ $$| | # @@$$ && | 3 |-+ ## @@ $$&&&&L= +-| | # @@ && == | | ## @@ && ===== = | 2 |-+ #$$ L& && =========== +-| | &&& == | | == | | = | 1 |-+ = +-| | == | | + + + = | 0 |-----------------------------------------------------------------------| -5 0 5 10 15 

Here is a parametric version of above.  Notice the use of sign to make the slopes match graphical reality.

function y = H0(u) y = (1-u) .^2 .* (1+2*u); end function y = H1(u) y = (1-u) .^2 .*u; end function y = H2(u) y = (u-1) .* u .^2; end function y = H3(u) y = (3-2 .*u) .* u .^2; end n = 4; t = [0 1 2 4 7]; x = [4 2 1.5 1 4]; y = [5 4.5 4 3 0]; run = [1 1 0 0 1]; rise = [-1 1 1 -1 -2]; for k = 2:n+1 tt = t(k-1):0.05:t(k); delt = t(k)-t(k-1); xx = H0( (tt-t(k-1))/delt )*x(k-1) + H1( (tt-t(k-1))/delt )*run(k-1)*delt*sign(x(k)-x(k-1)) + H2( (tt-t(k-1))/delt )*run(k)*delt*sign(x(k)-x(k-1)) + H3( (tt-t(k-1))/delt )*x(k); yy = H0( (tt-t(k-1))/delt )*y(k-1) + H1( (tt-t(k-1))/delt )*rise(k-1)*delt*sign(y(k)-y(k-1)) + H2( (tt-t(k-1))/delt )*rise(k)*delt*sign(x(k)-x(k-1)) + H3( (tt-t(k-1))/delt )*y(k); plot(xx,yy) hold on end plot(x,y,'rd')
  6 |-----------------------------------------------------------------------\ | | + + + + + | | | 5 |-+ *****************************| | *************** | | ####L*** | | ######## | 4 |-+ L +-| | $$| |%%%%%%%%%%% | 3 |-+$$$$%%%%%%%%%%% +-| |$$$$%%%%%%% | | %%%%%% | 2 |-+ %%%%%%% +-| | %%%% | | %%%%%%% | | %%%% | 1 |-+ %%%%% +-| | %%%% | | + + + + + %%%% | 0 |-----------------------------------------------------------------------\ | 1 1.5 2 2.5 3 3.5 4   6 |-----------------------------------------------------------------------| | + + + + + | | | 5 |-+ *****************************| | *************** | | ####L*** | | ######## | 4 |-+ L +-| |$$ | |%%%%%%%%%%% | 3 |-+  %%%%%%%%%%% +-| | %%%%%%% | | %%%%%% | 2 |-+ %%%%%%% +-| | %%%% | | %%%%%%% | | %%%% | 1 |-+ %%%%% +-| | %%%% | | + + + + + %%%% | 0 |-----------------------------------------------------------------------| 1 1.5 2 2.5 3 3.5 4