Last active
December 9, 2015 18:58
-
-
Save martunta/4313559 to your computer and use it in GitHub Desktop.
Mat Metodes: http://en.wikipedia.org/wiki/Bisection_method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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