Ahmed Abrar Shayor
Ahmed Abrar Shayor

Reputation: 1

Error showing when using Symfit function in python to fit curve made out of data from excel file

The code was just taken from the Symfit documentation. https://symfit.readthedocs.io/en/stable/examples/ex_fourier_series.html Now I only modified xdata and ydata to read it instead from an excel file using pandas. But the problem is not matching data types as far as I understood from other similar Q/A. Could anyone tell me the solution to this. UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('float64'), dtype('<U1')) -> None

  from symfit import parameters, variables, sin, cos, Fit
  import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt

  def fourier_series(x, f, n=0):
   """
Returns a symbolic fourier series of order `n`.

:param n: Order of the fourier series.
:param x: Independent variable
:param f: Frequency of the fourier series
"""
# Make the parameter objects for all the terms
a0, *cos_a = parameters(','.join(['a{}'.format(i) for i in range(0, n + 1)]))
sin_b = parameters(','.join(['b{}'.format(i) for i in range(1, n + 1)]))
# Construct the series
series = a0 + sum(ai * cos(i * f * x) + bi * sin(i * f * x)
                 for i, (ai, bi) in enumerate(zip(cos_a, sin_b), start=1))
return series

x, y = variables('x, y')
w, = parameters('w')
model_dict = {y: fourier_series(x, f=w, n=12)}
print(model_dict)


# Make step function data
data= pd.read_csv('/content/Vel_Prof.csv')
xdata = ['t']
ydata = ['v']

# Define a Fit object for this model and data
fit = Fit(model_dict, x=xdata, y=ydata)

fit_result = fit.execute()
print(fit_result)

# Plot the result
plt.plot(xdata, ydata)
plt.plot(xdata, fit.model(x=xdata, **fit_result.params).y, ls=':')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

Upvotes: 0

Views: 77

Answers (0)

Related Questions