Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fitting spline 1D curve failing #97

Open
henigm opened this issue Sep 15, 2021 · 0 comments
Open

fitting spline 1D curve failing #97

henigm opened this issue Sep 15, 2021 · 0 comments

Comments

@henigm
Copy link

henigm commented Sep 15, 2021

I was trying to fit a 1D cubic spline to sample points, however i don't seem to get correct results. Can someone point me what i am missing here.

import numpy as np
from matplotlib import pyplot as plt
import pygpuspline.gpuspline as gs
import pygpufit.gpufit as gf

x = [1,2,3.5,5,6,7,10]
y = [0,3,5,7,8,10,12]
x1 = np.array(x, dtype=np.float32)
y1 = np.array(y, dtype=np.float32)

pts = np.array([x,y], dtype=np.float32)
coeff = gs.spline_coefficients(pts)
pts_flat = np.reshape(y1, (1, y1.size))

spline_fit_initial_parameters = np.array([[1,1,1,1]], dtype=np.float32)
tolerance = 1e-30
max_n_iterations = 1000
estimator_id = gf.EstimatorID.LSE

parameters_spline, states_spline, chi_squares_spline, n_iterations_spline, time_spline = gf.fit(pts_flat,
                                                                                                None,
                                                                                                gf.ModelID.SPLINE_1D,
                                                                                                spline_fit_initial_parameters,
                                                                                                tolerance,
                                                                                                max_n_iterations,
                                                                                                None, estimator_id,
                                                                                                coeff)

def cubic_func(x,a,b,c,d):
    return a * np.power(x,3) + b * np.power(x,2) + c * (x) + d
sample_x = np.arange(0,10,0.1)
sample_y = [cubic_func(each, *parameters_spline[0]) for each in sample_x]
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5))
ax1.plot(x, y)
ax2.plot(sample_x,sample_y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant