พี่ๆช่วยดูทีครับ (Mathlab)
% Illustration of Jacobi iteration % (Also known as Method of Simultaneous Displacement) % % The goal is to determine a set of values of x1...x4 that % satisfy the following equations... % f1(x1,x2,x3,x4) = -2*x1 + x2 + 1.5 = 0 % f2(x1,x2,x3,x4) = x1 - 2*x2 + x3 - 0.5 = 0 % f3(x1,x2,x3,x4) = x2 - 2*x3 + x4 - 0.5 = 0 % f4(x1,x2,x3,x4) = x3 - 2*x4 + 0.5 = 0 % % We will take an x1 out of f1 and an x2 out of f2 and so on. % x1 = lin_g1(x1,x2,x3,x4) = (x2 + 1.5) / 2 % x2 = lin_g2(x1,x2,x3,x4) = (x1 + x3 - 0.5) / 2 % x3 = lin_g3(x1,x2,x3,x4) = (x2 + x4 - 0.5) / 2 % x4 = lin_g4(x1,x2,x3,x4) = (x3 + 0.5) / 2 % % These are defined in files 'lin_g1.m' through 'lin_g4.m'
fprintf('\nSolution of System of Equations using Jacobi iteration\n');
x1 = input('Enter x1 estimate : '); x2 = input('Enter x2 extimate : '); x3 = input('Enter x3 estimate : '); x4 = input('Enter x4 extimate : '); maxerror = input('Enter max approx error : '); maxit = input('Enter max # of iterations: ');
count = 0; error = 100; fprintf('\niteration x1 x2 x3 x4\n');
while (error > maxerror) & (count < maxit) count = count + 1; x1new = lin_g1(x1,x2,x3,x4); x2new = lin_g2(x1,x2,x3,x4); x3new = lin_g3(x1,x2,x3,x4); x4new = lin_g4(x1,x2,x3,x4);
error1 = abs(x1new - x1); error2 = abs(x2new - x2); error3 = abs(x3new - x3); error4 = abs(x4new - x4); error = max([error1,error2,error3,error4]); fprintf('%6i %10.6f %10.6f %10.6f %10.6f\n',... count,x1new,x2new,x3new,x4new);
x1 = x1new; % assign new values x2 = x2new; x3 = x3new; x4 = x4new; end
รันแล้ว input x1,x2,x3,x4,maxerror,maxit ได้ แต่มันคำนวนหาผลลัพธ์ออกมาไม่ได้ครับ ช่วยแนะนำทีครับ ช่วยดูทีครับ
|