Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7729501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:59:00+00:00 2026-06-01T05:59:00+00:00

edit: I’m not looking for you to debug this code. If you are familiar

  • 0

edit: I’m not looking for you to debug this code. If you are familiar with this well-known algorithm, then you may be able to help. Please note that the algorithm produces the coefficients correctly.

This code for cubic spline interpolation is producing linear splines and I can’t seem to figure out why (yet). The algorithm comes from Burden’s Numerical Analysis, which is just about identical to the pseudo code here, or you can find that book from a link in the comments (see chapter 3, it’s worth having anyway). The code is producing the correct coefficients; I believe that I am misunderstanding the implementation. Any feedback is greatly appreciated. Also, i’m new to programming, so any feedback on how bad my coding is also welcome. I tried uploading pics of the linear system in terms of h, a, and c, but as a new user i can not. If you want a visual of the tridiagonal linear system that the algorithm solves, and which is set up by the var alpha, see the link in the comments for the book, see chap 3. The system is strictly diagonally dominant, so we know there exists a unique solution c0,…,cn. Once we know the ci values, the other coefficients follow.

import matplotlib.pyplot as plt

# need some zero vectors...
def zeroV(m):
    z = [0]*m
    return(z)

 #INPUT: n; x0, x1, ... ,xn; a0 = f(x0), a1 =f(x1), ... , an = f(xn).
 def cubic_spline(n, xn, a, xd):
    """function cubic_spline(n,xn, a, xd) interpolates between the knots
       specified by lists xn and a. The function computes the coefficients
       and outputs the ranges of the piecewise cubic splines."""        

    h = zeroV(n-1)

    # alpha will be values in a system of eq's that will allow us to solve for c
    # and then from there we can find b, d through substitution.
    alpha = zeroV(n-1)

    # l, u, z are used in the method for solving the linear system
    l = zeroV(n+1)
    u = zeroV(n)
    z = zeroV(n+1)

    # b, c, d will be the coefficients along with a.
    b = zeroV(n)     
    c = zeroV(n+1)
    d = zeroV(n)    

for i in range(n-1):
    # h[i] is used to satisfy the condition that 
    # Si+1(xi+l) = Si(xi+l) for each i = 0,..,n-1
    # i.e., the values at the knots are "doubled up"
    h[i] = xn[i+1]-xn[i]  

for i in range(1, n-1):
    # Sets up the linear system and allows us to find c.  Once we have 
    # c then b and d follow in terms of it.
    alpha[i] = (3./h[i])*(a[i+1]-a[i])-(3./h[i-1])*(a[i] - a[i-1])

# I, II, (part of) III Sets up and solves tridiagonal linear system...
# I   
l[0] = 1      
u[0] = 0      
z[0] = 0

# II
for i in range(1, n-1):
    l[i] = 2*(xn[i+1] - xn[i-1]) - h[i-1]*u[i-1]
    u[i] = h[i]/l[i]
    z[i] = (alpha[i] - h[i-1]*z[i-1])/l[i]

l[n] = 1
z[n] = 0
c[n] = 0

# III... also find b, d in terms of c.
for j in range(n-2, -1, -1):      
    c[j] = z[j] - u[j]*c[j+1]
    b[j] = (a[j+1] - a[j])/h[j] - h[j]*(c[j+1] + 2*c[j])/3.
    d[j] = (c[j+1] - c[j])/(3*h[j])   

# This is my only addition, which is returning values for Sj(x). The issue I'm having
# is related to this implemention, i suspect.
for j in range(n-1): 
    #OUTPUT:S(x)=Sj(x)= aj + bj(x - xj) + cj(x - xj)^2 + dj(x - xj)^3; xj <= x <= xj+1)
    return(a[j] + b[j]*(xd - xn[j]) + c[j]*((xd - xn[j])**2) + d[j]*((xd - xn[j])**3))

For the bored, or overachieving…

Here is code for testing, the interval is x: [1, 9], y:[0, 19.7750212]. The test function is xln(x), so we start 1 and increase by .1 up to 9.

ln = [] 
ln_dom = [] 
cub = [] 
step = 1. 
X=[1., 9.] 
FX=[0, 19.7750212]
while step <= 9.:
    ln.append(step*log(step))
    ln_dom.append(step)
    cub.append(cubic_spline(2, x, fx, step))
    step += 0.1

…and for plotting:

plt.plot(ln_dom, cub, color='blue')
plt.plot(ln_dom, ln, color='red')
plt.axis([1., 9., 0, 20], 'equal')
plt.axhline(y=0, color='black')
plt.axvline(x=0, color='black')
plt.show()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:59:02+00:00Added an answer on June 1, 2026 at 5:59 am

    Ok, got this working. The problem was in my implementation. I got it working with a different approach, where the splines are constructed individually instead of continuously. This is fully functioning cubic spline interpolation by method of first constructing the coefficients of the spline polynomials (which is 99% of the work), then implementing them. Obviously this is not the only way to do it. I may work on a different approach and post that if there is interest. One thing that would clarify the code would be an image of the linear system that is solved, but i can’t post pics until my rep gets up to 10. If you want to go deeper into the algorithm, see the text book link in the comments above.

    import matplotlib.pyplot as plt
    from pylab import arange
    from math import e
    from math import pi
    from math import sin
    from math import cos
    from numpy import poly1d
    
    # need some zero vectors...
    def zeroV(m):
        z = [0]*m
        return(z)
    
    #INPUT: n; x0, x1, ... ,xn; a0 = f(x0), a1 =f(x1), ... , an = f(xn).
    def cubic_spline(n, xn, a):
    """function cubic_spline(n,xn, a, xd) interpolates between the knots
       specified by lists xn and a. The function computes the coefficients
       and outputs the ranges of the piecewise cubic splines."""        
    
        h = zeroV(n-1)
    
        # alpha will be values in a system of eq's that will allow us to solve for c
        # and then from there we can find b, d through substitution.
        alpha = zeroV(n-1)
    
        # l, u, z are used in the method for solving the linear system
        l = zeroV(n+1)
        u = zeroV(n)
        z = zeroV(n+1)
    
        # b, c, d will be the coefficients along with a.
        b = zeroV(n)     
        c = zeroV(n+1)
        d = zeroV(n)    
    
        for i in range(n-1):
            # h[i] is used to satisfy the condition that 
            # Si+1(xi+l) = Si(xi+l) for each i = 0,..,n-1
            # i.e., the values at the knots are "doubled up"
            h[i] = xn[i+1]-xn[i]  
    
        for i in range(1, n-1):
            # Sets up the linear system and allows us to find c.  Once we have 
            # c then b and d follow in terms of it.
            alpha[i] = (3./h[i])*(a[i+1]-a[i])-(3./h[i-1])*(a[i] - a[i-1])
    
        # I, II, (part of) III Sets up and solves tridiagonal linear system...
        # I   
        l[0] = 1      
        u[0] = 0      
        z[0] = 0
    
        # II
        for i in range(1, n-1):
            l[i] = 2*(xn[i+1] - xn[i-1]) - h[i-1]*u[i-1]
            u[i] = h[i]/l[i]
            z[i] = (alpha[i] - h[i-1]*z[i-1])/l[i]
    
        l[n] = 1
        z[n] = 0
        c[n] = 0
    
        # III... also find b, d in terms of c.
        for j in range(n-2, -1, -1):      
            c[j] = z[j] - u[j]*c[j+1]
            b[j] = (a[j+1] - a[j])/h[j] - h[j]*(c[j+1] + 2*c[j])/3.
            d[j] = (c[j+1] - c[j])/(3*h[j]) 
    
        # Now that we have the coefficients it's just a matter of constructing
        # the appropriate polynomials and graphing.
        for j in range(n-1):
            cub_graph(a[j],b[j],c[j],d[j],xn[j],xn[j+1])
    
        plt.show()
    
    def cub_graph(a,b,c,d, x_i, x_i_1):
        """cub_graph takes the i'th coefficient set along with the x[i] and x[i+1]'th
           data pts, and constructs the polynomial spline between the two data pts using
           the poly1d python object (which simply returns a polynomial with a given root."""
    
        # notice here that we are just building the cubic polynomial piece by piece
        root = poly1d(x_i,True)
        poly = 0
        poly = d*(root)**3
        poly = poly + c*(root)**2
        poly = poly + b*root
        poly = poly + a
    
        # Set up our domain between data points, and plot the function
        pts = arange(x_i,x_i_1, 0.001)
        plt.plot(pts, poly(pts), '-')
        return
    

    If you want to test it, here’s some data you can use to get started, which comes from the
    function 1.6e^(-2x)sin(3*pi*x) between 0 and 1:

    # These are our data points
    x_vals = [0, 1./6, 1./3, 1./2, 7./12, 2./3, 3./4, 5./6, 11./12, 1]
    
    # Set up the domain
    x_domain = arange(0,2, 1e-2)
    
    fx = zeroV(10)
    
    # Defines the function so we can get our fx values
    def sine_func(x):
        return(1.6*e**(-2*x)*sin(3*pi*x))
    
    for i in range(len(x_vals)):
        fx[i] = sine_func(x_vals[i])
    
    # Run cubic_spline interpolant.
    cubic_spline(10,x_vals,fx)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
Edit: I'm looking for solution for this question now also with other programming languages.
--EDIT-- I believe this is a valid question that may have multiple answers (as
EDIT: looking for this: http://diminishing.org/extending-formtastic-with-a-sprinkle-of-jquery (If this works I'll answer my own question) I've
Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
EDIT: This question is more about language engineering than C++ itself. I used C++
Edit: I have solved this by myself. See my answer below I have set
Edit: The below question was answered by this . I have a new updated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.