Problem 4 Find an approximation to (sqrt 3) correct to within 10−4 using the Bisection method (Hint: Consider f(x) = x 2 − 3.) (Use your computer code)
I have no idea how to write this code. he gave us this template but is not working. If you run the program it prints a table but it keeps running. for some reason the program doesnt stop.
tol = 1.e-10;
a = 1.0;
b = 2.0;
nmax = 100;
% Initialization
itcount = 0;
error = 1.0;
% Graph of the function
xval = linspace(a,b,100);
for i=1:100
fval(i) = func(xval(i));
end
plot(xval,fval);
grid on;
hold on;
% iteration begins here
while (itcount <= nmax && error >= tol)
itcount = itcount + 1;
% Generate and save iteratres
x = a + (b-a)/2;
z(itcount) = x;
fa = func(a);
fb = func(b);
fx = func(x);
error = abs(fx);
% error = abs(x - xold);
if (error < tol)
x_final = x;
else
if (fa*fx < 0)
% root is between a and x
b = x;
else
% root is between x and b
a = x;
end
end
plot(z(1:itcount),zeros(itcount,1),'r+');
pause(5)
end
if (itcount < nmax);
val = func(x);
fprintf(1,'Converged solution after %5d iterations',itcount);
fprintf(1,' is %15.7e, %e \n',x_final, val);
else fprintf(1,'Not converged after %5d iterations',nmax);
end
function val = func(x)
%val = x^3 + 4 * x^2 - 10;
val = x^3 - x - 3;
%val = sin(x);
end

댓글 수: 3

tala saffarini
tala saffarini 2021년 3월 24일
can u help me??
function[x]=bisect(m)
a=1;
b=3;
k=0;
while b-a>eps*b
x=(a+b)/2
if x^2>m
b=x
else
a=x
end
k=k+1
end
Uttsa
Uttsa 2024년 7월 3일
Whats the use of "eps" can you elaborate?

댓글을 달려면 로그인하십시오.

답변 (7개)

David Hill
David Hill 2019년 10월 4일

12 개 추천

function c = bisectionMethod(f,a,b,error)%f=@(x)x^2-3; a=1; b=2; (ensure change of sign between a and b) error=1e-4
c=(a+b)/2;
while abs(f(c))>error
if f(c)<0&&f(a)<0
a=c;
else
b=c;
end
c=(a+b)/2;
end
Not much to the bisection method, you just keep half-splitting until you get the root to the accuracy you desire. Enter function above after setting the function.
f=@(x)x^2-3;
root=bisectionMethod(f,1,2);

댓글 수: 1

Justin Vaughn
Justin Vaughn 2022년 10월 10일
Thank you for this because I was not sure of how to easily send a functino into my method's function. yours helped tremendously!

댓글을 달려면 로그인하십시오.

SHUBHAM GHADOJE
SHUBHAM GHADOJE 2021년 5월 29일
편집: Walter Roberson 2024년 7월 12일

1 개 추천

function c = bisectionMethod(f,j,k,error)
%f=@(x)x^2-3;
%j=1;
%k=2;
%(ensure change of sign between a and b)
%error=1e-4
c=(j+k)/2;
while abs(f(c))>error
if f(c)<0&&f(a)<0
j=c;
else
k=c;
end
c=(j+k)/2;
end
Prathamesh Purkar
Prathamesh Purkar 2021년 6월 6일
편집: Walter Roberson 2021년 12월 3일

1 개 추천

tol = 1.e-10;
a = 1.0;
b = 2.0;
nmax = 100;
% Initialization
itcount = 0;
error = 1.0;
% Graph of the function
xval = linspace(a,b,100);
for i=1:100
fval(i) = func(xval(i));
end
plot(xval,fval);
grid on;
hold on;
% iteration begins here
while (itcount <= nmax && error >= tol)
itcount = itcount + 1;
% Generate and save iteratres
x = a + (b-a)/2;
z(itcount) = x;
fa = func(a);
fb = func(b);
fx = func(x);
error = abs(fx);
% error = abs(x - xold);
if (error < tol)
x_final = x;
else
if (fa*fx < 0)
% root is between a and x
b = x;
else
% root is between x and b
a = x;
end
end
plot(z(1:itcount),zeros(itcount,1),'r+');
pause(5)
end
if (itcount < nmax);
val = func(x);
fprintf(1,'Converged solution after %5d iterations',itcount);
fprintf(1,' is %15.7e, %e \n',x_final, val);
else
fprintf(1,'Not converged after %5d iterations',nmax);
end
function val = func(x)
%val = x^3 -x + 1;
val = x^3 -x + 1;
%val = sin(x);
end
narendran
narendran 2022년 7월 2일

1 개 추천

5cosx + 4.5572 -cos30cosx-ssin30sinx

댓글 수: 3

Image Analyst
Image Analyst 2022년 7월 2일
@narendran it doesn't look like this is an answer to the original question. I think you posted this in the wrong place.
syms x
y = 5*cos(x) + 4.5572 - cos(30)*cos(x)-sin(30)*sin(x)
y = 
fplot(y, [-20 20]); yline(0)
vpasolve(y,x)
ans = 
Walter Roberson
Walter Roberson 2024년 7월 3일
Note by the way that cos(30) is cos of 30 radians. It seems unlikely that is what is desired.

댓글을 달려면 로그인하십시오.

Aman Pratap Singh
Aman Pratap Singh 2021년 12월 3일
편집: Walter Roberson 2021년 12월 3일

0 개 추천

f = @(x)('x^3-2x-5');
a = 2;
b = 3;
eps = 0.001;
m = (a+b)/2;
fprintf('\nThe value of, after bisection method, m is %f\n', m);
while abs(b-a)>eps
if (f(a)*f(m))<0
b=m;
else
a=m;
end
m = (a+b)/2;
end
fprintf('\nThe value of, after bisection method, m is %f\n', m);

댓글 수: 3

f = @(x)('x^3-2x-5');
That means that f will become a function handle that, given any input, will return the character vector ['x', '^', '3', '-', '2', 'x', '-', '5'] which is unlikely to be what you want to have happen.
f(0)
ans = 'x^3-2x-5'
f(1)
ans = 'x^3-2x-5'
f(rand(1,20))
ans = 'x^3-2x-5'
S. M. Rayhanul
S. M. Rayhanul 2026년 2월 3일 8:46
If the product of functional value is positive and also 'a' or 'b'is equal to zero, what is it?
Walter Roberson
Walter Roberson 2026년 2월 3일 19:36
So f(a)*f(b) > 0, and a == 0 or b == 0.
It is unclear what you mean by "what is it?"
Any function that has a zero crossing can be operated on by a linear operator that moves one of the zero crossings to a given x value. For example cos(x) normally has a zero crossing at , but cos(x+pi/2) has a zero crossing at x = 0. So there isn't anything particulary special about the fact that a == 0 or b == 0.

댓글을 달려면 로그인하십시오.

Prosun
Prosun 2024년 9월 24일

0 개 추천

% Clearing Screen
clc
% Setting x as symbolic variable
syms x;
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
% Finding Functional Value
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = (a+b)/2;
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
else
a =c;
end
c = (a+b)/2;
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end
My
My 2025년 12월 21일

0 개 추천

function p = mybisection(f, a, b, TOL)
while (b - a)/2 > TOL
p = (a + b)/2;
if f(a)*f(p) > 0
a = p;
else
b = p;
end
end
p = (a + b)/2;
end
%main bisection
clc; clear;
f = @(x) x^2 - 3;
a = 1;
b = 2;
TOL = 1e-6;
root = mybisection(f, a, b, TOL)

카테고리

태그

질문:

2019년 10월 4일

댓글:

2026년 2월 3일 19:36

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by