Hi, I am having a hard time finding the root to a polynomial function using the bisection method

6 Ansichten (letzte 30 Tage)
Hi,
I am having a hard time finding the root to a polynomial function using the bisection method with the precision of 0.001. This is not a homework question. I am new at this and just trying to learn. This is what I have, but I have errors. I do appreciate any help or advice.
function root[x_lower,x_upper] 3*x.^3-10;
x_lower=-5;
x_upper=5;
x_mid= (x_lower* X_upper)/2;
while abs (f(x_mid))>0.001
if (f(x_mid)*f(x_upper))<0
x_lower=x_mid
else
x_upper=x_mid
end
x_mid=(x_lower+x_upper)/2;
end
fprintf('the root is %g\n', x_mid)
  2 Kommentare
Naeem Khan
Naeem Khan am 14 Dez. 2020
what is this last command used for "fprintf('the root is %g\n', x_mid)"
Walter Roberson
Walter Roberson am 15 Dez. 2020
The fprintf() in that form displays to the command window the text
the root is
followed on the same line by the value of the variable x_mid, with the value represented in direct decimal notation if its absolute value is in the range 0.0001 to 999999, and otherwise representing it in scientific notation. After that, newline is output to move on to the next line

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Feb. 2016
Change
function root[x_lower,x_upper] 3*x.^3-10;
to
function root(x_lower,x_upper)
f = @(x) 3*x.^3-10;
  3 Kommentare
Walter Roberson
Walter Roberson am 7 Feb. 2016
X_upper and x_upper are different variable names. You have used both. You need to be consistent.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by