필터 지우기
필터 지우기

Info

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

What am I doing wrong (NEwtons method?)

조회 수: 2 (최근 30일)
Marina Pirzada
Marina Pirzada 2020년 5월 10일
마감: MATLAB Answer Bot 2021년 8월 20일
Command window says I have an error on line 10. I have no clue whats wrong with my code though.

답변 (2개)

Cris LaPierre
Cris LaPierre 2020년 5월 10일
Much more helpful if you copy/paste the entire error as well as the code.
My initial guess is that you are getting an error that the index must be a positive integer? In your for loop, your loop counter i starts at 1. In you conditional statement on line 10, you use x(i-1). When i=i, it attempts to extract the value at x(0). However, in MATLAB, indexing starts at 1, not 0.
The simplest solution is to have your for loop index start at 2.

Walter Roberson
Walter Roberson 2020년 5월 10일
for i=1:Nmax
so i starts at 1.
if min(abs(x(i)-x(i-1)))<xtol||min(abs(f1(x(i))))<ftol
i is a scalar. x(i) and x(i-1) are scalars. abs() of subtracting two scalars would be a scalar. What is the purpose of taking min() of a scalar?
On the first iteration, i is 1. x(i)-x(i-1) is x(1)-x(1-1) which is x(1)-x(0) . But x(0) is not a legal subscript in MATLAB.
Notice that when you got to that line, you have just computed x(i+1) but you do not use x(i+1) in your test to see if you have finished.

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by