Help Fitting this with an invertible function

Stiganator

Platinum Member
Oct 14, 2001
2,492
3
81
If you plot data column 1 vs column 2, you should get a sigmoid type plot.

I have been trying to fit this with some kind of invertible function for a while now. The only invertible way I've found is using 3 piecewise linear functions.

I've been using matlab's Curve Fitting Toolbox, but I only get decent results if I use non- invertible functions.

If necessary the top plateau can be excluded (that how I got decent fits for the rest).

It does have to be invertible though.

<meta name="googlebot" content="nosnippet">
<meta name="googlebot" content="noarchive">
 
Last edited:

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
There are lots of sigmoidal functions that will work, but which one is appropriate depends on what you're trying to do. So, what exactly are you trying to accomplish? I assume this is some sort of calibration plot. If so, can you just correlate it the other way?

edit: This is what I am talking about. All the way at the bottom of the page, you'll see that the sigmoid function itself has a physical meaning since it is the solution to a differential equation. If you have a similar system, then the sigmoid function is perfect. Otherwise, there are many other sigmoidal functions that will do, just depending on what the application is and how well rooted in physics the model/fit needs to be.
 

Stiganator

Platinum Member
Oct 14, 2001
2,492
3
81
What I'm doing is inputting a range of current into a neuron and measuring how much spike advance and delay there is. Eventually, this will be the used with some additional steps on an implantable device, so one equation is ideal. Even with multiple linear equations, the curves on the sigmoid end up poorly defined.

I think I tried something of that form, but I'm going to double check.
 

Stiganator

Platinum Member
Oct 14, 2001
2,492
3
81
Yeah I did try that in the curve fitting tool with form 1/(1+exp(-ax))+b to give it a little extra freedom, but the best I got was a weak straight line.

So far it looks like the simplest equation to fit most of the curve is a linear over a quadratic, but that isn't invertible.
 

Stiganator

Platinum Member
Oct 14, 2001
2,492
3
81
The linear could be possible if there was an efficient was to automatically fit n number of linear sections. Is there a command in matlab that will do that.

i.e. multlin(a:),5), a:),7), 5) would fit 5 linear functions to the data set (that is not a real function just an example of what would be nice)
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Originally posted by: Stiganator
The linear could be possible if there was an efficient was to automatically fit n number of linear sections. Is there a command in matlab that will do that.

i.e. multlin(a:),5), a:),7), 5) would fit 5 linear functions to the data set (that is not a real function just an example of what would be nice)
You can do that if you manually specify the intervals, but there's no built-in way to do it automatically. I gotta eat now but I'll take another look at it later and report back.
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
Almost forgot... Anyway, I made a simple MATLAB program that fits multiple lines to the data. It's easily extendable to any number of lines without changing any of the code - just change the number of limits and parameters and it will vary the number of lines accordingly.


Main program:
Code:
load matrix.mat;

x=b(:,1);y=b(:,2);

Parameters=[1 1 1 1 1;%slopes
            1 1 1 1 1];%intercepts

lim=[-inf -1e-10 -1e-11 1e-11 inf];%interval limits for piecewise linear fits

plot(x,y,'k.');%plot data
for i=1:length(lim)-1
    params=Parameters(:,i)';

    a=find(x>lim(i));%all points above limit
    b=find(x<lim(i+1));%all points below limit
    
    xx=x(intersect(a,b));%keep only x in this interval
    yfit=y(intersect(a,b));%keep corresponding y
    
    %Fit the parameters
    [params,SSR]=fminsearch(@(params) LineFit(xx,yfit,params),params);
    
    [SSR,yy]=LineFit(xx,yfit,params);%Get the fitted values
    hold all;plot(xx,yy,'-');%Plot this fitted line
end
Function that compares the line to the data for fitting/plotting purposes:
Code:
function [SSR,yy]=LineFit(x,y,params)
%Fits the sigmoid function to input data (x,y)
yy=params(1).*x+params(2);%compute fitted line
SSR=sum((y-yy).^2);%sum of squared residuals (cost function)

edit: brain fart. How do I post nice, formatted code again? :roll: