Skip to content

Instantly share code, notes, and snippets.

@martunta
Last active December 9, 2015 18:58
Show Gist options
  • Save martunta/4313559 to your computer and use it in GitHub Desktop.
Save martunta/4313559 to your computer and use it in GitHub Desktop.
#pārbaudāmās funkcijas definīcija
def f (x)
x**3-x-2
end
#metodes definīcija
def bisect (a,b)
(a+b)/2
end
def metode (a,b)
@i+=1
print "#{@i}: ja a=#{a} un b=#{b}, tad "
x = bisect(a,b)
puts "x=#{x} un f(x)=#{f(x)}"
if f(a)*f(x) <= 0
b = x
else
a = x
end
x = metode(a,b) unless @i>20 || (b-a).abs<E
x
end
#definējam izejas datus a un b
a = 1.0 #force float, by adding .0
b = 2.0
E = 0.01
@i = 0 #izeja no rekursijas, ja nu gadijumaa kaut kas iecikleejas
#pārbaudam vai atšķiras zīmes
if (f(a) * f(b)) > 0
puts "kljuda: f(a) un f(b) jaabuut ar atskiriigaam ziimeem"
end
#sākam metodi
x1 = metode(a,b)
#atbilde ir
puts "atbilde 1 ir #{x1}"
E = 0.0001
x2 = metode(a,b)
#atbilde ir
puts "atbilde 2 ir #{x2}"
xd = (x2-x1).abs
puts "atskiriba ir #{xd}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment