( function(x)
{
y <- x^3 + 2*x^2 + 10*x - 20
}
(function(f, x0, x1, approx)
{
i <- 1
er(1) <- 100
if (f(x0)*f(x1) < 0){
a(1) <- x0
b(1) <- x1
c(1) <- (a(1)+b(1))/2
print (r\t\t a\t\t b\t\t c\t\t f(c)\t Error \n)
print (%2d \t %11.6f \t %11.6f \t %11.6f \t %11.6f \n,i,a(i),b(i),c(i),f(c(i)))
while (abs(er(i)) >= approx){
if (f(a(i))*f(c(i))< 0){
a(i+1) <- a(i)
b(i+1) <- c(i)}
if (f(a(i))*f(c(i))> 0){
a(i+1) <- c(i)
b(i+1) <- b(i)}
c(i+1) <- (a(i+1)+b(i+1))/2
er(i+1) <- abs((c(i+1)-c(i))/(c(i+1)))
print (%2d \t %11.6f \t %11.6f \t %11.6f \t %11.6f \t %7.6f \n,i+1,a(i+1),b(i+1),c(i+1),f(c(i+1)),er(i+1))
i=i+1 }}
else
print (" ")
}
Tidak ada komentar:
Posting Komentar