+ Reply to Thread
Results 1 to 13 of 13

Interpolation problem, need hepl

  1. #1
    Registered User
    Join Date
    07-02-2012
    Location
    Serbia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Interpolation problem, need hepl

    Hello everyone, I need to do interpolation of the following table
    1 1,oo2 +0,002 ±0,0010
    2 2,003 +0,003 ±0,0025
    3 3,003 +0,003 ±0,0025
    4 4,004 +0,004 ±0,0030
    5 5,002 +0,002 ±0,0010
    6 6,003 +0,003 ±0,0025
    7 7,003 +0,003 ±0,0025
    8 8,004 +0,004 ±0,0030
    9 9,004 +0,004 ±0,0030
    10 10,003 +0,003 ±0,0025

    So if you enter a value of 1.3 to get the results of the remaining lines,
    Thank you all in advance

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,913

    Re: Interpolation problem, need hepl

    What result(3) do you expect for 1.3? Can I assume you are wanting to linearly interpolate?

    Linear interpolation is not difficult if you are willing to spread it out over several cells. When I do it, it will look something like:

    cell 1) =MATCH(new_x,known_x's) This returns the interval that contains new_x (1.3 in your example)
    cells 2 and 3) =INDEX(known_x's,cell1) and cell1+1 These two return the known_x's for the interval found in cell 1.
    cells 3 and 4) =INDEX(known_y's,cell1) and cell1 +1 These two return the known_y's for the interval found in cell 1.
    cell 5) =TREND(cells3:4,cells2:3,new_x) This does the actual linear interpolation based on the interval containing new_x.

    This post is in the programming section, suggesting you might want a VBA solution. Putting the above into VBA is not difficult. The MATCH function is available to VBA (see VBA help for using worksheet functions in VBA). Once you've found the interval inside the range that contains new_x, the linear interpolation is easy. (Y3-Y1)/(X3-X1)=(Y2-Y1)/(X2-X1) solved for the appropriate variable.

  3. #3
    Registered User
    Join Date
    07-02-2012
    Location
    Serbia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Interpolation problem, need hepl

    If possible I would like visual basic solution,
    and I need a linear interpolation of the lines,
    so that bringing in random value of 1 to 10 (eg 1.3, 1.7, 5.9, 8.7, etc.).

    Thanks a lot

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,913

    Re: Interpolation problem, need hepl

    So, do you need someone to write the code for you? As I said, this isn't a difficult function to write. Here's a skeleton you can work with to build a UDF that you can call like a regular function from the worksheet:
    Please Login or Register  to view this content.
    That was quickly and sloppily done, so check my syntax and my algebra to make sure it is right. You may also want to add some "error handling" to deal with cases where new x is outside of the range of known_x.

  5. #5
    Registered User
    Join Date
    07-02-2012
    Location
    Serbia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Interpolation problem, need hepl

    Can you explain me how to determine the numbers between is written number, I imagined something like this:

    Sub Interpolacija()

    Dim x0 As Double, y0 As Double, z0 As Double, x1 As Double, y1 As Double, z1 As Double
    Dim differenceInX As Double
    Dim differenceInY As Double
    Dim Unos As Double
    Dim Row As Integer

    Unos = InputBox("Unesite velicinu")

    For Row = 2 To 12
    If ThisWorkbook.Sheets("Sheet1").Cells(Row, 1) = Unos Then
    MsgBox "uneta velicina vec postoji"
    Else
    If ThisWorkbook.Sheets("Sheet1").Cells(Row, 1) > Unos Then

    x0 = ThisWorkbook.Sheets("Sheet1").Cells(Row, 1)
    x1 = ThisWorkbook.Sheets("Sheet1").Cells(Row - 2, 1)

    y0 = ThisWorkbook.Sheets("Sheet1").Cells(Row, 1)
    y1 = ThisWorkbook.Sheets("Sheet1").Cells(Row - 2, 1)

    z0 = ThisWorkbook.Sheets("Sheet1").Cells(Row, 1)
    z1 = ThisWorkbook.Sheets("Sheet1").Cells(Row - 2, 1)
    Next Row
    End If
    End If
    End Sub

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,636

    Re: Interpolation problem, need hepl

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

  7. #7
    Registered User
    Join Date
    07-02-2012
    Location
    Serbia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Interpolation problem, need hepl

    Can you explain me how to determine the numbers between is written number, I imagined something like this:
    Please Login or Register  to view this content.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,913

    Re: Interpolation problem, need hepl

    The code you have looks like it should do fine locating the interval containing unos. You appear to have made the mistake of having z0, y0, and x0 all be the same value, so you need to change those statements to extract the correct values from the table. maybe y0 should be y0=thisworkbook.sheets("sheet1").cells(row,2).

    Having located the interval containing unos (which is the most difficult part of linear interpolation in my opinion) it should be as simple as putting the corresponding values into the linear interpolation formula given above. Am I misunderstanding your question?

  9. #9
    Registered User
    Join Date
    07-02-2012
    Location
    Serbia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Unhappy Re: Interpolation problem, need hepl

    Everything is fine, I understand what you say, the only problem is that my code will not want to execute, there is an error and I can not figure out where

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

    Re: Interpolation problem, need hepl

    I just posted a workbook to which I added a linear interpolation routine -- see http://www.excelforum.com/excel-work...-min-data.html.
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,913

    Re: Interpolation problem, need hepl

    Did it give you an error message, or does it just refuse to run? Did it give you a "next without for" type error or indicate an error in the block if's? Your code gave me a next without for message, which suggests that you need to move your end if statements so they are inside the loop.

  12. #12
    Registered User
    Join Date
    07-02-2012
    Location
    Serbia
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Interpolation problem, need hepl

    refuses to work... I didn't a long time deal with Visual Basic, so now all is a problem for me ...

  13. #13
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,913

    Re: Interpolation problem, need hepl

    I'm not sure what to say. when I correct the next without for error, the code seems to run just fine for me.

    Perhaps your installation of VBA doesn't like using Row as a variable name, since Row is also a property of the range collection, causing the compiler to crash over the name confusion.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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