Borrar filtros
Borrar filtros

how do i put iteration values into a vector?

1 visualización (últimos 30 días)
Robert
Robert el 7 de Dic. de 2013
Comentada: Robert el 7 de Dic. de 2013
Hi, im creating a matlab function that uses the secant method to create an approximation, im trying to take the values that are calculated in my while loop and put them into one vector showing all the values computed at each iteration, i can get my final iterated value to be the first element in the vector but cant get the values calculated before to appear my while loop looks like this
xold=x0;
xa=x1-(f(x1)*(x1-xold))/(f(x1)-f(xold));
iter=0;
while abs(xa-xold)>=D;
iter=iter+1;
xnew=xa-(f(xa)*(xa-xold))/(f(xa)-f(xold));
xold=xa;
xa=xnew;
end
in this x0 and x1 are initial values, iter is the number of iterations and D is the tolerance level of the funtion
also where abouts in this function do i need to put the vector code to show the iterations?

Respuesta aceptada

Wayne King
Wayne King el 7 de Dic. de 2013
Editada: Wayne King el 7 de Dic. de 2013
I'm not sure from your post, which values you want to retain over the life of the while loop, I'm assuming xnew
I would set the iter to 1 because you can't index a vector from zero in MATLAB and then increment the iter loop counter at the end of the while loop.
Without knowing all your variables, I can't test this, but does the following work?
xold=x0;
xa=x1-(f(x1)*(x1-xold))/(f(x1)-f(xold));
iter=1;
X = [];
while abs(xa-xold)>=D;
X(iter) =xa-(f(xa)*(xa-xold))/(f(xa)-f(xold));
xold=xa;
xa=xnew;
iter=iter+1;
end
  3 comentarios
Wayne King
Wayne King el 7 de Dic. de 2013
I've edited my post slightly. Doesn't the above do what you want?
Robert
Robert el 7 de Dic. de 2013
Done it, thanks so much!!!!!!

Iniciar sesión para comentar.

Más respuestas (1)

sixwwwwww
sixwwwwww el 7 de Dic. de 2013
Editada: sixwwwwww el 7 de Dic. de 2013
X = 0;
xold=x0;
xa=x1-(f(x1)*(x1-xold))/(f(x1)-f(xold));
iter=0;
X(iter + 1) = xa;
while abs(xa-xold)>=D;
iter=iter+1;
xnew=xa-(f(xa)*(xa-xold))/(f(xa)-f(xold));
xold=xa;
xa=xnew;
X(iter + 1) = xa;
end

Categorías

Más información sobre Programming 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!

Translated by