Skip to content

Instantly share code, notes, and snippets.

@c650
Last active March 27, 2017 14:31
Show Gist options
  • Select an option

  • Save c650/6003eaceb328dfcaaf492eb55d721feb to your computer and use it in GitHub Desktop.

Select an option

Save c650/6003eaceb328dfcaaf492eb55d721feb to your computer and use it in GitHub Desktop.
def f(x,y)
(((-0.0447 * x ** 6 + 0.2731 * x ** 5 - 0.7935 * x ** 4 + 1.7751 * x ** 3 - 2.561 * x ** 2 + 0.9885 * x + 1.0821) ** 2) - (y ** 2)) ** 0.5
end
startx = 0
endx = 2
starty = -1.2
endy = 1.2
PIECES = 1000000.0
xstep = (endx - startx) / PIECES
ystep = (endy - starty) / PIECES
area = xstep * ystep
sum = 0
xx = startx
yy = starty
while xx < endx
while yy < endy
tmp = f(xx,yy)
sum += tmp unless tmp.is_a? Complex
yy += ystep
end
xx += xstep
end
puts area * sum * PIECES
@kasanwa-solane

Copy link
Copy Markdown

Noticed the problem. The higher PIECES is, the smaller the resulting value. The way to fix it is to change line 32 to be puts area * sum * PIECES.

@c650

c650 commented Mar 26, 2017

Copy link
Copy Markdown
Author

I see. i'll correct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment