Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6998115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:22:12+00:00 2026-05-27T20:22:12+00:00

I have a some experimental data (for y, x, t_exp, m_exp), and want to

  • 0

I have a some experimental data (for y, x, t_exp, m_exp), and want to find the “optimal” model parameters (A, B, C, D, E) for this data using the constrained multivariate BFGS method. Parameter E must be greater than 0, the others are unconstrained.

def func(x, A, B, C, D, E, *args):
    return A * (x ** E) * numpy.cos(t_exp) * (1 - numpy.exp((-2 * B * x) / numpy.cos(t_exp))) +  numpy.exp((-2 * B * x) / numpy.cos(t_exp)) * C + (D * m_exp)

initial_values = numpy.array([-10, 2, -20, 0.3, 0.25])
mybounds = [(None,None), (None,None), (None,None), (None,None), (0, None)]
x,f,d = scipy.optimize.fmin_l_bfgs_b(func, x0=initial_values, args=(m_exp, t_exp), bounds=mybounds)

A few questions:

  1. Should my model formulation func include my independent variable x or should it be provided from the experimental data x_exp as part of *args?
  2. When I run the above code, I get an error func() takes at least 6 arguments (3 given), which I assume are x, and my two *args… How should I define func?

EDIT: Thanks to @zephyr’s answer, I now understand that the goal is to minimize the sum of squared residuals, not the actual function. I got to the following working code:

def func(params, *args):
    l_exp = args[0]
    s_exp = args[1]
    m_exp = args[2]
    t_exp = args[3]
    A, B, C, D, E = params
    s_model = A * (l_exp ** E) * numpy.cos(t_exp) * (1 - numpy.exp((-2 * B * l_exp) / numpy.cos(t_exp))) +  numpy.exp((-2 * B * l_exp) / numpy.cos(theta_exp)) * C + (D * m_exp)
    residual = s_exp - s_model
    return numpy.sum(residual ** 2)

initial_values = numpy.array([-10, 2, -20, 0.3, 0.25])
mybounds = [(None,None), (None,None), (None,None), (None,None), (0,None)]

x, f, d = scipy.optimize.fmin_l_bfgs_b(func, x0=initial_values, args=(l_exp, s_exp, m_exp, t_exp), bounds=mybounds, approx_grad=True)

I am not sure that the bounds are working correctly. When I specify (0, None) for E, I get a run flag 2, abnormal termination. If I set it to (1e-6, None), it runs fine, but selects 1e-6 as E. Am I specifying the bounds correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:22:13+00:00Added an answer on May 27, 2026 at 8:22 pm

    I didn’t want to try to figure out what the model you’re using represented, so here’s a simple example fitting to a line:

    x_true = arange(0,10,0.1)
    m_true = 2.5
    b_true = 1.0
    y_true = m_true*x_true + b_true
    
    def func(params, *args):
        x = args[0]
        y = args[1]
        m, b = params
        y_model = m*x+b
        error = y-y_model
        return sum(error**2)
    
    initial_values = numpy.array([1.0, 0.0])
    mybounds = [(None,2), (None,None)]
    
    scipy.optimize.fmin_l_bfgs_b(func, x0=initial_values, args=(x_true,y_true), approx_grad=True)
    scipy.optimize.fmin_l_bfgs_b(func, x0=initial_values, args=(x_true, y_true), bounds=mybounds, approx_grad=True)
    

    The first optimize is unbounded, and gives the correct answer, the second respects the bounds which prevents it from reaching the correct parameters.

    The important thing you have wrong is for almost all the optimize functions, ‘x’ and ‘x0’ refer to the parameters you are optimizing over – everything else is passed as an argument. It’s also important that your fit function return the correct data type – here we want a single value, some routines expect an error vector. Also you need the approx_grad=True flag unless you want to compute the gradient analytically and provide it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some arbitrary pixel data that I want to save as a PNG.
I have some vectors of experimental data that I need to massage, for example:
I am trying to experiment this parameter in MapReduce and I have some question.
I have some data loaded as a np.ndarray and need to convert it to
I have a program that operates on a large set of experimental data. The
I'm using the mclust library for R ( http://www.stat.washington.edu/mclust ) to do some experimental
I'm trying to make an experimental web application which minimises redundant data. I have
I'm trying to build some SQL-like abstraction and I have hit a problem. This
In a recent question I was encouraged to try using some basic data structures
I've seen some similar posts, but nothing quite like this... I have a website

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.