Curve Fitting 3
 Volume Number: 1 Issue Number: 13 Column Tag: Forth Forum

# Curve Fitting, Part III

By Jörg Langowski, Chemical Engineer, Grenoble, France, MacTutor Editorial Board

Last time I promised you to add some input/output to the curve fitter program so that it becomes a useful application. We'll do this here today.

A lot of basic graphic routines which you would have to write yourself on other machines are included in the Quickdraw ROM. This makes life very easy; we can use the screen like we would use a plotter (of course, we can do much better than that, but this is what we are dealing with today). What is not included in the ROM, of course, are utilities that plot coordinate axes, draw lines through pairs of x/y points, add symbols etc.

Most computer systems that are used for numerical calculation purposes contain a set of standard plotting routines, mostly callable from FORTRAN, that will help you generating a 'nice' graphical output with not too much effort. This column gives you some similar routines in FORTH; I have tried to stick closely to the Calcomp plotting routines, since they are something of a standard.

Our objective is to produce something like the curve in Fig. 1. The data points that the model is fitted to will be displayed using some symbols, while the fitted curve is plotted as a smooth line. X- and Y-axes are added to the plot.

The numbers that are to be plotted will be given in floating point format (in this case, single precision arrays as defined previously). So first of all we have to know how to scale the data to the integer values of the bit map that we are going to plot to.

The Calcomp convention is, for an array of N floating point numbers, to store the minimum of these numbers in cell N+1 of the array and the difference between the minimum and the maximum in cell N+2. These two numbers are then rounded to one significant figure. Given this information, one can write routines that automatically draw an axis that spans the range of the data values.

The rounding to one significant figure is done by the word next.int, which given the address of an extended number on the stack returns with the address of the rounded number (which is a local variable of next.int).

fscale finds the minimum and maximum values of an array and stores the scaling numbers above the last data point after rounding them to one significant digit.

xaxis and yaxis draw coordinate axes in x- and y-direction. Input parameters are (#ticks\length\array\npts), #ticks gives the number of ticks to be drawn (to the bottom of the x- and to the left of the y-axis), length is the total axis length in points, array the address of the scaled data array and npts the number of points in array.

Given two scaled arrays, line draws a line through the (x y) pairs defined by the two arrays. The symbol defined by the global symbol is plotted at each of the data points. Symbols could be e.g. '+' or '*'. Setting symbol to zero will draw no symbols (how could you have guessed). Depending on the setting of the global flag connecting, the symbols are either connected by a line or not.

For all the plotting routines cartesian has to be switched on and the origin to be defined within the output window by xyoffset. init.plot is used to do this job.

The main curve fitter program now consists of the following:

- the data arrays xdat and ydat are initialized with the simulated 'experimental data' (init). In this month's example, we use a sum of two exponentials as our model; this gives us five parameters to fit.

- a routine is called in which one can deliberately change the values of the parameters (for this we need floating point input, see below), so that one can see how the correct curve is fitted through the data starting from wrong parameters. One may also change the total number of parameters to be fitted; for instance, fitting three parameters only will force a single exponential fit to the data points. In this case, parameters 4 and 5 will have to be set to zero.

- the main loop calculates theoretical function values from xdat and the parameters and stores them in zdat. The data arrays are scaled and the plot displayed (like in Fig. 1); then one iteration of the fitting routine is taken, the parameters changed accordingly, displayed and the process repeated until parameter changes are below 1 part in 104.

## Floating point input

As I mentioned, the program would not be very useful without floating point input routines. In order to be independent of any particular Forth implementation, I am including a simple floating point input routine here which accepts a string and converts it to the decimal string format used by the SANE conversion routines.

Any such routine would mainly consist of taking successive characters from the input string, generating a decimal mantissa and keeping track of the number of digits behind the decimal point, then looking for an 'e' or 'E' to indicate an exponent and converting the exponent finally. In that aspect, the routine written here is very similar to the MacForth floating input routine. There is one difference, in that numbers without an exponent will be accepted and converted to floating point numbers, too. Therefore, you won't be able to use this routine as an extension of the Forth interpreter, as MacForth level 2 does. But in entering a lot of data, it can be very tedious always having to type an exponent.

fnumber takes a string as its input parameter and leaves on the stack:

```( addr of float\true ) if a valid floating point number was read from
the string,
( false ) if the conversion was not successful.
```

In the latter case, an error message is printed.

input.float reads a string from the keyboard into pad, then calls fnumber to convert it.

With the additions from this column, the curve fitter should finally be a utility that is useful to you (in case you have any curves to fit). Changing the function to be fitted is easy, and you might even install a make switch for vectored execution (V1#7) so that you can easily switch between different functions within one program.

Data input still has to be done manually, number by number. However, input.float may easily be extended to read input from a file. To transfer data to/from other applications through the scrap requires some more work; I'll deal with that problem soon.

## Feedback dept. Re: finding object code of unnamed tokens

The procedure to decompile the object code of an axed token is the following (V1#2):

- convert the token to an absolute address, using token>addr. If the word contained there is \$4e4f (TRAP \$F), the next words will be Forth code. Start decompiling at the following word. Of course, different versions of MacForth will give different addresses for the individual words due to different dictionary arrangements; but this procedure should work for any version of Level 1 or Level 2.

## Re: MacModula floating point

Shortly after my comment on errors in MacModula 2's 32-bit floating point arithmetic, I received a letter from the author of those routines, Daan Strebe:

" A few weeks ago I followed several bug reports to find two fundamental errors in the floating-point routines, one a conceptual error in the rounding and the other a misunderstanding about the 68000 processor instruction set. These two errors affected the multiply routine most, although the others were somewhat affected also. I revamped those routines and then ran a complex set of comprehensive tests, and the results allow me to state with a margin of confidence that the floating point in the latest rev is now not only slightly faster than it was, but also that the only errors in the basic routines (not necessarily including those in the math library) are from rounding. The rounding as it is now implemented yields 3 downward, 4 upward, and 1 non-round per 8 random floating-point operations. This should be satisfactory for most users; full IEEE rounding would considerably decrease the speed. I believe the compromise was successful. "

So everything should be fixed now. Let's hope that the new update will be distributed soon (it probably is when this goes to the printer's).

```Listing 1: Plotting routines and floating point input for the curve fitter
program

( Additions to the curve fitting program)
( for graphical output and floating point input.)
( © 1985 J. Langowski for MacTutor)
( Again, only the parts that have been changed)
( or added with respect to the last two columns )
( are printed here)

: s> 1008 fp68k @sr 10 and ;

: numstring create 24 allot ;
numstring zzs1 ( internal conversion string )

: dec. ( float\format# -- )
zzformat ! zzformat swap zzs1 b2d
zzs1 dup w@ 255 > if ." -" else ."  " then
dup 4+ count over 1 type ." ."
swap 1+ swap 1- type ( mantissa )
2+ w@ ( get exponent )
1 w* zzformat @ + 1-
." E" 0 .r ;

create xdat 400 allot   create ydat 400 allot
create zdat 400 allot   create residues 400 allot
100 10 matrix derivmat    10 11 matrix resmat
5 constant npars        80 constant npts
create par 5 10 * allot
create delta 5 4*  allot

float logten  ten logten x2x  logten lnx
: log10x dup lnx logten swap f/ ;

( define function )                               ( 090485 jl )
float temp ( local to func)

func ( x -- f[x] = par[1] + par[2] * exp[par[3]*x]
+ par[4] * exp[par[5]*x] )
dup temp x2x
par 40 + temp f* temp expx  par 30 + temp f*
par 20 + over f* dup expx  par 10 + over f*
par over f+  temp over f+ ;
axe temp
: >fa1  fa1 s2x ;
: init_pars   one par x2x  two par 10+ x2x
-one par 20 + x2x  two par 20 + f/
one par 30 + x2x
-one par 40 + x2x ten par 40 + f/ ;
init_pars

: one_iter
make_derivmat  residuals
make_resmat     delta 0 0 resmat npars gauss ;

: new_pars 16 ( true if no significant changes)
npars 0 do par i 10 * +
delta i 4* +  over  s+
delta i 4* + fa1 s2x  fa1 f/
fa1 fabs  eps fa1 f> and loop ;

80 ' npts !    5 ' npars !

: init ( initialize data arrays)
npts 0 do i sp@ fa1 in2x 4*
xdat over + fa1 swap x2s
ydat over + fa1 func  ranf fa2 x2x
ten fa2 f/  fa2 over f+  swap x2s
drop  loop ;

( plotting routines)                        ( 092385 jl )
float 1/2  1 sp@ 1/2 in2x drop  2 sp@ 1/2 in/ drop
float small  ten small x2x  -200 sp@ fa1 in2x drop
fa1 small x^y
( anything smaller than small will be zero)
float sc.aux  float sc.exp
: next.int ( float -- rounded to 1 dec. place )
dup small f>
if dup sc.aux x2x  sc.aux  dup fabs  dup log10x
dup 1/2 swap f- frti
ten sc.exp x2x  sc.aux sc.exp x^y   sc.aux x2x
sc.exp sc.aux f/  sc.aux frti  sc.exp sc.aux f*
sc.aux
else drop zero then  ;

float xlow  float xhi  float sc.factor
: fscale
( array \ n -- | start, scale -> array[n+1..n+2] )
over xlow s2x  over xhi s2x
over over 1 do
dup i 4* + dup xlow s> not if dup xlow s2x then
dup xhi  s> if xhi s2x  else drop then  loop
xlow xhi f-
over 4*  +  xlow next.int swap x2s
1+ 4* +  xhi  next.int swap x2s  ;

: .fscale ( array \ n -- )
4* + dup fa1 s2x ." min = " fa1 7 dec.
4+ fa1 s2x ." , scale = " fa1 7 dec. cr ;

( xtick, ytick )                                  ( 092385 jl )

: xtick ( rx x -- ) dup 0 move.to
dup -5 draw.to  dup 15 - -16 move.to
get.textsize >r 9 textsize swap 2 dec. r> textsize
0 move.to ;

: ytick ( ry y -- ) -45 over 4- move.to
get.textsize >r 9 textsize swap 2 dec. r> textsize
0 over move.to    -5 over draw.to
0 over 6- move.to  0 swap draw.to ;

( xaxis)                                           ( 092385 jl )
float start  float del
: xaxis
( #ticks\length\array\npts -- | starts at origin )
0 0 move.to
4* + dup  start s2x  4+ delta s2x
over /  ( #ticks\length/tick -- )
over 0 do dup i 1+ * dup 0 draw.to
delta fa1 x2x i 1+ sp@ fa1 in* drop
3 pick sp@ fa1 in/ drop
start fa1 f+    fa1 swap xtick loop
drop drop ;

( yaxis)                                          ( 092385 jl )
: yaxis
( #ticks\length\array\npts -- | starts at origin )
0 0 move.to
4* + dup  start s2x  4+ delta s2x
over /  ( #ticks\length/tick -- )
over 0 do dup i 1+ * 0 over draw.to
delta fa1 x2x i 1+ sp@ fa1 in* drop
3 pick sp@ fa1 in/ drop
start fa1 f+    fa1 swap ytick loop
drop drop ;

( line, variables )                               ( 092585 jl )
variable xline.length variable yline.length
variable xpos  variable ypos  variable symbol
variable connecting  connecting off
float xstart float ystart float xdel float ydel

( line)                                           ( 092585 jl )
: line ( xarray\yarray\npts\xlength\ylength -- )
yline.length !  xline.length !   0 0 move.to
over over 4* + dup ystart s2x 4+ ydel s2x
3 pick over 4* + dup xstart s2x 4+ xdel s2x
0 do over i 4* + fa1 s2x  xstart fa1 f-  xdel fa1 f/
dup  i 4* + fa2 s2x  ystart fa2 f-  ydel fa2 f/
xline.length fa1 in*  fa1 xpos x2in
yline.length fa2 in*  fa2 ypos x2in
xpos @  ypos @  over over
connecting @ if draw.to else move.to then
-4 -4 rmove symbol @ emit move.to
loop   drop drop ;

( calc.theor, disp.theor, disp.data, scale.all)
( 092585 jl )
: calc.theor
npts 0 do xdat i 4* + fa1 s2x fa1 func
zdat i 4* + x2s  loop ;
: disp.theor
0 symbol !  connecting on
xdat zdat npts 400 250 line ;
: disp.data
43 symbol !  connecting off
xdat ydat npts 400 250 line ;
: scale.all
xdat npts fscale  ydat npts fscale
ydat npts 4* + @  zdat npts 4* + !
ydat npts 1+ 4* + @  zdat npts 1+ 4* + !  ;

( disp.axes)                                      ( 092585 jl )
: disp.axes
5 400 xdat npts xaxis  5 250 ydat npts yaxis ;
: init.plot
page 50 255 xyoffset cartesian on ;

( floating point input)                           ( 092385 jl )
: fsign zzs1 ;  : fexpo zzs1 2+ ;
: fmant zzs1 4+ ;  variable frac   float float.out
variable decimals
: input.sign 0 fsign !
dup c@ case 45 of -1 fsign w! 1+ endof
43 of             1+ endof endcase ;
: input.mantissa   0 decimals !   0 frac !
fmant 21 +  fmant 1+ do  i fmant 1+ -  fmant c!
dup c@ dup
case 48 57 range.of ic! frac @ decimals +!
1+ 1 endof
46 of   drop 1 frac !    1+ 0 endof
20 swap endcase  +loop drop ;
: input.exponent
dup c@ dup bl = not
if dup 69 =  swap 101 =  or not
if  drop 0
else dup 1+ c@ 43 = 1 and +
number decimals @ - fexpo w! -1
then
else  decimals @ -1 * fexpo w! -1  2drop
then  ;

: fnumber zzs1 24 blanks
if fsign float.out d2b float.out -1
else ." floating input error!" cr 0 then ;

: get.pars
npars 0 do
begin cr ." par[" i 1+ . ." ] = " input.float until
par i 10 * + x2x loop
cr begin cr ." total # of parameters to be fitted "
5 input.number until
' npars !  cr ;

: .pars npars 0 do
." par[" i 1+ . ." ] = " i 10 * par + 7 dec. cr loop ;

( main curve fitter program)                 ( 092685 jl )
: fit.curve  page
init_pars ." initializing data arrays..." cr init
5 ' npars ! init.plot get.pars .pars
." fitting " npars . ." parameters to "
npts . ." data points "
calc.theor scale.all page
disp.data disp.axes disp.theor
begin  one_iter new_pars not while
page .pars
calc.theor page disp.data disp.axes disp.theor
repeat
300 300 move.to ;
```

Community Search:
MacTech Search:

Microsoft Remote Desktop 8.0.16 - Connec...
With Microsoft Remote Desktop, you can connect to a remote PC and your work resources from almost anywhere. Experience the power of Windows with RemoteFX in a Remote Desktop client designed to help... Read more
Spotify 1.0.4.90. - Stream music, create...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more
djay Pro 1.1 - Transform your Mac into a...
djay Pro provides a complete toolkit for performing DJs. Its unique modern interface is built around a sophisticated integration with iTunes and Spotify, giving you instant access to millions of... Read more
Vivaldi 1.0.118.19 - Lightweight browser...
Vivaldi browser. In 1994, two programmers started working on a web browser. Our idea was to make a really fast browser, capable of running on limited hardware, keeping in mind that users are... Read more
Stacks 2.6.11 - New way to create pages...
Stacks is a new way to create pages in RapidWeaver. It's a plugin designed to combine drag-and-drop simplicity with the power of fluid layout. Features: Fluid Layout: Stacks lets you build pages... Read more
xScope 4.1.3 - Onscreen graphic measurem...
xScope is powerful set of tools that are ideal for measuring, inspecting, and testing on-screen graphics and layouts. Its tools float above your desktop windows and can be accessed via a toolbar,... Read more
Cyberduck 4.7 - FTP and SFTP browser. (F...
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
Labels & Addresses 1.7 - Powerful la...
Labels & Addresses is a home and office tool for printing all sorts of labels, envelopes, inventory labels, and price tags. Merge-printing capability makes the program a great tool for holiday... Read more
teleport 1.2.1 - Use one mouse/keyboard...
teleport is a simple utility to let you use one single mouse and keyboard to control several of your Macs. Simply reach the edge of your screen, and your mouse teleports to your other Mac! The... Read more
Apple iMovie 10.0.8 - Edit personal vide...
With an all-new design, Apple iMovie lets you enjoy your videos like never before. Browse your clips more easily, instantly share your favorite moments, and create beautiful HD movies and Hollywood-... Read more

## Latest Forum Discussions

Use Batting Average and the Apple Watch...
Batting Average, by Pixolini, is designed to help you manage your statistics. Every time you go to bat, you can use your Apple Watch to track  your swings, strikes, and hits. [Read more] | Read more »
Celebrate Studio Pango's 3rd Annive...
It is time to party, Pangoland pals! Studio Pango is celebrating their 3rd birthday and their gift to you is a new update to Pangoland. [Read more] | Read more »
Become the World's Most Important D...
Must Deliver, by cherrypick games, is a top-down endless-runner witha healthy dose of the living dead. [Read more] | Read more »
SoundHound + LiveLyrics is Making its De...
SoundHound Inc. has announced that SoundHound + LiveLyrics, will be one of the first third-party apps to hit the Apple Watch. With  SoundHound you'll be able to tap on your watch and have the app recognize the music you are listening to, then have... | Read more »
Adobe Joins the Apple Watch Lineup With...
A whole tidal wave of apps are headed for the Apple Watch, and Adobe has joined in with 3 new ways to enhance your creativity and collaborate with others. The watch apps pair with iPad/iPhone apps to give you total control over your Adobe projects... | Read more »
Z Steel Soldiers, Sequel to Kavcom'...
Kavcom has released Z Steel Soldiers, which continues the story of the comedic RTS originally created by the Bitmap Brothers. [Read more] | Read more »
Seene Lets You Create 3D Images With You...
Seene, by Obvious Engineering, is a 3D capture app that's meant to allow you to create visually stunning 3D images with a tap of your finger, and then share them as a 3D photo, video or gif. [Read more] | Read more »
Lost Within - Tips, Tricks, and Strategi...
Have you just downloaded Lost Within and are you in need of a guiding hand? While it’s not the toughest of games out there you might still want some helpful tips to get you started. [Read more] | Read more »
The Petcube Camera is a device that lets you use live video to check in on your pet, talk to them, and play with them using a laser pointer - all while you're away. And the Petcube app is coming to the Apple Watch, so you'll be able to hang out with... | Read more »
Now You Can Manage Your Line2 Calls With...
You'll be able to get your Line2 cloud phone service on the Apple Watch very soon. The watch app can send and receive messages using hands-free voice dictation, or by selecting from a list of provided responses. [Read more] | Read more »

## Price Scanner via MacPrices.net

Intel Compute Stick: A New Mini-Computing For...
The Intel Compute Stick, a new pocket-sized computer based on a quad-core Intel Atom processor running Windows 8.1 with Bing, is available now through Intel Authorized Dealers across much of the... Read more
Heal to Launch First One-Touch House Call Doc...
Santa Monica, California based Heal, a pioneer in on-demand personal health care services — will offer the first one-touch, on-demand house call doctor app for the Apple Watch. Heal’s Watch app,... Read more
Mac Notebooks: Avoiding MagSafe Power Adapter...
Apple Support says proper usage, care, and maintenance of Your Mac notebook’s MagSafe power adapter can substantially increase the the adapter’s service life. Of course, MagSafe itself is an Apple... Read more
12″ Retina MacBook In Shootout With Air And P...
BareFeats’ rob-ART morgan has posted another comparison of the 12″ MacBook with other Mac laptops, noting that the general goodness of all Mac laptops can make which one to purchase a tough decision... Read more
FileMaker Go for iPad and iPhone: Over 1.5 Mi...
FileMaker has announced that its FileMaker Go for iPad and iPhone app has surpassed 1.5 million downloads from the iTunes App Store. The milestone confirms the continued popularity of the FileMaker... Read more
Sale! 13-inch 2.7GHz Retina MacBook Pro for \$...
Best Buy has the new 2015 13″ 2.7GHz/128GB Retina MacBook Pro on sale for \$1099 – \$200 off MSRP. Choose free shipping or free local store pickup (if available). Price for online orders only, in-... Read more
Minimalist MacBook Confirms Death of Steve Jo...
ReadWrite’s Adriana Lee has posted a eulogy for the “Digital Hub” concept Steve Jobs first proposed back in 2001, declaring the new 12-inch MacBook with its single, over-subscribed USB-C port to be... Read more
13-inch 2.7GHz Retina MacBook Pro for \$1234 w...
Adorama has the 13″ 2.7GHz/128GB Retina MacBook Pro in stock for \$1234.99 (\$65 off MSRP) including free shipping plus a free LG external DVD/CD optical drive. Adorama charges sales tax in NY & NJ... Read more
13-inch 2.5GHz MacBook Pro available for \$999...
Adorama has the 13-inch 2.5GHz MacBook Pro on sale for \$999 including free shipping plus NY & NJ sales tax only. Their price is \$100 off MSRP. Read more
Save up to \$600 with Apple refurbished Mac Pr...
The Apple Store is offering Apple Certified Refurbished Mac Pros for up to \$600 off the cost of new models. An Apple one-year warranty is included with each Mac Pro, and shipping is free. The... Read more

## Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Support Technician IV - Jack Henry a...
Job Description Jack Henry & Associates is seeking an Apple Support Technician. This position while acting independently, ensures the proper day-to-day control of Read more
*Apple* Client Systems Solution Specialist -...
…drive revenue and profit in assigned sales segment and/or region specific to the Apple brand and product sets. This person will work directly with CDW Account Managers Read more
*Apple* Software Support - Casper (Can work...
…experience . Full knowledge of Mac OS X and prior . Mac OSX / Server . Apple Remote Desktop . Process Documentation . Ability to prioritize multiple tasks in a fast pace Read more
*Apple* Software Support - Xerox Corporation...
…Imaging experience Full knowledge of Mac OS X and prior Mac OSX / Server Apple Remote Desktop Process Documentation Ability to prioritize multiple tasks in a fast pace Read more