Software Listing of Author : "John D'errico"
- A suite of minimal bounding objects
- License: Shareware
- Price:


've written quite a few separate tools to generate a minimal bounding object of some ilk. So if you have a set of points in the plane, and wish to generate the minimal bounding... - rectangle - triangle - general quadrilateral - circle - parallelogram - semi-circle there is a tool in here to solve your task efficiently. You will also find minboundsphere, for 3-d data. Inscribed objects are also supported, computed by incircle and insphere. Caveat - if you have only an image, don't expect these tools to work directly. (The image processing toolbox can do much here anyway - look there first.) In order to use these tools, I require a set of extracted points.
- Publisher: John D'Errico
- Date Released: 09-06-2013
- Download Size: 266 KB
- Download
- Platform: Matlab, Scripts
- arclength
- License: Shareware
- Price:


The arclength function computes the length of a general space curve. This is not too difficult if the curve is assumed to be piecewise linear (a one liner) but if the curve is to be a smoothly interpolated spline, then the problem becomes more difficult. A nice way to solve this problem for the cubic case is to formulate it as a numerical integration, whereupon the solution can be gained from quadgk. Carefully written code breaks the problem into a series of integrals between each pair of breaks on the curve to avoid the derivative singularities one would find otherwise.
- Publisher: John D'Errico
- Date Released: 02-06-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- distance2curve
- License: Shareware
- Price:


I've seen many people ask for a way to find the closest point on a curve from some given point in space. If the curve is a piecewise linear one, this is not too difficult, since this reduces to finding the closest point on a line segment, and then testing each line segment. (With care, you need not test EVERY line segment.) For a cubic spline however, this becomes more difficult, but still doable in a mathematical sense without an explicit optimization. Not too much more than roots is necessary. Distance2curve allows you to specify a set of general points in an n-dimensional space as a connected space curve. A spline (or pchip) is then fit in terms of arc length along the curve, and the closest point identified.
- Publisher: John D'Errico
- Date Released: 23-04-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- Estimatenoise
- License: Freeware
- Price: 0.00


Some curve fitting or smoothing tools can benefit from knowledge of the noise variance to expect on your data. Kalman filters use this information, also some spline fitting tools. So I wrote a function to extract the noise variance from a signal vector. It also works on any specified dimension of an array. A few examples of this code in use: Simple linear data, with purely additive N(0,1) gaussian noise: t = 0:10000; x = t + randn(size(t)); mv = estimatenoise(x) mv = 1.0166 Gaussian noise added to a sine wave (Nominal variance = 0.01) t = linspace(0,1,1000)'; x = sin(t*50) + randn(size(t))/10; mv = estimatenoise(x) mv = 0.0096887 Pure gaussian noise, with a nominal variance of 9. (Note that var would have been a better estimator for this particular case...) mv = estimatenoise(3*randn(2,3,1000),3) mv = 9.6584 8.2696 8.632...
- Publisher: John D'Errico
- Date Released: 11-05-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- fminsearchbnd
- License: Shareware
- Price:


Fminsearch does not admit bound constraints. However simple transformation methods exist to convert a bound constrained problem into an unconstrained problem. Fminsearchbnd is used exactly like fminsearch, except that bounds are applied to the variables. The bounds are applied internally, using a transformation of the variables. (Quadratic for single bounds, sin(x) for dual bounds.) The bounds are inclusive inequalities, which admit the boundary values themselves, but will not permit ANY function evaluations outside the bounds. Note that fminsearchbnd allows the user to exactly fix a variable at some given value, by setting both bounds to the exact same value. Example usage: rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2; % unconstrained fminsearch solution fminsearch(rosen,[3 3]) ans = 1.0000 1.0000 % Lower bounds, no upper...
- Publisher: John D'Errico
- Date Released: 03-02-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- Fminsearchcon
- License: Freeware
- Price: 0.00


Nonlinear optimization problems with bound constraints can be solved using FMINSEARCHBND (as well as using many other tools.) For the user who has a problem with linear inequality constraints and/or general nonlinear inequalities as well as bound constraints, FMINSEARCHCON provides a toolbox free alternative, based on FMINSEARCH. FMINSEARCHCON provides a simple, though carefully coded optimization code based on the Matlab provided FMINSEARCH. It uses transformations to deal with the bound constraints, but a penalty function approach to deal with the inequality constraints. The actual objective function is never evaluated outside of the bounds, as the bounds are always tested first. Note that while FMINSEARCHCON does everything that FMINSEARCHBND is capable of and more, I've chosen to provide both these tools independently because...
- Publisher: John D'Errico
- Date Released: 21-01-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- Fminspleas
- License: Freeware
- Price: 0.00


I need to thank Duane Hanselman for suggesting this great idea. Fminspleas is a simple nonlinear least squares tool that fits regression models of the form Y = a1*f1(X,C) + a2*f2(X,C) + ... + an*fn(X,C) X can be any array, so it works on multidimensional problems, and C is the set of only intrinsically nonlinear parameters. f1, f2, etc., must return a column vector result, of the same length as Y. Because the optimization (in this case, fminsearch) need only work on the intrinsically nonlinear parameters, far fewer function evaluations are required. The example I give in the help took only 32 function evaluations to estimate 2 linear parameters plus 1 nonlinear parameter, versus over 300 evaluations had I just called fminsearch directly. Fminspleas now allows you to specify bound constraints on the nonlinear parameters only. I'll...
- Publisher: John D'Errico
- Date Released: 10-04-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- inpaint_nans3
- License: Shareware
- Price:


I am occasionally asked for a 3-d version of my useful inpaint_nans tool. This does it, although I only included the most commonly used methods from inpaint_nans.
- Publisher: John D'Errico
- Date Released: 27-04-2013
- Download Size: 297 KB
- Download
- Platform: Matlab, Scripts
- interparc
- License: Shareware
- Price:


A common request is to interpolate a set of points at fixed distances along some curve in space (2 or more dimensions.) The user typically has a set of points along a curve, some of which are closely spaced, others not so close, and they wish to create a new set which is uniformly spaced along the same curve. When the interpolation is assumed to be piecewise linear, this is easy. However, if the curve is to be a spline, perhaps interpolated as a function of chordal arclength between the points, this gets a bit more difficult. A nice trick is to formulate the problem in terms of differential equations that describe the path along the curve. Then the interpolation can be done using an ODE solver. As an example of use, I'll pick a random set of points around a circle in the plane, then generate a new set of points that are equally...
- Publisher: John D'Errico
- Date Released: 08-04-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- LSE
- License: Shareware
- Price:


This submission was written by request - as a tool to handle linear least squares problems, subject to linear equality constraints that may potentially be rank deficient. (It handles problems with full rank constraints of course too.) In the event of a rank deficient constraint system, it tests for consistency of the constraints. I added a few other features to LSE: - It allows multiple right hand sides to the least squares problem, fully vectorized of course. - Weights may be supplied. - You are offered a choice of least squares solvers, either backslash or pinv. LSE solves the problem (for an unknown vector x) argmin norm(A*x - b) subject to the constraints C*x = d As an example, consider the random system A = rand(10,3); b = rand(10,1); With a rank deficient constraint set C = [1 1 1;1 1 1]; d = [1;1]; X = lse(A,b,C,d) X...
- Publisher: John D'Errico
- Date Released: 16-02-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- Moving window standard deviation
- License: Freeware
- Price: 0.00


Occasionally I see a request for computation of a running, windowed standard deviation. This is easily accomplished using filter and the alternative formula for the standard deviation: std = sqrt((sum(x.^2) - n*xbar.^2)/(n-1)). Movingstd allows you to specify forward, backward or central windows of any desired length. It patches the ends, shortening the window as necessary.
- Publisher: John D'Errico
- Date Released: 06-01-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- Movingslope
- License: Freeware
- Price: 0.00


The gradient function in Matlab allows you to compute the slope of a curve along its entire length. But if your curve is a noisy one, then gradient will also be noisy. In this event one might desire to fit a moderately low order polynomial regression model in a sliding window, then differentiate that model. (Like a Savitzky-Golay filter.) All of this can be done efficiently in Matlab using filter. Note that this tool does not constrain the length of the support to be even or odd. Also, this tool uses pinv to generate the filter coefficients - a more stable and accurate methodology than does the sgolay tool on the file exchange. A few examples of movingslope in action: Estimate the first derivative using a 7 point window with first through fourth order models in the sliding window. Note that the higher order approximations provide...
- Publisher: John D'Errico
- Date Released: 19-06-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- Nonlinear Regression Shapes
- License: Freeware
- Price: 0.00


The art of fitting a nonlinear regression model often starts with choosing a model form. This submission is an attempt to teach the reader a simple but general paradigm for their models as a sum of fundamental shapes that are then shifted and scaled to fit the data. I've included a bestiary of fundamental forms, each of which has been plotted. Each form also has a description of some fundamental characteristics, such as limits and other special values. Who might wish to read this submission? Anyone who is interested in fitting an empirical model to their (1-d) data, although many of the ideas in here are applicable to problems in higher dimensions too. Please e-mail me of any errors I've made, as well as any interesting functional forms that I've failed to include in the bestiary
- Publisher: John D'Errico
- Date Released: 04-04-2013
- Download Size: 41 KB
- Download
- Platform: Matlab, Scripts
- Optimization Tips and Tricks
- License: Freeware
- Price: 0.00


New users and old of optimization in MATLAB will find useful tips and tricks in this document, as well as examples one can use as templates for their own problems. Use this tool by editing the file optimtips.m, then execute blocks of code in cell mode from the editor, or best, publish the file to HTML. Copy and paste also works of course. Some readers may find this tool valuable if only for the function pleas - a partitioned least squares solver based on lsqnonlin. This is a work in progress, as I fully expect to add new topics as I think of them or as suggestions are made. Suggestions for topics I've missed are welcome, as are corrections of my probable numerous errors. The topics currently covered are listed below. Contents 1. Linear regression basics in matlab 2. Polynomial regression models 3. Weighted regression models 4....
- Publisher: John D'Errico
- Date Released: 10-02-2013
- Download Size: 92 KB
- Download
- Platform: Matlab, Scripts
- polyfitn
- License: Shareware
- Price:


Polyfitn is an extension of polyfit, allowing the user to create models with more than one independent variable. It also allows the user to specify a general model, for example, a quadratic model, with constant and quadratic terms, but no linear term. For example, to fit a polynomial model to points selected from a cosine curve, we will only need the even ordered terms. x = -2:.1:2; y = cos(x); p = polyfitn(x,y,'constant x^2 x^4 x^6'); p.Coefficients ans = [0.99996 -0.49968 0.041242 -0.0012079] The coefficients won't be exact of course, as I used only a finite number of terms for what is essentially a truncated Taylor series, and I had only a finite amount of points to build the model from. The exact first 4 coefficients for a cosine series would have been: >> [1 -1/2 1/24 -1/720] ans = 1 -0.5 0.041667 -0.0013889 so we got the...
- Publisher: John D'Errico
- Date Released: 02-01-2013
- Download Size: 51 KB
- Download
- Platform: Matlab, Scripts
- putvar (Sripts)
- License: Shareware
- Price:


(This function was suggested to me as a counterpart to the uigetvar function.) The putvar tool allows you to assign a variable from a function workspace directly into the base workspace. I can envision this tool having value in one of two ways. First, during a debugging session, one might wish to save one or more variables from the function workspace into the base workspace. This way those variables will still exist after the debugger has terminated and you have exited from the function. A second use for this tool is as an alternative way to return a variable (or variables) from a function, possibly a GUI, during the operation of that GUI. As an example of the operation of this code, here is a simple function that uses putvar. Note that you can provide either a variable in the workspace as input, or the name itself of the variable....
- Publisher: John D'Errico
- Date Released: 22-03-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
- RMSEARCH
- License: Freeware
- Price: 0.00


Some optimization problems have very simple surfaces to optimize. The optimizer simply proceeds downhill to the unique minimizer and returns happily - all is good in the world. Sadly, more often the objective function has multiple local minimizers, you as the user provide poor starting values, and the optimization returns what is essentially junk for a solution. My response would typically be that you needed to provide better starting values. At that time, I'd also try to explain the idea of a basin of attraction for any minimum. Its the set of points that when used as starting values, will allow a given optimizer to converge to a given local minimum. Starting values that lie in the basin of attraction of the global minimizer are not always that easy to choose for all problems. One solution is to use a randomly multiply started...
- Publisher: John D'Errico
- Date Released: 20-01-2013
- Download Size: 102 KB
- Download
- Platform: Matlab, Scripts
- The Fibonacci Sequence
- License: Shareware
- Price:


Often I see students asking for help on a tool to compute the Fibonacci numbers. Or, I'll find them asking for help on a Project Euler problem. Or, a student has been assigned the problem of computing the fibonacci numbers using a recursive implementation. After all, these numbers lend themselves splendidly to teaching a student to use recursion. The problem is that a direct, simple, recursive scheme is a poor one for the Fibonacci numbers, unless the recursion is written very carefully. This tool teaches you how to compute the Fibonacci numbers in a variety of ways, good, bad, ugly. I teach the concept of memoization, a vitally important tool for many recursive schemes, not only for Fibonacci numbers. (If you do teach a student recursion, use it as an excuse to also teach them about memoization!) Of course, I also employ some...
- Publisher: John D'Errico
- Date Released: 27-06-2013
- Download Size: 399 KB
- Download
- Platform: Matlab, Scripts
- User Defined Constants
- License: Shareware
- Price:


Did you ever want the ability to define a special constant that is seen by and used in all of your functions, without needing to create global variables? For example, suppose you used the golden ratio often in your work. Rather than creating a global variable named phi, or being forced to pass a variable around as an argument to your functions, I'll just use defcon to do the work for me. >> defcon('phi',(1+sqrt(5))/2) Here, I used defcon to create a new function called phi on my search path, that acts like the ones and zeros functions. >> format long g >> phi ans = 1.61803398874989 >> phi(1,2) ans = 1.61803398874989 1.61803398874989 phi is now a matlab function, that can be used inside other functions without any further effort. In fact, phi even has help defined, done automatically by defcon. >> help phi phi - User Defined...
- Publisher: John D'Errico
- Date Released: 01-04-2013
- Download Size: 10 KB
- Download
- Platform: Matlab, Scripts
