matlab code for iterative equation

조회 수: 6 (최근 30일)
segun egbekunle
segun egbekunle 2016년 6월 18일
댓글: Alper Olca 2020년 3월 27일
Please I need matlab code to solve this iterative equation X (k+1)= c+ Tx(k) For k=0,1,2,3… with the input value c, T and x and stops when the iteration converges .
  댓글 수: 1
Star Strider
Star Strider 2016년 6월 18일
Use a while loop. Decide on what ‘converges’ means in this context.
Write your code. If you have problems, post it here, along with any error it throws (copy and paste all the red text in the Command Window to a Comment here).
Experiment! Unless your code somehow manages to connect to the nuclear missile command codes, the world will not come to an end if it throws an error.

댓글을 달려면 로그인하십시오.

답변 (3개)

Roger Stafford
Roger Stafford 2016년 6월 18일
If you like the lazy approach to problems, note that if abs(T) < 1, you can rewrite your equation as:
x(k+1)-b = T*(x(k)-b)
where b = c/(1-T), and therefore
x(k+1)-b = T^k*(x(1)-b).
In this form it is obvious what x(k) will converge to, namely b, since x(k)-b must converge to zero. Accordingly, carrying out all those tedious iterations becomes unnecessary. As I say, that is the lazy method.
  댓글 수: 1
Alper Olca
Alper Olca 2020년 3월 27일
x1=5;
x2=5;
x3=5;
x4=5;
td=10^-2;
a=0;
for i= 1:10
a=0+i;
if( (abs(x1-x1)<td && abs(x2-x2)<td) && (abs(x3-x3)<td)&& abs(x4-x4)<td)
x1=(-23+x2-x3+2*x4)/4;
x2=(-21-2*x1+x3-3*x4)/6;
x3=(-11+x1+2*x2-x4)/5;
x4=(22+x1-2*x2+3*x3)/6;
end
k=(4*x1-x2+x3-2*x4);
l=(2*x1+6*x2-x3+3*x4);
m=(-x1-2*x2+5*x3+x4);
n=(-x1+2*x2-3*x3+6*x4);
end
rslt=[k l m n ; x1 x2 x3 x4]

댓글을 달려면 로그인하십시오.


segun egbekunle
segun egbekunle 2016년 6월 26일
편집: Walter Roberson 2016년 6월 26일
x0=zeros(1,n)';
for k=1 :itmax+1
x= c + T*x;
disp(x)
need improvement for the iteration to stop after obtaining the same value twice
  댓글 수: 3
segun egbekunle
segun egbekunle 2016년 6월 29일
it gives error message at if all(x == xold) and xold(end) = x; i have modified it to num_previous = 2; xold = nan(1, num_previous); for k = 1 : itmax + 1; x= c + G*x;
end
xold(1:end-1) = xold(2:end);
disp(x)
now it converges but i need little modification to display the number of iterations before convergence and to display no convergence when there is no convergence
Alper Olca
Alper Olca 2020년 3월 27일
x1=5;
x2=5;
x3=5;
x4=5;
td=10^-2;
a=0;
for i= 1:10
a=0+i;
if( (abs(x1-x1)<td && abs(x2-x2)<td) && (abs(x3-x3)<td)&& abs(x4-x4)<td)
x1=(-23+x2-x3+2*x4)/4;
x2=(-21-2*x1+x3-3*x4)/6;
x3=(-11+x1+2*x2-x4)/5;
x4=(22+x1-2*x2+3*x3)/6;
end
k=(4*x1-x2+x3-2*x4);
l=(2*x1+6*x2-x3+3*x4);
m=(-x1-2*x2+5*x3+x4);
n=(-x1+2*x2-3*x3+6*x4);
end
rslt=[k l m n ; x1 x2 x3 x4]

댓글을 달려면 로그인하십시오.


segun egbekunle
segun egbekunle 2016년 6월 29일
i have modified my previuos answer to
num_previous = 2; xold = nan(1, num_previous); for k = 1 : itmax + 1; x= c + T*x;
end
xold(1:end-1) = xold(2:end);
disp(x)
now it converges but i need little modification to display the number of iterations before convergence and to display no convergence when there is no convergence

카테고리

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