How do I implement a Matlab function, findtbracket.m, that, when a random function is inputed, the file will find the initial interval of the function?

1 visualización (últimos 30 días)
The assignment asks us to implement a Matlab file, findtracket.m, of the form
function [a,b]=findbracket(f,X0)
% f: function handle f(x) to find a zero for
% x0: starting point/center of interval containing zero
to try to find an initial interval [a,b] containing the input x_0 and bracketing the zero of f(x)
Also, the function should begin with a=b=x_0 and a step size del=2^(-k) choosen so that
fl(x_0-del)<fl(x_0-del/2)=x_0
While sgn f(a)=sgn f(b), decrease a by del, increase b by del, evaluate f(a) and f(b) and double del.
This is what I have so far (I am a Matlab newbie):
function [a,b]=find bracket(f,x0)
a=x0;
b=x0;
while 1
a=a-dx;
if f(a)*f(b)<0,break;
b=b+del;
if f(a)*f(b)<0, break;
end
  1 comentario
Walter Roberson
Walter Roberson el 18 de Feb. de 2016
The PNG image asks for "findbracket" not "findtracket".
You are not showing us how you invoke the code. And for sure you will need del and/or dx to be defined to use your code.

Iniciar sesión para comentar.

Respuesta aceptada

John BG
John BG el 18 de Feb. de 2016
Mónica
prueba lo siguiente:
x0=4.333
a=x0
b=x0
k=1
f=@(x) sin(x)
k3=1
while ~(floor(x0-2^(-k3-1))==floor(x0))
k3=k3+1
end
k4=1
while ~(floor(x0-2^-k4)==floor(x0))
k4=k4+1
end
k=max(k3,k4)
d=2^-k
while (sign(f(a))==sign(f(b)))
a=a-d
b=b+d
d=2*d
end
da
a = 2.583333000000000
b = 6.083333000000000
me parece que el enunciado que has adjuntado, implementado tal y como se lee, se cuelga para ciertos valores de x0, por ejemplo, tanto
k2=1
while ~(floor(x0-2^(-k2-1))==x0 && floor(x0-2^-k2)<x0)
k2=k2+1
end
como
k2=1
while ~(floor(x0-2^(-k2-1))==floor(x0) && floor(x0-2^-k2)<floor(x0))
k2=k2+1
end
se cuelgan para x0=4.9
por lo que quizás sea posible modificar el enunciado así:
Si esta respuesta te resulta de utilidad para solucionar tu pregunta, te agradeceria me votes haciendo click en el link que tiene el pulgar arriba al lado de esta respuesta.
Atentamente
John

Más respuestas (1)

Walter Roberson
Walter Roberson el 18 de Feb. de 2016
It is not acceptable to have a space in the name of a function. "findbracket" not "find bracket".
Your code does not initialize dx or del. It appears to me from the PNG that dx and del should be the same value.

Categorías

Más información sobre MuPAD en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by