Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:47 09 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM, + and X - linear interpolation

Author Message
cdeagle
Senior Member

Joined: 22/06/2014
Location: United States
Posts: 266
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


 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2959
Posted: 08:51am 07 Mar 2017
Copy link to clipboard 
Print this post

Hi David,

Thanks for sharing all your code recently - I can see your having fun with your MMX144 Module
Hopefully others will find something useful from your posts over the last few days.

Do let us know if you come across any 'strange' behaviour in your usage of double-precision calculations.

WW
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025