Curve that touches three points: Difference between revisions

m (title)
Line 58:
}</lang>
 
 
=={{header|J}}==
<pre>
NB. coordinates:(x,y) starting point (10,10) medium point (100,200) final point (200,10)
 
X=: 10 100 200
Y=: 10 200 10
 
NB. matrix division computes polynomial coefficients
NB. %. implements singular value decomposition
NB. in other words, we can also get best fit polynomials of lower order.
 
polynomial=: (Y %. (^/ ([: i. #)) X)&p.
 
 
assert 10 200 10 -: polynomial X NB. test
 
 
 
Filter=: (#~`)(`:6)
 
Round=: adverb def '<.@:(1r2&+)&.:(%&m)'
assert 100 120 -: 100 8 Round 123 NB. test, round 123 to nearest multiple of 100 and of 8
 
 
 
NB. libraries not permitted, character cell graphics are used.
 
 
GRAPH=: 50 50 $ ' ' NB. is an array of spaces
 
NB. place the axes
GRAPH=: '-' [`(([:<0; i.@:#)@:])`]} GRAPH
GRAPH=: '|' [`(([:<0;~i.@:#)@:])`]} GRAPH
GRAPH=: '+' [`((<0;0)"_)`]} GRAPH NB. origin
 
 
NB. clip the domain.
EXES=: ((<:&(>./X) *. (<./X)&<:))Filter 5 * i. 200
WHYS=: polynomial EXES
 
 
NB. draw the curve
1j1 #"1 |. 'X' [`((<"1 WHYS ;&>&:([: 1 Round %&5) EXES)"_)`]} GRAPH
 
 
NB. were we to use a library:
load'plot'
'title 3 point fit' plot (j. polynomial) i.201
</pre>
 
=={{header|Julia}}==
Anonymous user