필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

working on a bisection code and im pretty lost is it right so far?

조회 수: 2 (최근 30일)
adriana resendez
adriana resendez 2015년 2월 25일
마감: MATLAB Answer Bot 2021년 8월 20일
function [xr ea iter] = bisect(func,x1, xu, es, maxit)
if func(x1)*func(xu)>=0, error('no sign change'); end
xold = x1;
for iter=1:maxit
xr = ???
test = func(x1)*func(xr)
ea = abs((xr-xold)/xr)*100;
if test<0, xu = xr;
elseif test>0; ???;
else ea = 0;
end
  댓글 수: 2
John D'Errico
John D'Errico 2015년 2월 25일
Please use the code formatting button "{} Code" when you post code. Your code is unreadable as it is.
John D'Errico
John D'Errico 2015년 2월 25일
편집: John D'Errico 2015년 2월 25일
Having fixed your code (I'll do that once) to make it readable...
Is it right? Of course not.
You have assignments like
xr = ???
and
???;
That last was a good one, though not truly valid syntax. I'll admit that when I stop writing code to take a rest, I usually add a random line like
jw56ywykmej
This way I cannot miss finding it when I resume work later on. At the very least, mlint will tell me about it.
Your code is missing end statements. Lots of other issues I could pick at. For example, why use a for loop instead of a while loop? A for loop forces the code to run for maxit iterations. What if it has converged before then? Yes, this last is a minor issue that relates to efficiency, instead of does the code work.
Essentially, you are going in a reasonable direction, but you are not there yet. So why not finish writing it? Try it out? Learn to debug your code, so learn to use the debugger.

답변 (0개)

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by