Has to be postive integer or logical.

조회 수: 2 (최근 30일)
Andy
Andy 2012년 10월 13일
Attempted to access t(0); index must be a positive integer or logical.
I'm trying to make it that as t is 0, the value is a given input "a". Here's my code:
function y = mynewton(f,a,n)
t(0)=a;
for i=[0:n]
t(i+1)=t(i-1)-f(t(i-1))/diff(f(x),t(i-1));
end
y=t;
  댓글 수: 1
per isakson
per isakson 2012년 10월 13일
Matlab indexing is one-based by design

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

채택된 답변

Wayne King
Wayne King 2012년 10월 13일
편집: Wayne King 2012년 10월 13일
MATLAB indexes from 1, not 0 like C, so you must do
t(1) = a;
and then in your for loop, you cannot start from 1, because you attempt to access t(i-1) and f(i-1). Starting from i = 1, this would error.
Also, I'm not sure what you're trying to do with f(x). MATLAB will not understand what you mean by f(x), that is human notation for doing math.
It looks like you are trying to do numerical computation, not symbolic, in which case f will be a vector. You have to address elements of a vector by using meaningful indices from 1 to the length of the vector.
  댓글 수: 1
Andy
Andy 2012년 10월 13일
Thank you so much for help me out.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by