x = linspace(-2,1);
y = 0.5-sin(x) -x.^2
plot(x,y);
hold on
plot([-2 1], [0 0]);
hold off
grid on
xp = 0;
xn = 0.5;
x0 = [-1.5 -1]
xp = -0.5, xn = 0.8;
xmp = ((xn + xp)/2);
ymp = 0.5-sin(xmp)-xmp^2
if ymp > 0
xp = xmp
else
xn = xmp
end
xmp = ((xn + xp)/2)
ymp = 0.5-sin(xmp)-xmp^2
if ymp > 0
xp = xmp
else
xn = xmp
end
xmp - ((xn + xp)/2)
% create brackets
xp = -0.5;
xn = 0.8;
% calculate the midpoint
xmp = (xn+xp)/2;
% calculate f(xmp)
ymp = 0.5 - sin(xmp) - xmp.^2;
% define the Bisection Method using for loop
for iteration = 1:5
if ymp < 0
% ymp is less than zero
xn = xmp;
else
% ymp is greater than zero
xp = xmp'
end
xmp = (xn + xp) / 2;
ymp = 0.5 - sin(xmp) - xmp.^2;
end
% display the root value
xmp
ymp
% plot the function and the root
xvals = linspace(0.1,0.5);
yvals = 0.5 - sin(xvals) - xvals.^2;
plot(xvals,yvals)
hold on
plot([0.1 0.5],[0 0])
plot(xmp,ymp,'r.','MarkerSize',30)
hold off
grid on
while abs(ymp) > 0.001
if ymp < 0
% ymp is less than zero
xn = xmp
else
% ymp is greater than zero
xp = xmp
end
xmp = (xn + xp) / 2;
ymp = 0.5 - sin(xmp) - xmp.^2;
end
x = linspace(-2,1);
y = 0.5-sin(x) -x.^2
plot(x,y);
hold on
plot([-2 1], [0 0]);
hold off
grid on
xp = 0;
xn = 0.5;
x0 = [-1.5 -1]
xp = -0.5, xn = 0.8;
xmp = ((xn + xp)/2);
ymp = 0.5-sin(xmp)-xmp^2
if ymp > 0
xp = xmp
else
xn = xmp
end
xmp = ((xn + xp)/2)
ymp = 0.5-sin(xmp)-xmp^2
if ymp > 0
xp = xmp
else
xn = xmp
end
xmp - ((xn + xp)/2)
% create brackets
xp = -0.5;
xn = 0.8;
% calculate the midpoint
xmp = (xn+xp)/2;
% calculate f(xmp)
ymp = 0.5 - sin(xmp) - xmp.^2;
% define the Bisection Method using for loop
for iteration = 1:5
if ymp < 0
% ymp is less than zero
xn = xmp;
else
% ymp is greater than zero
xp = xmp'
end
xmp = (xn + xp) / 2;
ymp = 0.5 - sin(xmp) - xmp.^2;
end
% display the root value
xmp
ymp
% plot the function and the root
xvals = linspace(0.1,0.5);
yvals = 0.5 - sin(xvals) - xvals.^2;
plot(xvals,yvals)
hold on
plot([0.1 0.5],[0 0])
plot(xmp,ymp,'r.','MarkerSize',30)
hold off
grid on
while abs(ymp) > 0.001
if ymp < 0
% ymp is less than zero
xn = xmp
else
% ymp is greater than zero
xp = xmp
end
xmp = (xn + xp) / 2;
ymp = 0.5 - sin(xmp) - xmp.^2;
end
x = linspace(-2,1);
y = 0.5-sin(x) -x.^2
plot(x,y);
hold on
plot([-2 1], [0 0]);
hold off
grid on
xp = 0;
xn = 0.5;
x0 = [-1.5 -1]