Menu

#254 Fit complex function

open
nobody
None
5
2020-09-20
2020-09-18
No

Hello everyone,

I would like to fit a complex function. However, also in the fit function "using" requires floating numbers and cannot handle complex numbers.
My data file has 3 columns: x, real component of f(x), imaginary component of f(x)
I have managed so far with a work around using multibranch fitting:

i={0,1} #imaginary unit

#a complex function
f(x) = (a+b*i)*x

#multibranch fit: 
#fit first real part, then imaginary part
g(x,y) = (y==0 ? real(f(x)) : imag(f(x)))
fit g(x,y) "file.dat" u 1:-2:(column(-2)==0 ? $2:$3) via a,b

however, to do this I needed to duplicate the data in the file to be able to run this multibranch fit (block 0 is used to fit the real part, block 1 for the imaginary part).

Now my question:
is there anyway to fit a complex function more elegant and/or is there anyway to read in the data file twice without having to duplicate the data within the file?

For the latter I also managed using CoreUtils with the "cat" command:

fit g(x,y) '< cat "file.dat" "newlines.txt" "file.dat"'

where "newlines.txt" contains only two empty new lines, so that the second time file .dat is loaded as a new block.

Thanks for your help!
Best,
Alex

For more info about CoreUtils in gnuplot see e.g. here:
https://stackoverflow.com/questions/11549004/gnuplot-plot-data-from-two-files-in-one-x-coordinate-in-other-y
http://gnuplot.10905.n7.nabble.com/combining-data-from-different-input-files-td3528.html
http://gnuwin32.sourceforge.net/packages/coreutils.htm

Discussion

  • Alexander Stangl

    Thanks for your reply.
    This blogspot describes bascially the same approach. So for gnuplot duplicating the data (or reading it in twice with CoreUtilities) and fitting real and complex data via multi-branch fit seems to be the best option with good fitting results.

     

Log in to post a comment.