While loop and previous values

조회 수: 2 (최근 30일)
Una Ib
Una Ib 2017년 11월 26일
편집: KL 2017년 11월 26일
So, I have a vector x, with the starting values x0=[0;0;0]
while abs(x(1)-y(1))> epsilon || abs(x(2)-y(2))> epsilon || abs(x(3)-y(3))> epsilon
dk = -f_gradient(x);
alphak = (dk' * dk)/(dk' * A * dk);
x = x + alphak * dk;
end
How do I set the starting value here? I can't write x= x0+ alphak * dk for obvious reasons, and I cant use a for loop because I don't know what the endindex in the while loop will be. Is there a possibility to add another while loop inside my loop that adds to x for as long as the first while loop holds? If so, how would I write that? Thanks a lot :)

답변 (1개)

KL
KL 2017년 11월 26일
편집: KL 2017년 11월 26일
I suppose y is a constant 3x1 vector and only x is changing in this while loop. So simply assign the intial values of x to x itself,
x = x0;
while any(abs(x-y)>epsilon)
%code
end
I do not understand the "obvious reasons", if the above doesn't make any sense because of your f_gradient function, what value are you assigning to x in the first place?

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by