Plotting with bisection method

108 visualizaciones (últimos 30 días)
ILoveMath
ILoveMath el 28 de Abr. de 2022
Respondida: Chunru el 28 de Abr. de 2022
I have this so far:
clc;
xlabel('x')
ylabel('f(x)')
title('Roots of f(x)= -x^4+12x^2-3x-5')
fx= @(x) -x.^4+12*x.^2-3*x-5;
tol= 10^-10;
a=0;
b=1;
while 1
c=(a+b)/2;
if abs(fx(c)) <= tol
fprintf('foud the root at x= %f\n', c);
break;
end
if fx(a)*fx(c)<0
b=c;
else
a=c;
end
end
xx= -4:.1:4;
plot(xx,fx(xx),'b-');
grid on;
hold on;
plot(c,fx(c),'r*', 'Markersize', 10);
xlabel('x')
ylabel('f(x)')
title('Roots of f(x)= -x^4+12x^2-3x-5')
Need help getting to this:

Respuesta aceptada

Chunru
Chunru el 28 de Abr. de 2022
Bisection method tries to find solution in a given interval. Generally, you don't know how many roots a non-linear equation has and where the roots are. So it might be an guess and check problem. For your problem, you can plot the curve and estimate where the roots are roughly located.
clc;
xlabel('x')
ylabel('f(x)')
title('Roots of f(x)= -x^4+12x^2-3x-5')
fx= @(x) -x.^4+12*x.^2-3*x-5;
tol= 10^-10;
% intervals of all roots
aa = [-4 -2 0 2];
bb = [-2 0 2 4];
xx= -4:.1:4;
plot(xx,fx(xx),'b-');
xlabel('x')
ylabel('f(x)')
title('Roots of f(x)= -x^4+12x^2-3x-5')
grid on;
hold on;
for i_interval = 1:length(aa)
a=aa(i_interval);
b=bb(i_interval);
while 1
c=(a+b)/2;
if abs(fx(c)) <= tol
fprintf('foud the root at x= %f\n', c);
break;
end
if fx(a)*fx(c)<0
b=c;
else
a=c;
end
end
plot(c,fx(c),'r*', 'Markersize', 10);
end
foud the root at x= -3.528261 foud the root at x= -0.537768 foud the root at x= 0.809110 foud the root at x= 3.256919

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by