Prev: request for an algorithm or code
Next: struct_ array
From: Torsten Hennig on 30 Jul 2010 05:45 > The basic "count = 0 and while" structure should look > something like this: > > count = 0; > maxcount = 1000; > while error > tol > count = count+1; > if count > maxcount > disp('Reached maximum iterations before error > re error within tolerance'); > break > end > % prepare variables for next iteration > end > > I don't have time to comment on the rest of the code > provided above, but if you don't add the count to the > while loop you run the risk of the while loop never > ending. Good point. So the actual status of the program looks something like count = 0; maxcount = 1000; tol = 0.01; error = 1.0; x_lower = -20.0; x_upper = 20.0; x_old(1) = 5; x_old(2) = 10; while error > tol count = count+1; if count > maxcount disp('Reached maximum iterations before error within tolerance'); break end % prepare variables for next iteration y_array(1) = x_old(1)+x_old(2); y_array(2) = x_old(2)^2+x_old(1); y = @(x) -(y_array(1)-x^2-x); z = @(x) -(y_array(2)^2-x-x^2); x_new(1) = fminbnd(y,x_lower,x_upper); x_new(2) = fminbnd(z,x_lower,x_upper); error = norm(x_old-x_new); x_old = x_new; end Best wishes Torsten.
From: Anna Kaladze on 30 Jul 2010 11:43 Torsten hi, Thanks a lot for your code! I have slightly adjusted your code, but looks like it does not work for a slightly different problem. It takes too long (or maybe I did smth. wrong). However, looks like if one sets x_old to [0.124993625436672, -0.624980876303235] it will equal to the feedback vector. But I can’t see why the code does not find that. Can you please have a look? Thanks. tol=0.01; q=9; x_old(1) = 0.1; x_old(2) = 0.1; error=1.0; x_lower = -20.0; x_upper = 20.0; while error > tol y_array(1) = x_old(1)+x_old(2); y_array(2) = x_old(2)^2-x_old(1); y = @(x) -(y_array(1)-x^2-x); z = @(x) -(y_array(2)^2-x-x^2); x_new(1) = fminbnd(y,x_lower,x_upper); x_new(2) = fminbnd(z,x_lower,x_upper); x_feedback(1) = x_new(1)-x_old(1); x_feedback(2) = x_new(2)-x_old(2); error = norm(x_old-x_new); x_old = x_old.*(1+error/q); end
From: Anna Kaladze on 30 Jul 2010 12:15 Hi Andy and Torsten, Thanks a lot for your replies. I didn't realize that you posted those last messages, so please ignore my last post. I will try to correct the code based on the "count" reccomendations to see if it works.
From: Anna Kaladze on 31 Jul 2010 02:28 Hi. The method does not work unfortunately. The code is: count = 0; maxcount = 1000; tol = 0.01; error = 1.0; x_lower = -20.0; x_upper = 20.0; x_old(1) = 1; x_old(2) = 1; while error > tol count = count+1; if count > maxcount disp('Reached maximum iterations before error within tolerance'); break end % prepare variables for next iteration y_array(1) = x_old(1)+x_old(2); y_array(2) = x_old(2)^2-x_old(1); y = @(x) -(y_array(1)-x^2-x); z = @(x) -(y_array(2)^2-x^2-x); x_new(1) = fminbnd(y,x_lower,x_upper); x_new(2) = fminbnd(z,x_lower,x_upper); x_feedback(1)=x_old(1)-x_new(1); x_feedback(2)=x_old(2)-x_new(2); error = norm(x_old-x_feedback); x_old = x_feedback; end The answer is x_old=[-0.0411221739472606,-0.126633531641413]. Can you please see what might be wrong? Thanks a lot.
From: Torsten Hennig on 31 Jul 2010 02:29
> Hi. The method does not work unfortunately. > The code is: > > count = 0; > maxcount = 1000; > tol = 0.01; > error = 1.0; > x_lower = -20.0; > x_upper = 20.0; > x_old(1) = 1; > x_old(2) = 1; > while error > tol > count = count+1; > if count > maxcount > disp('Reached maximum iterations before error within > tolerance'); > break > end > % prepare variables for next iteration > y_array(1) = x_old(1)+x_old(2); > y_array(2) = x_old(2)^2-x_old(1); > y = @(x) -(y_array(1)-x^2-x); > z = @(x) -(y_array(2)^2-x^2-x); > x_new(1) = fminbnd(y,x_lower,x_upper); > x_new(2) = fminbnd(z,x_lower,x_upper); > x_feedback(1)=x_old(1)-x_new(1); > x_feedback(2)=x_old(2)-x_new(2); > error = norm(x_old-x_feedback); > x_old = x_feedback; I don't understand why you set x_old = x_feedback here instead of x_old = x_new. If the iteration converges, you will have x_feedback = 0 in the end and thus x_old=0 in the end. I don't think that this is what you want. From your first mail I assumed that you wanted to update x_guess by x_new - but this does not seem to be the case - does it ? > end > The answer is > x_old=[-0.0411221739472606,-0.126633531641413]. Can > you please see what might be wrong? Thanks a lot. Best wishes Torsten. |