TweetFollow Us on Twitter

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, 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

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 

( 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/ ;

: 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
: ( 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*
  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 swap x2s
     1+ 4* +  xhi 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
  dup -5  dup 15 - -16
  get.textsize >r 9 textsize swap 2 dec. r> textsize
  0 ;

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

( xaxis)                                           ( 092385 jl )
float start  float del
: xaxis 
   ( #ticks\length\array\npts -- | starts at origin )
  0 0
  4* + dup  start s2x  4+ delta s2x
  over /  ( #ticks\length/tick -- )
  over 0 do dup i 1+ * dup 0
         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
  4* + dup  start s2x  4+ delta s2x
  over /  ( #ticks\length/tick -- )
  over 0 do dup i 1+ * 0 over
            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
    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 else then
       -4 -4 rmove symbol @ emit
    loop   drop drop ;

( calc.theor, disp.theor,, 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 ;
    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
      else  decimals @ -1 * fexpo w! -1  2drop
      then  ;

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

: input.float pad 22 blanks pad 22 expect pad fnumber ;

  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 .pars
  ." fitting " npars . ." parameters to "
               npts . ." data points "
  calc.theor scale.all page disp.axes disp.theor
  begin  one_iter new_pars not while
    page .pars
    calc.theor page disp.axes disp.theor
  300 300 ;


Community Search:
MacTech Search:

Software Updates via MacUpdate

SpamSieve 2.9.37 - Robust spam filter fo...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
Viber 11.3.1 - Send messages and make fr...
Viber lets you send free messages and make free calls to other Viber users, on any device and network, in any country! Viber syncs your contacts, messages and call history with your mobile device, so... Read more
Monosnap 3.6.1 - Versatile screenshot ut...
Monosnap lets you capture screenshots, share files, and record video and .gifs. Features Capture Capture full screen, just part of the screen, or a selected window Make your crop area pixel... Read more
WhatRoute 2.2.6 - Geographically trace o...
WhatRoute is designed to find the names of all the routers an IP packet passes through on its way from your Mac to a destination host. It also measures the round-trip time from your Mac to the router... Read more
MacFamilyTree 9.0.5 - Create and explore...
MacFamilyTree gives genealogy a facelift: modern, interactive, convenient and fast. Explore your family tree and your family history in a way generations of chroniclers before you would have loved.... Read more
WhatsApp 0.3.4375 - Desktop client for W...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Mactracker 7.8.1 - Database of all Mac m...
Mactracker provides detailed information on every Mac computer ever made, including items such as processor speed, memory, optical drives, graphic cards, supported OS X versions, and expansion... Read more
Boom 3D 1.3.1 - 3D surround sound and ph...
Boom 3D is a revolutionary app with 3D Surround Sound and phenomenally rich and intense audio that is realistic and works on any headphones. Features 3D surround sound Built-in audio player... Read more
OmniGraffle 7.11.2 - Create diagrams, fl...
OmniGraffle helps you draw beautiful diagrams, family trees, flow charts, org charts, layouts, and (mathematically speaking) any other directed or non-directed graphs. We've had people use Graffle to... Read more
OmniGraffle Pro 7.11.2 - Create diagrams...
OmniGraffle Pro helps you draw beautiful diagrams, family trees, flow charts, org charts, layouts, and (mathematically speaking) any other directed or non-directed graphs. We've had people use... Read more

Latest Forum Discussions

See All

Steam Link Spotlight - Dicey Dungeons
Steam Link Spotlight is a new feature where we take a look at PC games that play exceptionally well with the Steam Link app. In case you missed it, our last entry focused on Faeria, a collectible card game that used to be available on the App Store... | Read more »
I can't believe Sky came out after...
I play games almost exclusively on mobile, and I’ve been doing so since around the time I started writing for 148Apps. This is why I’m late to the party on Journey. It wasn’t until last week that the game was playable on mobile, and it wasn’t until... | Read more »
Gigantic X guide - What you need to know...
Gigantic X continues to inspire loot lust over here at 148Apps, particularly because the game has already been updated just in its second week of release. Unfortunately, this 1.1.0 patch doesn’t bring a whole ton of new goodies with it, but it does... | Read more »
Steam Link Spotlight - Faeria
If you’ve been following for a while, chances are you’ve seen me talk about Faeria. I reviewed it when it initially came out on iOS, and again when The Adventure Pouch: Oversky came out. I also put the game on my best games of 2017 list... | Read more »
Gigantic X guide - Tips and tricks for b...
Gigantic X has only been out for a little over a week, but it’s shaping up to be the mobile loot shooter of our dreams. That said, it’s not exactly the most friendly game out there. We noted in our review that you need to invest some time in the... | Read more »
Do Not Feed The Monkeys is one of the mo...
I’ve done a lot of messed up stuff in video games. I’ve beat people to death, slaghtered innocent animals, and even committed genocide. In doing all of that though, I’m not sure I’ve felt as uncomfortable as I have while playing Do Not Feed The... | Read more »
Civilization VI - What you need to know...
Last week, the mobile version of Civilization VI got updated to include the huge Rise and Fall expansion. Where previous updates to the game provided one or two new civilizations and maybe a few scenarios, Rise and Fall makes sweeping changes to... | Read more »
Combo Quest (Games)
Combo Quest 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Combo Quest is an epic, time tap role-playing adventure. In this unique masterpiece, you are a knight on a heroic quest to retrieve... | Read more »
Hero Emblems (Games)
Hero Emblems 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ** 25% OFF for a limited time to celebrate the release ** ** Note for iPhone 6 user: If it doesn't run fullscreen on your device... | Read more »
Puzzle Blitz (Games)
Puzzle Blitz 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Puzzle Blitz is a frantic puzzle solving race against the clock! Solve as many puzzles as you can, before time runs out! You have... | Read more »

Price Scanner via

Price drop! B&H now has new 4-Core Mac mi...
B&H Photo has dropped prices on 2018 4-Core Mac minis to $100 off MSRP, only $699. They’re also offering a $100 discount on the 6-Core Mac mini. Shipping is free: – 3.6GHz Quad-Core mini: $699 $... Read more
Amazon is offering a 37% discount on Apple Sm...
Amazon has Apple Smart Keyboards for current-generation 10″ iPad Airs and previous-generation 10″ iPad Pros on sale today for $99.99 shipped. That’s a 37% discount over Apple’s regular MSRP of $159... Read more
12″ iPad Pros on sale today for up to $400 of...
Amazon has new 2018 Apple 12″ iPad Pros in stock today and on sale for up to $400 off Apple’s MSRP. These are the same iPad Pros sold by Apple in its retail and online stores. Be sure to select... Read more
Apple has 2019 iMacs, Certified Refurbished,...
Apple has Certified Refurbished 2019 21″ & 27″ iMacs now available starting at $929 and up to $350 off the cost of new models. Apple’s one-year warranty is standard, shipping is free, and each... Read more
Sale! New 11″ WiFi iPad Pros for up to $400 o...
Walmart has new 2018 Apple 11″ WiFi iPad Pros in stock today and on sale for up to $400 off Apple’s MSRP. These are the same iPad Pros sold by Apple in its retail and online stores. Choose free local... Read more
New 2019 15″ 2.3GHz 8-Core MacBook Pro on sal...
Apple resellers B&H Photo and Amazon are offering the new 2019 15″ 2.3GHz Silver 8-Core MacBook Pro for $2449. That’s $350 off Apple’s MSRP and the lowest price available for an 8-Core MacBook... Read more
B&H has new 4-Core and 6-Core Mac minis o...
B&H Photo has the new 2018 4-Core and 6-Core Mac minis on sale for $80-$100 off Apple’s standard MSRP. Shipping is free: – 3.6GHz Quad-Core mini: $719 $80 off MSRP – 3.0GHz 6-Core mini: $999 $... Read more
10″ iPad Airs on sale for up to $50 off Apple...
B&H Photo has new 10.5″ iPad Airs on sale today for $30-$50 off Apple’s standard MSRP including free overnight shipping to many address in the US: – 10.5″ 64GB WiFi iPad Air: $469 $30 off MSRP –... Read more
Apple has clearance 2018 13″ 2.3GHz Quad-Core...
Apple has Certified Refurbished 2018 13″ 2.3GHz 4-Core Touch Bar MacBook Pros available starting at $1489. Apple’s one-year warranty is included, shipping is free, and each MacBook has a new outer... Read more
Clearance 2018 13″ MacBook Airs drop to an al...
B&H Photo has clearance 2018 13″ MacBook Airs available for $300 off Apple’s original MSRP with prices starting at only $899. Overnight shipping, or expedited shipping, is free depending on your... Read more

Jobs Board

*Apple* Mobility Pro - Best Buy (United Stat...
**719892BR** **Job Title:** Apple Mobility Pro **Job Category:** Store Associates **Location Number:** 001096-Grove City-Store **Job Description:** At Best Buy, our Read more
Best Buy *Apple* Computing Master - Best Bu...
**719975BR** **Job Title:** Best Buy Apple Computing Master **Job Category:** Store Associates **Location Number:** 001198-East Orange-Store **Job Description:** The Read more
*Apple* Mobile Master - Best Buy (United Sta...
**721421BR** **Job Title:** Apple Mobile Master **Job Category:** Store Associates **Location Number:** 000878-Dubuque-Store **Job Description:** **What does a Best Read more
Best Buy *Apple* Computing Master - Best Bu...
**716411BR** **Job Title:** Best Buy Apple Computing Master **Job Category:** Sales **Location Number:** 001089-Watertown-Store **Job Description:** **What does a Read more
*Apple* Mobility Pro - Best Buy (United Stat...
**721359BR** **Job Title:** Apple Mobility Pro **Job Category:** Store Associates **Location Number:** 000952-Baytown-Store **Job Description:** At Best Buy, our Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.