Has to be postive integer or logical.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Respuesta aceptada
Wayne King
el 13 de Oct. de 2012
Editada: Wayne King
el 13 de Oct. de 2012
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!