Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

using output of scipy.interpolate.UnivariateSpline later in python or in Matlab without needing original datapoints

Date: 2022-09-17 15:48:01

I'm using scipy.interpolate.UnivariateSpline to smoothly interpolate a large amount of data. Works great. I get an object which acts like a function.

Now I want to save the spline points for later and use them in Matlab (and also Python, but that's less urgent), without needing the original data. How can I do this?

In scipy I have no clue; UnivariateSpline does not seem to offer a constructor with the previously-computed knots and coefficients.

In MATLAB, I've tried the Matlab functions spline() and pchip(), and while both come close, they have errors near the endpoints that look kind of like Gibbs ears.

Here is a sample set of data that I have, in Matlab format: 

 

splinedata = struct('coeffs',[-0.0412739180955273 
-0.0236463479425733 0.42393753107602 -1.27274336116436

 0.255711720888164 1.93923263846732 

-2.30438927604816 1.02078680231079 0.997156858475075 

-2.35321792387215 0.667027554745454 0.777918416623834],...

 'knots',[0 0.125 0.1875 0.25 0.375 0.5 0.625 0.75 0.875 0.9999],...

 'y',[-0.0412739180955273 -0.191354308450615 -0.869601364377744

 -0.141538578624065 0.895258135865578 -1.04292294390242 

0.462652465278345 0.442550440125204 -1.03967756446455

 0.777918416623834])

The coefficients and knots are the result of calling get_coeffs() and get_knots() on the scipy UnivariateSpline. The 'y' values are the values of the UnivariateSpline at the  knots, or more precisely: 

 

y = f(f.get_knots())

where f is my UnivariateSpline.

How can I use this data to make a spline that matches the behavior of the UnivariateSpline, without having to use the Curve-Fitting Toolbox? I don't need to do any data fitting in Matlab, I just need to know how to construct a cubic spline from knots/coefficients/spline values. 

Expert Answer:

s: 

You can do it by using the functions _eval_args() and _from_tck() from the class UnivariateSpline. The first one gives returns the spline parameters, which you can store and later create a similar spline object using the the second one.

Here is an example: 

 

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline

x = np.linspace(-3, 3, 50)
y = np.exp(-x**2) + 0.1 * np.random.randn(50)

spl1 = UnivariateSpline(x, y, s=.5)

xi = np.linspace(-3, 3, 1000)

tck = spl1._eval_args

spl2 = UnivariateSpline._from_tck(tck)

plt.plot(x, y, 'ro', ms=5, label='data')
plt.plot(xi, spl1(xi), 'b', label='original spline')
plt.plot(xi, spl2(xi), 'y:', lw=4, label='recovered spline')

plt.legend()
plt.show()

 

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com