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

  • SEARCH
  • Home
  • 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 6724741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:43:32+00:00 2026-05-26T09:43:32+00:00

I need to (numerically) calculate the first and second derivative of a function for

  • 0

I need to (numerically) calculate the first and second derivative of a function for which I’ve attempted to use both splrep and UnivariateSpline to create splines for the purpose of interpolation the function to take the derivatives.

However, it seems that there’s an inherent problem in the spline representation itself for functions who’s magnitude is order 10^-1 or lower and are (rapidly) oscillating.

As an example, consider the following code to create a spline representation of the sine function over the interval (0,6*pi) (so the function oscillates three times only):

import scipy
from scipy import interpolate
import numpy
from numpy import linspace
import math
from math import sin

k = linspace(0, 6.*pi, num=10000) #interval (0,6*pi) in 10'000 steps
y=[]
A = 1.e0 # Amplitude of sine function

for i in range(len(k)):

  y.append(A*sin(k[i]))

tck =interpolate.UnivariateSpline(x, y, w=None, bbox=[None, None], k=5, s=2)
M=tck(k)

Below are the results for M for A = 1.e0 and A = 1.e-2

https://i.stack.imgur.com/HOmtf.png Amplitude = 1

https://i.stack.imgur.com/W8r3l.png Amplitude = 1/100

Clearly the interpolated function created by the splines is totally incorrect! The 2nd graph does not even oscillate the correct frequency.

Does anyone have any insight into this problem? Or know of another way to create splines within numpy/scipy?

Cheers,
Rory

  • 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-05-26T09:43:33+00:00Added an answer on May 26, 2026 at 9:43 am


    I’m guessing that your problem is due to aliasing.

    What is x in your example?

    If the x values that you’re interpolating at are less closely spaced than your original points, you’ll inherently lose frequency information. This is completely independent from any type of interpolation. It’s inherent in downsampling.

    Nevermind the above bit about aliasing. It doesn’t apply in this case (though I still have no idea what x is in your example…

    I just realized that you’re evaluating your points at the original input points when you’re using a non-zero smoothing factor (s).

    By definition, smoothing won’t fit the data exactly. Try putting s=0 in instead.

    As a quick example:

    import matplotlib.pyplot as plt
    import numpy as np
    from scipy import interpolate
    
    x = np.linspace(0, 6.*np.pi, num=100) #interval (0,6*pi) in 10'000 steps
    A = 1.e-4 # Amplitude of sine function
    y = A*np.sin(x)
    
    fig, axes = plt.subplots(nrows=2)
    for ax, s, title in zip(axes, [2, 0], ['With', 'Without']):
        yinterp = interpolate.UnivariateSpline(x, y, s=s)(x)
        ax.plot(x, yinterp, label='Interpolated')
        ax.plot(x, y, 'bo',label='Original')
        ax.legend()
        ax.set_title(title + ' Smoothing')
    
    plt.show()
    

    enter image description here

    The reason that you’re only clearly seeing the effects of smoothing with a low amplitude is due to the way the smoothing factor is defined. See the documentation for scipy.interpolate.UnivariateSpline for more details.

    Even with a higher amplitude, the interpolated data won’t match the original data if you use smoothing.

    For example, if we just change the amplitude (A) to 1.0 in the code example above, we’ll still see the effects of smoothing…

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need an algorithm to calculate, numerically, the degree of similarity between two drawn
I need to calculate averages, standard deviations, medians etc for a bunch of numerical
I have a couple of numerical datasets that I need to create a concept
Need a function that takes a character as a parameter and returns true if
I was considering trying PLINQ to parallelize some numerical methods which need to be
I have an associative array that I might need to access numerically (ie get
I have a two dimensional array that I need to sort numerically. Here is
I'm writing a Monte Carlo algorithm, in which at one point I need to
This is my first real question of need for any of those Gridview experts
I need to calculate a 1d histogram that must be dynamically maintained and looked

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.