using last result to start an iteration process and continously

1 次查看(过去 30 天)
Please how do I make an answer gotten to repeat a process of two variables and then until its convergence. In other words, I don't know the number of iterations to be done.

回答(1 个)

Guillaume
Guillaume 2015-2-17
I'm sorry to say that your question is not clear at all. It's obvious that English is not your native language.
Anyway, to have a loop that terminates under a given condition, use while:
condition = false;
while ~condition
condition = resultofsomeoperation();
end
  2 个评论
bayoishola20
bayoishola20 2015-2-18
Sorry about the unclear question,
clear all % Clear variable cache;workspace
clc % Clear screen;command window
% Initial x1 and x2
x1 = 5, x2 = 4
% Equations
y1 = (x1-3)^2 + (2*x2 - 8)^2
y2 =(x1-4)^2 + (x2 - 10)^2
%Given y values
y = [5;49]
% First set of y values
yo = [y1;y2]
f = y - yo
syms xo1 xo2 % xo1,xo2 are symbols
A11 = diff((x1-3)^2 + (2*x2 - 8)^2,x1) %differentiation wrt "xo1"
A12 =diff((x1-3)^2 + (2*x2 - 8)^2,x2) %differentiation wrt "xo2"
A21 =diff((x1-4)^2 + (x2 - 10)^2,x1) %differentiation wrt "xo1"
A22 =diff((x1-4)^2 + (x2 - 10)^2,x2) %differentiation wrt "xo2"
A = [A11 A12;A21 A22]
% Initial x1 and x2
for i = 5
x1 = 5, x2 = 4
A = [ 2*x1 - 6, 8*x2 - 32; 2*x1 - 8, 2*x2 - 20]
dx = inv(A'*A)*(A'*f)
dx1 = dx(1,1)
dx2 = dx(2,1)
[dx1;dx2]
new_x1_x2 = [x1+dx1; x2+dx2]
end
I need the values of x1 and x2 to be repeated so that at the end of the day, it does converge. More like the newton raphson, Runge-Kutta and co. iterations.
Guillaume
Guillaume 2015-2-19
Like I said, use a while:
%initialise data
%... calculate A, f, etc.
x1 = 5, x2 = 4;
convergencethreshold = 1e-8; %or whatever you want
dx = [Inf Inf];
while any(abs(dx) > convergencethreshold)
A = [2*x1 - 6, 8*x2 - 32; 2*x1 - 8, 2*x2 - 20]
dx = inv(A'*A)*(A'*f)
x1 = x1 + dx;
x2 = x2 + dx;
end

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by