필터 지우기
필터 지우기

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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by