Skip to content

Instantly share code, notes, and snippets.

@sg-s
Created September 8, 2014 21:51
Show Gist options
  • Save sg-s/aedc21ba579140d5b492 to your computer and use it in GitHub Desktop.
Save sg-s/aedc21ba579140d5b492 to your computer and use it in GitHub Desktop.
How to find the inverse of an arbitrary function in MATLAB

Assume you have a custom function, say, hill.m, that looks like this:

function r = hill(x,xdata)
A = x(1);
k = x(2);
n  =x(3);
r = A*xdata.^n;
r = r./(xdata.^n + k^n);
% when xdata is negative, return 0
r(xdata<0) = 0;	

you want to find the inverse. But simply inverting the equation is messy -- and error prone as you subtract numbers that can't be negative, and take fractional powers that must be real. Imposing these conditions is dirty, and there's a better way to find the inverse numerically using fzero

Assuming the parameters of your Hill function are [10 25 2], and you want to find the point where the value of the function is 9, this point is given by:

xinv = fzero( @(x)(hill([10 25 2],x)-9), 1)
xinv=
  75.000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment