วันพุธที่ 29 สิงหาคม พ.ศ. 2555


check logic ของ Matlab
>> 1>0

ans =

     1

>> 1<0

ans =

     0

>> 1 < 0 & 2==1

ans =

     0

>> 2 ~= 1

ans =

     1

>> 1 <0

ans =

     0

>>  1<0 | 2~=1

ans =

     1

การแก้สมการ 3 ตัวแปร ของ matlab (สมการจาโคบี)

clear, clc

%linear system of equation
%A is 3*3 metrix
%Ax = b
A= [3  -0.1  -0.2
    0.1   7  -0.3
    0.3  -0.2  10];
%A = { 3  -0.1  -0.2; 0.1   7  -0.3; 0.3  -0.2  10];
%b is 3*1 metrix
b = [7.85; -19.3; 71.4];

%initial guess
x10 = 0; x20 = 0;  x30 = 0;
x1 = ( b(1) - A(1,2)*x20 - A(1,3)*x30 ) / A(1,1);
x2 = ( b(2) - A(2,1)*x10 - A(2,3)*x30 ) / A(2,2);
x3 = ( b(3) - A(3,1)*x10 - A(3,2)*x20 ) / A(3,3);

ep1 = 0.01; ep2 = 0.01; ep3 = 0.01;
OK = abs(x1-x10) < ep1 & abs(x2-x20) <ep2 & abs(x3-x30) < ep3; %check eror
while ~OK
    x10 = x1; x20=x2; x30=x3;
    x1 = ( b(1) - A(1,2)*x20 - A(1,3)*x30 ) / A(1,1);
    x2 = ( b(2) - A(2,1)*x10 - A(2,3)*x30 ) / A(2,2);
    x3 = ( b(3) - A(3,1)*x10 - A(3,2)*x20 ) / A(3,3);
    OK1 = abs(x1-x10) < ep1; %check eror of x1
    OK2 = abs(x2-x20) < ep2; %check eror of x2
    OK3 = abs(x3-x30) < ep3; %check eror of x3
    OK = OK1 & OK2 & OK3; %check eror of all2
end

งาน Reactor system


clear, clc
%ใส่ตัวแปร
q01=5;
q03=8;
q12=3;
q15=3;
q23=1;
q24=1;
q25=1;
q34=8;
q31=1;
q44=11;
q54=2;
q55=2;

%สมการ balance ในถัง
%-(q12+q15)*c1  +0                 +q31*c3        +0       +0            =-5*10
%q12*c1        -(q23+q24+q25)*c2  +0             +0       +0            =0
%0             +(q23)*c2          -(q31+q34)*c3  +0       +0            =-160
%0             +q24*c2            +q34*c3        -q44*c4  +q54*c5       =0
%q15*c1        +q25*c2            +0             +0       -(q54+q55)*c5 =0


A = zeros(5,5);
A(1,1) = -(q12+q15);
A(1,3) = q31;
A(2,1) = q12;
A(2,2) = -(q23+q24+q25);
A(3,2) = q23;
A(3,3) = -(q31+q34);
A(4,2) = q24;
A(4,3) = q34;
A(4,4) = -q44;
A(4,5) = q54;
A(5,1) = q15;
A(5,2) = q25;
A(5,5) = -(q54+q55);

b = zeros(5,1);
b(1)=-50;
b(3)=-160;
เมื่อ RUN แล้ว ในหน้าต่าง command window ให้ใส่

>> x = inv(A)*b
เพื่อหาค่า x
คำตอบจะได้
x =

   11.5094
   11.5094
   19.0566
   16.9983
   11.5094

งาน การใช้ fex ,fsolve
ในหน้าต่าง m file
ให้ใส่สมการเข้าไป


function y = fex_nonlinear(x)

y = [ x(1)^2 + x(1)*x(2) - 10
     x(2) + 3*x(1)*x(2)^2 - 57 ];



เมื่อ RUN แล้ววไปที่หน้าต่าง command window ให้ใส่

>> fex_nonlinear([1 1])

ans =

    -8
   -53

>> x = fsolve(@fex_nonlinear,[1 1])
Optimization terminated: first-order optimality is less than options.TolFun.

x =

    2.0000    3.0000

จากนั้นพิม
>>[x,fval,exitflag] = fsolve(@fex_nonlinear,[1 1])
Optimization terminated: first-order optimality is less than options.TolFun.

x =

    2.0000    3.0000


fval =

  1.0e-013 *

         0
   -0.2842


exitflag =

     1
****exitflag = 1 
1,2,3,4 คำตอบเชื่อถือได้
0,-1,-2,-3,-4 ไม่น่าเชื่อถือ

การให้ matlab ช่วยเรา (help)
ที่หน้าต่าง command window ให้ใส่ help fsolve
รอพักนึกจากนัน้กด doc fsolve ที่มันขึ้นมาให้
จากนั้นจะปรากฎหน้าต่าง แสดงว่า   fsolve ใช้อย่างไร มีความหมายอย่างไร

งาน ตัวอย่างการคำนวณการสกัด

(ใน m file )
function y = fsolve_ext(x)
%จากโจทย์
%1st-stage: cR = y2*S -y1*S -x1*R = 0
%2nd-stage: x1R + 0 - y2S - x2R =0
%y1 = mx1
%y2 = mx2
x1 = x(1);
x2 = x(2);
y1 = x(3);
y2 = x(4);

c = 0.1;
R = 1;
S= 12;
m = 0.125;

y = [c*R + y2*S - y1*S - x1*R
    x1*R - y2*S - x2*R
    y1 - m*x1
    y2 - m*x2];

เมื่อ RUN แล้ววไปที่หน้าต่าง command window ให้ใส่


>> x = fsolve(@fsolve_ext,[1 1 1 1])
จะขี้นว่า
Optimization terminated: first-order optimality is less than options.TolFun.

x =

    0.0526    0.0211    0.0066    0.0026
จากนั้น check exitflag เพื่อ check คำตอบ


>> [x,fval,exitflag] = fsolve(@fsolve_ext,[1 1 1 1])
จะขี้นว่า
Optimization terminated: first-order optimality is less than options.TolFun.

x =

    0.0526    0.0211    0.0066    0.0026


fval =

  1.0e-014 *

   -0.1082
    0.0555
   -0.0153
   -0.0167


exitflag =

     1



เสร็จจ้าาาาา!