Skip to content

Instantly share code, notes, and snippets.

@martunta
Last active December 9, 2015 18:59
Show Gist options
  • Save martunta/4313717 to your computer and use it in GitHub Desktop.
Save martunta/4313717 to your computer and use it in GitHub Desktop.
#pārbaudāmās funkcijas definīcija
def f(x)
#x**2-612 #atkomentē lai izmēģinātu šo piemēru
Math.cos(x)-x**3 #aizkomentē, lai izmēģinātu pirmo piemēru
end
#ar roku izrēķinam atvasinājumu no f(x)
def df(x)
#2*x #atkomentē lai izmēģinātu šo piemēru
-Math.sin(x) - 3*(x**2)
end
def metode (x)
@i+=1
x1 = x - (f(x)/df(x))
puts "#{@i}: ja x=#{x}, tad x1=#{x1}"
x = metode(x1) unless @i>20 || (x1-x).abs<E
x
end
#definējam sākotnējo punktu X
x = 0.5 #nomainam uz 10.0 pirmajam piemēram
E = 0.01
@i = 0 #izeja no rekursijas, ja nu gadijumaa kaut kas iecikleejas
#sākam metodi
x1 = metode(x)
#atbilde ir
puts "1 atbilde ir #{x1}"
E = 0.0001
x2 = metode(x)
puts "2 atbilde 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