# Mathematica, fitting an arbitrary number of parameters

• Mathematica
• NeoDevin
In summary, the speakers are discussing ways to fit data as a function of a variable x to the real part of a function with a fixed parameter A. One speaker has tried using a numerical fitting method with the first N of the c_n parameters, but has encountered difficulties. Another speaker suggests that the code may be missing a factor and that the use of the symbol N may be causing issues. The first speaker will check and thanks the other speakers for their help.

#### NeoDevin

Let's say I have some data, as a function of a variable $x$. I want to fit this to the real part of the function

$$\frac{A}{1-ix}\left(1+\sum_{n=1}^\infty\frac{c_n}{(1-ix)^n}\right)$$

by numerically fitting the first $N$ of the $c_n$'s ($A$ is fixed). I tried something like

Code:
A = 1; N = 100;
fit = FindFit[data, Re[A(1+Sum[c[k]/(1-i x)^k,{k,1,N}])], Table[c[k], {k,1,N}], x];
Plot[Re[A(1+Sum[c[k]/(1-i x)^k,{k,1,N}])]/.fit, {x,0,10}]

However, this way does not work. For the case $N=5$, I put in the summation explicitly, calling my parameters c1, c2, etc, and it produces different (better) results than the above code. This is not feasible if I want the first 100 or 1000 coefficients, so any help with the above method would be appreciated. Thanks in advance.

Anybody?

It looks like your code is missing the factor 1 / (1-ix)

Also, in Mathematica the symbol N is protected, use n instead.

Redbelly98 said:
It looks like your code is missing the factor 1 / (1-ix)
Yes, it is, but the original code wasn't. That was just me missing it when typing it into here.
DaleSpam said:
Also, in Mathematica the symbol N is protected, use n instead.
That could be the problem, I'll check it later. Thanks for the replies.

## 1. How do I fit an arbitrary number of parameters in Mathematica?

To fit an arbitrary number of parameters in Mathematica, you can use the NonlinearModelFit function. This function allows you to specify the number of parameters you want to fit, and it will automatically find the best-fit values for those parameters.

## 2. Can I use Mathematica to fit data with more than one independent variable?

Yes, you can use the NonlinearModelFit function to fit data with multiple independent variables. You will need to specify the appropriate number of parameters for each independent variable in the function.

## 3. How do I know if my model is a good fit for my data?

Mathematica provides several tools for evaluating the quality of a model fit, including the FitResiduals and FitDeviance functions. These functions can help you assess the overall goodness of fit and identify any outliers or areas where the model may not be accurately representing the data.

## 4. Can I customize the fitting process in Mathematica?

Yes, you can use various options and settings in the NonlinearModelFit function to customize the fitting process. For example, you can specify a specific algorithm or method to use, set bounds for the parameter values, or add constraints to the model.

## 5. How can I visualize the results of my model fit in Mathematica?

Mathematica has built-in functions for visualizing the results of a model fit, such as Plot and ListPlot. You can use these functions to plot the fitted model curve along with the original data points, allowing you to visually assess the fit and any potential discrepancies.