Borrar filtros
Borrar filtros

I need to find the smallest possible root.

5 visualizaciones (últimos 30 días)
Ewynne
Ewynne el 9 de Feb. de 2017
Comentada: Walter Roberson el 9 de Feb. de 2017
I don't understand how to make this work.I need to find the smallest possible root and have an output/printout which needs to include the root x^*, the value f(x^*), the number of iterations performed and whether convergence was reached or the maximum number of iterations was reached before convergence was achieved. With the stopping criterion f(x^*)<10^(-5), maximum iterations set to 20 and start with xL=0 and xR=2.5. This what I have, but it won't run, also I don't know how to do printout.
clear all;
xL=0
xR=2.5
for j=1:20
xM=(xL+xR)/2;
fM=sin(3*xM)-2cos(xM);
fL=sin(3*xL)-2cos(xL);
fR=sin(3*xR)-2cos(xR);
if fM*fL<0
xR=xM
elseif fM*fR<0
xL=xM
end
if abs(fM)<10^(-5)
break
end
end

Respuestas (1)

Walter Roberson
Walter Roberson el 9 de Feb. de 2017
fM=sin(3*xM)-2cos(xM);
MATLAB does not support implicit multiplication. You need a "*" or ".*" between the "2" and the "cos"
  3 comentarios
Ewynne
Ewynne el 9 de Feb. de 2017
Also, thank you!
Walter Roberson
Walter Roberson el 9 de Feb. de 2017
xL=0
xR=2.5
root_found = false;
for j=1:20
fprintf('Iteration #%d\n', j);
xM=(xL+xR)/2;
fM=sin(3*xM)-2*cos(xM);
fL=sin(3*xL)-2*cos(xL);
fR=sin(3*xR)-2*cos(xR);
if fM*fL<0
xR=xM
else
xL=xM
end
if abs(fM)<10^(-5)
root_found = true;
break
end
end
xM
fM

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Identification en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by