Determine if valid initial guess for closed numerical method

If I'm given a function f(x), how can I determine whether say [0 1] or [1,2 ] are valid initial guesses for a closed numerical method, of solving f(x)=0.

답변 (1개)

Torsten
Torsten 2018년 2월 21일
편집: Torsten 2018년 2월 21일

0 개 추천

If you want to use "fzero", you can first evaluate f in the endpoints of the interval [a b] you provide. If sign(f(a)*f(b)) < 0, you provide a valid initial guess.
Or did you want to ask something else - because your question was quite unclear ?
Best wishes
Torsten.

댓글 수: 4

Well, I'm trying to write code for bisection method, to find the roots of a function. It's for an assignment, and they are asking of us to first check if the guesses are valid and then to "determine the result such that f(x)< abs(E) where E=0.1." I'm guessing I should do the "if" statement and below it a bunch of code intended to find the root and then after that "else disp('Invalid guess')". But I was looking at some notes from class, and they wrote it out like this:
xa=10
xb=21
iteration=1
while iteration<= 30 && abs(xb-xa)>=10^-4
xc= (xa+xb)/2
if f(xc)*f(xa)<0
xb=xc
else
xa=xc
end
iteration=iteration+1
end
This confuses me because they have if f(xc)*(xa)<0 (which makes sure it's a valid guess), but then else (if >0), they say that xa=xc ? Shouldn't it be that the guess wasn't valid?
Rachel Dawn
Rachel Dawn 2018년 2월 21일
편집: Rachel Dawn 2018년 2월 21일
Oh wait! I just realized that it says f(xc), not f(xb), so I would do if f(xa)*f(xb)>0 code to find root and else *disp('invalid guess')
But I'm still not sure about the f(x)<abs(E) part. What does this imply? & should it be placed at the end of the code?
Also, sorry for all the questions, but in the following code:
while(iteration <=100 && abs(xleft-xright)>10^-3
it's part of the bisection method, but what does that abs part imply? why is it necessary?
Try
xa = 10;
xb = 21;
fxa = f(xa);
fxb = f(xb);
if fxa*fxb > 0
disp('Invalid initial guess.')
end
iteration = 1;
E = 0.1;
error_f = 1.0;
while (iteration < 100 && error_f >= E)
xc = (xa+xb)/2;
fxc = f(xc);
if fxc*fxa<0
xb = xc;
else
xa = xc;
fxa = fxc;
end
error_f = abs(fxc);
iteration = iteration+1;
end
xc
You do not need to include the abs-part from above in the while-statement because the number of iterations already determines the minimum length of the interval in which you search for the root.
Best wishes
Torsten.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 2월 21일

댓글:

2018년 2월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by