フィルターのクリア

Plotting with bisection method

19 ビュー (過去 30 日間)
ILoveMath
ILoveMath 2022 年 4 月 28 日
回答済み: Chunru 2022 年 4 月 28 日
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 Center および File ExchangeMATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by