+ Reply to Thread
Results 1 to 17 of 17

Linear interpolation of large data sets

  1. #1
    Registered User
    Join Date
    04-02-2012
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Linear interpolation of large data sets

    Hey all,

    I'm an engineering student, and I was recently asked to crunch a lot of data, but I have an issue. The data that the lab produced is a measure of certain optical properties of amorphous silicon through a range of wavelengths, with wavelength being the independent variable (x) in the data set. A sample of the data looks like this:


    210.77812200 1.365183 3.158104
    212.36825600 1.383018 3.203131
    213.95842000 1.422307 3.261899
    215.54863000 1.460486 3.293017
    217.13891600 1.482046 3.325932
    218.72921800 1.513605 3.334321
    220.31958000 1.535114 3.351916

    Where the first column is wavelength (nm), the second column is n, and the third column is k. I need to use this data, but to use it, I need to use integer values for wavelength, and was hoping to use linear interpolation. I interpolated a few by hand:


    211 1.36767 3.16439
    212 1.37889 3.19270

    But the process takes a bit of time, and I need to interpolate from 211 nm to 1700 nm. I didn't see a function for this in excel, so does anyone know a faster way to interpolate this data?

    Thanks,

    --Avren--

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,408

    Re: Linear interpolation of large data sets

    Why not just take the actual wavelength and round it?

    =ROUND(A1,0)

    Hope this helps.

    Pete

  3. #3
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Linear interpolation of large data sets

    Just to be sure: Because you are rounding off values for nm, you need to change the values for n and k? Could you supply the formula that describes the relationship between the 3 factors?
    Sincerely
    S?ren Larsen

    "Give a man a fish, and you'll feed him for a day. Give a man a fishing rod, and he'll steal your yacht!"

  4. #4
    Registered User
    Join Date
    04-02-2012
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Linear interpolation of large data sets

    Sure thing. The relationship looks like this:

    First, you take the difference between the desired value and the lower value of the data the desired value resides between, and divide it by the difference between the higher value and the lower value that describe the region the desired value lies within, ie:

    (211-210.778)/(212.268-210.778)

    This gives the point along the line between the higher and lower values that the value you want lies at as a percentage. You then apply that percentage to the linear relationship of the related data for n and k. A reasonable explanation is found on the wikipedia page for interpolation. This is Linear interpolation.

  5. #5
    Valued Forum Contributor
    Join Date
    03-16-2012
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    992

    Re: Linear interpolation of large data sets

    Quote Originally Posted by Avren View Post
    Sure thing. The relationship looks like this:

    First, you take the difference between the desired value and the lower value of the data the desired value resides between, and divide it by the difference between the higher value and the lower value that describe the region the desired value lies within, ie:

    (211-210.778)/(212.268-210.778)

    This gives the point along the line between the higher and lower values that the value you want lies at as a percentage. You then apply that percentage to the linear relationship of the related data for n and k. A reasonable explanation is found on the wikipedia page for interpolation. This is Linear interpolation.
    Are you then not assuming a linear relationship between nm, n and k? That was the reason for me asking the above question; it might not be linear. In any case, if it is linear, then, assuming your data starts in A1, in D1:

    =B1+(B2-B1)*(ROUND($A1,0)-$A1)/($A2-$A1)

    Copy down and to the right. Does it work?

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,369

    Re: Linear interpolation of large data sets

    Even though this is an Excel forum, this might be easiest in QuattroPro if you have it, because QuattroPro had a nice built in linear interpolation function (@LINTERP). I don't know about other spreadsheets, but Microsoft just doesn't seem to want to put a linear interpolation function into Excel.

    If you are stuck with Excel, here's how you do it. As you've observed, the formula for linear interpolation is easy once you've isolated the interval that contains your desired x value. So the hard part of linear interpolation is locating the interval containing x.

    1) Use the MATCH() function to locate the interval containing x
    2) Use the INDEX() function (4 instances) to extract the x and y values at the endpoints of that interval
    3) Use your formula (or the TREND() function can also work) to determine y at you given x

    I'm not real good at nor fond of trying to build this kind of thing into a single cell. I prefer to spread this out over several cells. I expect someone here is good at nesting these functions in a way that will yield a single cell, if you really need it condensed to one cell.

  7. #7
    Registered User
    Join Date
    04-02-2012
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Linear interpolation of large data sets

    Soren, the data set itself is certainly not linear by any means, but linear interpolation doesn't need linear data. The assumption here is that, for a small interval (in this case a single nanometer or so) a linear relationship will very closely model the actual curve, and yield a passable estimate of the value. Of course, I could apply my functions to all the intervals, but the intervals the integers can be found on are not regularly represented. I can find 211 and 212 in the first two rows, but only 213 between the second two, making copy and paste impossible, and applying the formulas by hand through the data set beyond time consuming and inefficient.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Linear interpolation of large data sets

    If you can use a UDF,


    Please Login or Register  to view this content.
    The formula in F2 and copied across and down is =LInterp($E2,$A$2:$A$8,B$2:B$8)

    The code is at http://www.box.com/s/s0m5gs96jd3stuqa7tpu

    If the data is all monotone as in your example, you could instead use a garden-variety formula in lieu of a UDF.
    Last edited by shg; 04-04-2012 at 11:20 AM.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Registered User
    Join Date
    04-02-2012
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Linear interpolation of large data sets

    I love your results, but by copying down through the data, will this compensate for row 2 and 3 being representative of 2 integers (211 and 212) while rows 3 and 4 are only representative of 1 (213)?

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Linear interpolation of large data sets

    Yes, but you could plot it and see for yourself.
    Last edited by shg; 04-02-2012 at 02:15 PM.

  11. #11
    Registered User
    Join Date
    04-02-2012
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Linear interpolation of large data sets

    Thanks a lot of the help! THis is going to save me quite a bit of time.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Linear interpolation of large data sets

    You're welcome, good luck.

  13. #13
    Registered User
    Join Date
    04-02-2012
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Linear interpolation of large data sets

    So, I'm having trouble with the UDF. I've never loaded one before, and I've been looking at it online, but I'm not getting any information on the subject that I would consider informative. Any help?

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Linear interpolation of large data sets

    Copy it to your desktop, open the VBE in Excel, and drag the file into the Project Explorer window over the VBA project for the workbook of interest.

  15. #15
    Registered User
    Join Date
    04-02-2012
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Linear interpolation of large data sets

    Okay, I've loaded the program and it's running, but I'm amending the code you've provided to read:

    =LInterp($E2,$A2:$A8,B2:B8)

    So the range that's analyzed changes dynamically throughout the spreadsheet.

  16. #16
    Registered User
    Join Date
    04-02-2012
    Location
    Toledo, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Linear interpolation of large data sets

    Ahahaha, never mind, I just needed to adjust the range, this works perfectly. New formula reads:

    =LInterp($E2,$A$2:$A$700,B$2:B$700)

    I forgot to adjust for my range being larger than the example. We all have Tuesdays like this I guess. Thanks so much for your help.

  17. #17
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Linear interpolation of large data sets

    Glad it worked for you, good luck.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1