# Levenberg marquardt C coding algorithm

• a.mlw.walker
In summary, you are struggling to adjust your model using the Levenberg-Marquardt algorithm to fit a curve to your data. Your dataset and desired function are more complex than the original surface that the algorithm was designed for, which may be causing issues with convergence. Consider using a different curve fitting algorithm or simplifying your function to improve the results. Good luck!
a.mlw.walker
HI guys. Haven't been here in a while...
I have an algorithm attached that uses the levenberg marquardt non linear curve fitting alogrithm to fit a surface.
My C coding skills are reasonable, however I am struggling to adjust the model so that I can fit my curve to it. I want to use this algorithm as its a nice standalone algorithm as opposed to some of these otherones that come as part of much larger packages.
I have a simple dataset:
double tx[5] = {1, 2, 3, 4, 5};
double tz[5] = {1.2, 2.693, 4.325, 6.131, 8.125};
that I am trying to fit to the following function:
Code:
 for (i = 0; i <sizeof(tz)-1; i++)
{
invtrig = arcsinh(sinh(p[2])*exp(p[0]*(i+1)*2*3.145));
Fitted_Curve[i] = (1/(p[0]*p[1]))*(p[2]-invtrig);
x += pow((tz[i]-Fitted_Curve[i]), 2);

}
return x;
The original surface that the model was trying to fit was:
Code:
double f( double tx, double tz, const double *p )
{
return p[0] + p[1]*tx + p[2]*tz;
}
Due to the simplicity of this algorithm I am struggling to see how to "convert" it all over so that it will successfully solve mine (which i have the solution to). I can get it to compile if I force it to, but it doesn't converge on the correct results at all - heads to #NAN#
I have attached the code, there is one file called myalgorithm.c which I have been working on to get mine to work, and then there is surface1.c which is the example. Obviously only bring one into a project at a time as they both have a main in.
Thanks

#### Attachments

• myalgorithm.zip
22.6 KB · Views: 142

First of all, welcome back to the forum! It's great to see you again.

I understand that you are struggling to adjust your model to fit your curve using the Levenberg-Marquardt non-linear curve fitting algorithm. I have experience with this algorithm and I would be happy to offer some advice.

Firstly, it's important to understand that this algorithm works by minimizing the sum of the squared residuals between the fitted curve and the actual data points. In order to do this, it requires an initial guess for the parameters of your model (in this case, p[0], p[1], and p[2]). The algorithm then iteratively adjusts these parameters until it finds the best fit for your data.

In order to successfully use this algorithm, you need to make sure that your model is appropriate for your data. Looking at the original surface that the model was trying to fit, it seems like a simple linear function. However, your dataset and desired function are more complex, involving trigonometric functions and exponential functions. This may be the reason why the algorithm is not converging on the correct results.

One suggestion I have is to try using a different curve fitting algorithm that allows for more complex functions, such as the Levenberg-Marquardt algorithm within the GNU Scientific Library (GSL). This library has a variety of curve fitting algorithms that may better suit your needs.

Another option is to simplify your desired function to make it more similar to the original surface that the algorithm was designed for. This may make it easier for the algorithm to converge on the correct results.

I hope this helps and good luck with your project! Let us know if you have any further questions.

## 1. What is the Levenberg-Marquardt C coding algorithm?

The Levenberg-Marquardt C coding algorithm is a mathematical optimization method used to solve nonlinear least squares problems. It is commonly used in data fitting and curve fitting applications to find the best fit for a given set of data points.

## 2. How does the Levenberg-Marquardt C coding algorithm work?

This algorithm combines the steepest descent method with the Gauss-Newton method. It starts with an initial guess for the parameters and iteratively improves the solution by updating the parameters using a combination of the steepest descent and Gauss-Newton methods. This allows for a faster convergence compared to using either method alone.

## 3. What are the advantages of using the Levenberg-Marquardt algorithm over other optimization methods?

The Levenberg-Marquardt algorithm is known for its efficient and robust performance in solving nonlinear least squares problems. It is also relatively easy to implement and can handle a wide range of problems, including those with a large number of parameters. Additionally, it provides a good trade-off between speed and accuracy.

## 4. Can the Levenberg-Marquardt C coding algorithm handle noisy data?

Yes, the Levenberg-Marquardt algorithm is designed to handle noisy data. It does this by adjusting the step size during each iteration based on the curvature of the objective function. This helps to prevent the algorithm from getting stuck in local minima and allows it to find a solution that is more robust to noise in the data.

## 5. Are there any limitations to using the Levenberg-Marquardt C coding algorithm?

Like any optimization method, the Levenberg-Marquardt algorithm has its limitations. It may not work well for highly nonlinear problems with a large number of parameters. It also requires an initial guess for the parameters, which can be difficult to determine for certain problems. Additionally, it may not always converge to the global minimum, but rather a local minimum, depending on the initial guess and the problem itself.

• Programming and Computer Science
Replies
1
Views
876
• Differential Equations
Replies
2
Views
2K
• Programming and Computer Science
Replies
9
Views
2K
• Programming and Computer Science
Replies
3
Views
1K
• Programming and Computer Science
Replies
3
Views
1K
• Programming and Computer Science
Replies
1
Views
1K
• Programming and Computer Science
Replies
9
Views
1K
• Engineering and Comp Sci Homework Help
Replies
3
Views
1K
• Programming and Computer Science
Replies
4
Views
1K
• Engineering and Comp Sci Homework Help
Replies
4
Views
1K