SHARE
TWEET

Untitled

a guest May 19th, 2019 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Doolitle
  2. function [x]=doolittle(a,b)
  3.   n=size(a,1)
  4.   l=eye(n,n)
  5.   u=zeros(n,n)
  6.  
  7.   for i=1:n,
  8.     u(1,i)=a(1,i);
  9.   end
  10.   for i=2:n,
  11.     l(i,1)=a(i,1)/u(1,1);
  12.   end
  13.   for j=2:n-1,
  14.     for k=j:n,
  15.       y=0;
  16.       for s=1:j-1,
  17.         y=y+l(j,s)*u(s,k);
  18.       end
  19.       u(j,k)=a(j,k)-y;
  20.       for q=j:n-1,
  21.         z=0;
  22.         for p=q+1:n,
  23.           z=z+l(p,s)*u(s,q);
  24.           l(p,q)=1/u(q,q)*(a(p,q)-z);
  25.         end
  26.       end
  27.     end
  28.   end
  29.   w=0;
  30.   for s=1:n-1,
  31.     w=w+l(n,s)*u(s,n);
  32.   end
  33.   u(n,n)=a(n,n)-w;
  34.   y=l\b;
  35.   x=u\y;
  36. endfunction
  37.  
  38. Crout
  39. function [x]=crout(a,b)
  40.   n=size(a,1)
  41.   l=zeros(n,n)
  42.   u=eye(n,n)
  43.  
  44.   for i=1:n,
  45.     l(i,1)=a(i,1);
  46.   end
  47.   for i=2:n,
  48.     u(1,i)=a(1,i)/l(1,1);
  49.   end
  50.   for k=2:n-1,
  51.     for j=k:n,
  52.       y=0;
  53.       for s=1:k-1,
  54.         y=y+l(j,s)*u(s,k);
  55.       end
  56.       l(j,k)=a(j,k)-y;
  57.       for p=k:n-1,
  58.         z=0;
  59.         for q=p+1:n,
  60.           z=z+l(p,s)*u(s,q);
  61.           u(p,q)=1/l(p,p)*(a(p,q)-z);
  62.         end
  63.       end
  64.     end
  65.   end
  66.   w=0;
  67.   for s=1:n-1,
  68.     w=w+l(n,s)*u(s,n);
  69.   end
  70.   l(n,n)=a(n,n)-w;
  71.   y=l\b;
  72.   x=u\y;
  73. endfunction
  74.  
  75. Hasil Eksekusi
  76. -->A=[3 -1 1;10 3 -2;1 1 -1]
  77.  A  =
  78.     3.   - 1.    1.  
  79.     10.    3.  - 2.  
  80.     1.     1.  - 1.  
  81.  
  82. -->A=[3 -1 1;10 3 -2;1 1 -1];
  83.  
  84. -->B=[12;-23;6];
  85.  
  86. -->getd
  87.  
  88. -->x=doolittle(A,B)
  89.  x  =
  90.     4.5  
  91.   - 71.  
  92.   - 72.5  
  93.  
  94. -->x=crout(A,B)
  95.  x  =
  96.     4.5  
  97.   - 71.  
  98.   - 72.5  
  99.  
  100. -->x=A\B
  101.  x  =
  102.     4.5  
  103.   - 71.  
  104.   - 72.5  
  105.  
  106. -->A=[2 6 -1;1 -8 3;1 6 -10];
  107.  
  108. -->B=[0;0;0];
  109.  
  110. -->getd
  111. Warning :redefining function: crout                  
  112. Warning :redefining function: doolittle              
  113.  
  114. -->x=doolittle(A,B)
  115.  x  =
  116.     0.  
  117.     0.  
  118.     0.  
  119.  
  120. -->x=crout(A,B)
  121.  x  =
  122.     0.  
  123.     0.  
  124.     0.  
  125.  
  126. -->x=A\B
  127.  x  =
  128.     0.  
  129.     0.  
  130.     0.  
  131.  
  132. -->A=[1 1 -3;2 2 -6;1 -9 2];
  133.  
  134. -->B=[9;18;3];
  135.  
  136. -->getd
  137. Warning :redefining function: crout                  
  138. Warning :redefining function: doolittle              
  139.  
  140. -->x=doolittle(A,B)
  141.  !--error 27
  142. division by zero...
  143. at line      23 of function doolittle called by :  
  144. x=doolittle(A,B)
  145.  
  146. -->x=crout(A,B)
  147.  !--error 27
  148. division by zero...
  149. at line      23 of function crout called by :  
  150. x=crout(A,B)
  151.  
  152. -->x=A\B
  153.  warning
  154.  matrix is close to singular or badly scaled. rcond =    0.0000D+00
  155. computing least squares solution. (see lsq)
  156.  
  157.  x  =
  158.     0.    
  159.   - 1.08  
  160.   - 3.36  
  161.  
  162. -->diary(0)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top