I have made an program,
function bisection;
x1=input('enter the first value=')
x2=input('enter the second value=')
%f3=[];
for x=1:20
%x=1;
x3=(x1+x2)/2;
while x3-x1 >= 0.001
f3(x)=x3^3 + x3^2 - 3*x3 - 3;
f1(x)=x1^3 + x1^2 - 3*x1 - 3;
if ((f3(x)*f1(x)) < 0)
x2=x3;
else
x1=x3;
end
break
end
format long
f3'
disp('The root is found to be =');
x3
end
. . . . . program calculate function of interval (x1, x2) given by user,
my program compile and execute but little stupid repetition until for loop complete, i want to stop further printing loop when desired value is achieved with while condition is used.
The Bisection method is implemented in the following method. The function to be analyzed, as well as the interval borders are passed as parameters.
function bisection(f, x1, x2)
if f(x1)*f(x2) < 0 % check precondition sign(f(x1)*f(x2)) = -1
while abs(x2-x1) >= 0.001
x3=(x1+x2)/2;
if ((f(x1)*f(x3)) < 0)
x2 = x3;
elseif ((f(x2)*f(x3)) < 0)
x1 = x3;
else
break
end
end
fprintf('The root is found to be = %.3f\n', x3);
else
fprintf('f(x1) and f(x2) must have opposite signs!\n')
end
Runner part of your root finding task.
format long
f = @(x) x^3 + x^2 - 3*x - 3;
x1=input('enter the first value=');
x2=input('enter the second value=');
bisection(f, x1, x2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.