| Posted: 08:18am 07 Mar 2017 |
Copy link to clipboard |
 Print this post |
|
This post describes a MMBASIC program that can be used to linearly interpolate tabular data provided by the user. Although written and checked out on the MMX, it should work for all versions of the Micromite.
Here's a typical user interaction with a demo program that shows how to interact with the subroutine that actually performs the interpolation. Please note the x data values must be provided in ascending order.
program demo_lnt1 this program demonstrates the procedure for calling the subroutine interp1 which performs a linear interpolation of tabular data input by the user. subroutine interp1 performs linear interpolation of data of the form y = f(x). note: the data must be ordered as follows: x(1) < x(2) < x(3) < .... < x(n) please input the number of x and y data points (a minimum of two x and y data points must be input) ? 4 please input the x data for point # 1 ? 1 please input the y data for point # 1 ? 1 please input the x data for point # 2 ? 2 please input the y data for point # 2 ? 4 please input the x data for point # 3 ? 3 please input the y data for point # 3 ? 9 please input the x data for point # 4 ? 4 please input the y data for point # 4 ? 16 please input the x value to interpolate ? 3.3 program demo_lnt1 <linear interpolation of y = f(x)> interpolated value = 3.3 function value = 11.1
Here's the source code listing.
' program demo_lnt1.bas March 7, 2017 ' this program demonstrates the procedure for calling ' the subroutine interp1 which performs a linear ' interpolation of tabular data input by the user. ' subroutine interp1 performs linear interpolation of ' data of the form y = f(x). ' note: the data must be ordered as follows: ' x(1) < x(2) < x(3) < .... < x(n) ''''''''''''''''''''''''''''''''''' print " " print "program demo_lnt1" print " " print "this program demonstrates the procedure for calling" print "the subroutine interp1 which performs a linear" print "interpolation of tabular data input by the user." print " " print "subroutine interp1 performs linear interpolation of" print "data of the form y = f(x)." print " " print "note: the data must be ordered as follows:" print " " print "x(1) < x(2) < x(3) < .... < x(n)" do print " " print "please input the number of x and y data points" print "(a minimum of two x and y data points must be input)" input npts% loop until (npts% >= 2) ' dimension arrays dim x(npts%) as float, y(npts%) as float for i% = 1 to npts% print " " print "please input the x data for point #"; i% input x(i%) print " " print "please input the y data for point #"; i% input y(i%) next i% do print " " print "please input the x value to interpolate" input xval loop until (xval >= x(1) and xval <= x(npts%)) ' perform linear interpolation interp1(npts%, xval, fval) print " " print "program demo_lnt1" print " " print "<linear interpolation of y = f(x)>" print " " print "interpolated value = ", xval print " " print "function value = ", fval print " " end ''''''''''''''''''''''''' ''''''''''''''''''''''''' sub interp1(n%, xval, fval) ' linear interpolation subroutine ' y = f(x) ' input ' n = number of x and y data points (n >= 2) ' x() = vector of x data (n rows) ' y() = vector of y data (n rows) ' xval = x argument ' output ' fval = interpolated function value at xval ''''''''''''''''''''''''''''''''''''''''''''' local xinfac, y1, y2 ' compute index and interpolation factor for i% = 1 to n% if (xval <= x(i%)) then if (i% = 1) then xinfac = 0.0 index& = 1 else j% = i% - 1 xinfac = (xval - x(j%)) / (x(i%) - x(j%)) index% = j% end if exit for end if next i% y1 = y(index%) y2 = y(index% + 1) fval = y1 + xinfac * (y2 - y1) end sub
|