ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ
ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ

Need help on solving this bisection method question

์กฐํšŒ ์ˆ˜: 23 (์ตœ๊ทผ 30์ผ)
Kaitlyn Waters
Kaitlyn Waters 2020๋…„ 11์›” 23์ผ
ํŽธ์ง‘: Alan Stevens 2020๋…„ 11์›” 23์ผ
: Given the equation ๐‘“(๐‘ฅ) = โˆ’2๐‘ฅ^6 โˆ’ 1.6๐‘ฅ^4 + 12๐‘ฅ + 1
Write a code to use the bisection method to calculate the maximum between [0,1]. Iterate until the approximate absolute error falls below 5%. Print the root and number of iterations on the screen.
I've done this so far with the script but I don't know where to go from here. Please help.
syms x
f(x)=-2*x^6-1.6*x^4+12*x+1;
dfdx= diff(f,x);
a=0;
b=1;
i=0;
err=1000;
while err>0.05
c=(a+b)/2
if double((dfdx(a)))*double((dfdx(c)))<0
a=c
else
b=c
end
err=;
i=i+1;
end
fprintf('The max of equation in [0,1] is %f by %d',c, i)

์ฑ„ํƒ๋œ ๋‹ต๋ณ€

Alan Stevens
Alan Stevens 2020๋…„ 11์›” 23์ผ
ํŽธ์ง‘: Alan Stevens 2020๋…„ 11์›” 23์ผ
You really don't need any symbolic parameters here; and you had the logic with a and b the wrong way round! Try
f = @(x) -2*x.^6-1.6*x.^4+12*x+1;
dfdx= @(x) -12*x.^5 - 6.4*x.^3 + 12;
a=0;
b=1;
i=0;
err=1000;
while err>0.05
c=(a+b)/2;
if dfdx(a)*dfdx(c)<0
b=c;
else
a=c;
end
err = abs(dfdx(c)); % Assuming the error is in the gradient
i=i+1;
end
fprintf('The max of equation in [0,1] is %f in %d iterations',f(c), i)

์ถ”๊ฐ€ ๋‹ต๋ณ€ (0๊ฐœ)

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Numbers and Precision์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

Community Treasure Hunt

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

Start Hunting!

Translated by