Alternative iterative solvers

This post looks at alternative solutions to a problem posted on Quora:

How can 2^x = 10x be solved?

One of the alternatives requires a new function linking to Scipy that has been added to the pyScipy module, that can be downloaded from:

py_SciPy.zip

For more details of the many functions linking Excel to Scipy (and related Python modules), through pyxll, see:

Scipy functions with Excel and pyxll and the following posts.

A quick plot of the function 2^x – 10x shows that there are solutions at x just greater than 0 and just less than 6:

The simplest and easiest way to get a close estimate of the exact solutions is to use the Excel Goal Seek function (although this wasn’t mentioned in any of the replies on Quora!). It is found under What-if Analysis on the Data tab:

The required inputs are an initial guess of the ‘x’ value and the function to be solved. In this example, since we are looking for the x value to make the function zero, I have multiplied it by 1 million for greater precision. Goal Seek adjusts the value in cell D6 to make cell E6 very close to zero:

In this case starting with x = 1 returns a result of 0.107755, and x = 5 returns 5.877011.

The most frequent solution given in the Quora discussion was to use the Newton-Raphson method to find the two solutions by iteration. This can be done in Excel using the py_Brent function (which is a refinement of the Newton-Raphson method), that calls the Scipy brentq function, and is included in the Scipy download linked above. There is also a VBA version that can be downloaded from:

ItSolve.zip

See Newton-Raphson and Brent’s Method – Solver examples for background information and examples.

The py_Brent function requires an input lower and upper bound for the solution, which must evaluate to values with different signs, and a text function in Python lambda format. Note that exponentiation must be in Python format (**), rather than ^.

The third alternative is to use the Lambert W function, which is defined by W(z) = w, where we^w = z. The function has an infinite number of solutions with complex numbers, but for real results there are just two, indicated as W(0) and W(-1). See Lambert W function for more details.

The Lambert W function can be used to find the desired results as shown below:

Scipy has a lambertw function, that can be called from Excel using the pyScipy py_CallfuncS function:

The required input is the function name (lambertw), the function input value and output index (0 or -1), entered in a column, and the number of input rows. The returned value is divided by LN(2) to find the required results.

The py_CallfuncS function does not allow for input or output of complex numbers. To allow that, I have added the py_LambertW function to the pyScipy module, included in the download at the top of the post.

For the current example the input Z value is specified in a single cell, but complex numbers may be specified in two adjacent cells, or multiple complex numbers in a 2 column range. The function also allows adjustment of the target tolerance value, and the option to return complex numbers. By default results are returned as real for real input or complex for complex input, but the function result may be complex for real input with K other than 0 or -1, in which case RtnComplex should be set to True.

The final results using the LambertW functions are as for the other options:

The examples shown above can be downloaded from: Lambert.zip

Posted in Excel, Link to Python, Maths, Newton, NumPy and SciPy, PyXLL, UDFs, VBA | Tagged , , , , , , , , | Leave a comment

py_xlCBA – Supports with defined deflections

As stated in the previous post, this post will cover the calculation of beam actions and displacements when one or more supports have a defined vertical displacement, using the pyCBA beam analysis package. The Python code has been updated since the previous post, so the revised code and associated spreadsheets should be downloaded from:

py_xlCBA.zip

The procedure used for analysis with specified support deflections in the latest update is:

  • Find results with no displacement at all fixed supports. This is easily done using the original input data ranges, but select only the first three columns of the Supports data.
  • At the first support with a specified displacement, set the support translation restraint to 0 and apply unit upward force.
  • Calculate the force required for the specified displacement:
    Specified displacement/displacement due to unit force
  • Apply this force at the support, and add the resulting actions and displacements to the results from the first stage.
  • Repeat for each support with a specified displacement.

This procedure is shown step by step in the screenshots below and is included on the DefStages sheet of the py_CBA-def-stages.xlsb file. The final results are compared with the results of a Strand7 analysis, with near exact agreement.

Analyse with no deflection at fixed supports:

At the first fixed support, set translation restraint to zero, and apply unit upward force:

Apply force required for the specified displacement, and add results to the first stage results:

Repeat for Support 2:

and Support 3:

Final results are in near exact agreement with Strand7:

Strand7 output for the same beam:

Posted in Beam Bending, Excel, Finite Element Analysis, Frame Analysis, Link to Python, Newton, PyXLL, Strand7, UDFs | Tagged , , , , , , , , , | Leave a comment

py_xlCBA update

Following the previous post the py_xlCBA spreadsheet and associated code have been updated. The updated files, including open-source code, can be downloaded from:

py_xlCBA.zip

Note that the link in the previous post was downloading the wrong file, and has just been corrected.

The main updates are:

  • The py_CBA function now returns an array of beam or reaction results, rather than the py_CBA cache object.
  • The cache_object is now generated using the py_CBAcache function.
  • Beam or reaction results can now be extracted from the cache object using the pyCBARes or pyCBAReact functions.
  • The beam results now include beam slopes as well as deflections.
  • The reaction results include the beam slope and deflection at each support, as well as reaction forces and bending moments.
  • Supports may now be allocated a specific vertical deflection, as well as the displacement and rotation fixed or spring restraints. Note that if a non-zero displacement is specified any spring stiffness for deflections at that support is ignored, but spring or fixed rotation restraints are still applied.

The new input and output is shown below:

  • The supports input may now be three columns as before, or four columns if specified support displacements are required. Note that there must be at least one support with zero (or blank) displacement.
  • The main beam results array is now generated using the py_CBA function.
  • The results cache object is returned as BeamResults@x (lower left), using the py_CBAcache function.
  • As before, the CBA generated Python graphs may be returned immediately below the cache object.
  • Results may be extracted from the cache using the py_CBARes or py_CBAReact functions.

As before, the latest functions have been checked against Strand7 analyses of 15 different span arrangements, each with 6 different support conditions. The py_CBA results were generated in the py_CBA-Check26-2.xlsb spreadsheet, and all results are compared with Strand7 in Check py_CBA-16Jan26.xlsb. The results for the different support conditions are copied to six different sheets, and the results for any span type can be displayed by entering the Span Type number in cell Y2. The results are very close, except that currently where a specified deflection analysis is carried out, and the first support is at X = 0, the shear force at that support is shown as zero, as shown below:

The results have also been compared against the py_Conbeam function on the py_CBA-Check v Conbeam-support def.xlsb spreadsheet, with near exact agreement. Results are shown below for a 3 spam beam with two cantilevers, and 3 supports with specified deflections:

The next post will look at the procedure used to calculate the beam actions and deflections with specified deflections at one or more supports.

Posted in Beam Bending, Excel, Finite Element Analysis, Frame Analysis, Link to Python, Newton, PyXLL, Strand7, UDFs | Tagged , , , , , , , , , , | 1 Comment

Calling PyCBA from Excel

PyCBA is a Python based continuous beam analysis program. From the on-line docs:

PyCBA is a python implementation of the Continuous Beam Analysis program, originally coded in Matlab and subsequently ported to C++ (by Pierrot).

PyCBA is for fast linear elastic analysis of general beam configurations. It uses the matrix stiffness method to determine the displacements at each node. These are then used to determine the member end forces. Exact expressions are then used to determine the distribution of shear, moment, and rotation along each member. Cumulative trapezoidal integration is then used to determine the rotations and deflections along each member. The program features:

  • Multiple load types: point load; uniformly distributed load; patch load, and; moment load;
  • Spring supports, both vertical and rotational, enabling it to be used as part of a subframe analysis;
  • Results are output at 100 (user can change) positions along each span, enable accurate deflection estimation.

One of the main functions of PyCBA is that the basic analysis engine forms the basis for higher-level analysis. Current PyCBA includes modules for:

  • Influence line generation
  • Moving load analysis for bridges, targeted at bridge access assessments

I have set up a spreadsheet based on py_Conbeam that converts the py_Conbeam input to PyCBA format, then runs the PyCBA analysis and returns the results to the spreadsheet. The spreadsheet and associated files can be downloaded from:

Edit 18 Jan 2026: This link was previously downloading the wrong file. The link has now been corrected.

py_xlCBA.zip

The spreadsheet requires the following programs in addition to Excel:

  • Python, Numpy, Scipy, and Matplotlib
  • PyCBA, which can be installed with pip. See the documentation page for details.
  • pyxll to transfer data between Excel and Python, including generation of dynamic Matplotlib graphics in Excel.

The spreadsheet uses 4 new Python functions, which have been added to the Beam_Act_2.py module, which also includes the py_Conbeam function and related functions.

In addition to the Python code the download file includes:

  • py_xlCBA.xlsb: The new spreadsheet.
  • py_CBA-Check v Conbeam.xlsb: Check of py_CBA results against py_Conbeam.
  • py_CBA-Check26-1.xlsb: Check of py_CBA results against Strand7 results for multiple different span and support conditions
  • Check py_CBA-2Jan26.xlsb: Summary of the Strand7 check results
  • py_xlCBA-Strand7.zip: Strand7 data file and results

The bulk of the work is done by the new py_CBA function which converts the input data from py_Conbeam format, creates a BeamAnalysis object, and runs the analyze method. The resulting beam_results object is returned to Excel as a cache_object, from which other pxll based Excel functions can extract and display the required results values.

Optionally, the function will also return graphs of the beam actions and deflections:

A table of beam shear forces, bending moments, and deflections at specified locations along the beam is returned by the pyCBARes function, with the results cache object and a list of output points as input:

Support reactions are returned by the pyCBAReact function. Note that currently reactions are only returned for supports that are fixed against deflection and/or rotation. For supports with spring restraints the associated reactions are returned as zero.

The py_CBAA function calls the py_CBA function and returns results as numeric arrays, rather than a cache object. The values returned are determined by the specified “out” index. Currently the available results are limited to the input files generated by py_CBA. See the “functions” sheet of the spreadsheet for a list of the available options.

As for the py_Conbeam spreadsheet, the check against Strand7 showed near exact agreement for all cases:

Note that currently it is not possible to specify support deflections in PyCBA, so the check runs are limited to spans type 1 to 4, which have either fixed or spring restraints at each support.

Posted in Beam Bending, Excel, Frame Analysis, Link to Python, Newton, NumPy and SciPy, PyXLL, Strand7, UDFs | Tagged , , , , , , , , , | 1 Comment

py_ConBeamU and Strand7 check updates-2

Recent work with the py_ConBeamU spreadsheet found that it was returning incorrect results when the last support had a rotation spring restraint. In spite of the checks against Strand7 having 90 different support conditions, none of them included this one, so I have modified the check files to include it. The modified Python code (version 1.05), and the new check runs, can be downloaded from:

py_ConBeamU.zip

See py_ConBeamU and Strand7 check updates for more details of the download files.

The VBA version did not have this problem, but I have updated the check runs to be consistent with the Python version, and these can be downloaded from:

ConBeamU.zip

The results from the previous version of py_ConbeamU and the latest version are shown below for the restraint case causing the problem:

The updated file now agrees almost exactly with the Strand7 results for this load case (Span Type 14 on “Sheet 6”):

Posted in Beam Bending, Excel, Link to Python, Newton, PyXLL, Strand7, UDFs | Tagged , , , , , , , | Leave a comment