Plotting with bisection method

61 次查看(过去 30 天)
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:

采纳的回答

Chunru
Chunru 2022-4-28
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by