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