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 (최근 30일)
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
Walter Roberson
Walter Roberson 2016년 2월 18일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

John BG
John BG 2016년 2월 18일
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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 18일
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.

카테고리

Help CenterFile Exchange에서 MuPAD에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by