Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scilab 4.34 KB | None | 0 0
  1.  
  2. //***************************************************
  3. //TP 2
  4. //***************************************************
  5.  //*****************************************************
  6. //affichage des systèmes d'équations linéaires
  7. //*****************************************************
  8.  
  9. function str2=clean_complexe(str1)
  10. //pour afficher correctement le coefficient de x_i
  11. //cas général nombre complexe: (a+ib)x
  12. // cas particuliers (réel ou imaginaire pur):  ax ou ibx
  13. if grep(str1,'%i')<>[] then str2=strsubst(str1,'%i','i')
  14.                             if (grep(str2,'+')<>[])|(grep(str2,'-')<>[]) then str2='('+str2+')'
  15.                             end
  16.                             str2=strsubst(str2,'*','')
  17.                        else str2=str1
  18. end
  19. endfunction
  20.  
  21.  
  22. function str2=un(str1)
  23. //comme clean_complexe mais gère le cas particulier
  24. //des coefficients  +1 et -1
  25. if grep(str1,'%i')<>[] then str2=strsubst(str1,'%i','i')
  26.                             if (grep(str2,'+')<>[])|(grep(str2,'-')<>[]) then str2='('+str2+')'
  27.                             end
  28.                             str2=strsubst(str2,'*','')
  29.         elseif stripblanks(str1)=="1" then str2=" "
  30.                elseif stripblanks(str1)=="-1" then str2="-"
  31.                                               else str2=str1
  32. end
  33. endfunction
  34.  
  35. function sol=plus(bool)
  36. //ecrire le signe + seulement si nécessaire!
  37. if bool then sol=""
  38. else sol="+"
  39. end
  40. endfunction
  41.  
  42.  
  43. function L=affichage(A,y)
  44. //affiche un système d'équations A*x=y
  45. // à p équations et n inconnues
  46. // sous forme de chaine de caractères
  47. // avec les variables x_1,x_2,...,x_n
  48. A=clean(A);
  49. y=clean(y);
  50. signe=sign(A);
  51. abso=abs(A);
  52. [p,n]=size(A);
  53. //conversion des coefficients en chaines de caractères
  54. //+justification à droite (tous les nombres ont la même "longueur")
  55. B=justify(string(A),'r');
  56. Y=justify(string(y),'r');
  57. // transformation du système matriciel (A,y)
  58. // en une liste L de p équations à n inconnues (x_1,x_2 ....x_n)
  59. // cas particulier de la variable x_1 (pas de "+" devant les nombres positifs)
  60. for i=1:p
  61.     if signe(i,1)==0 then L(i)=''
  62.                      else L(i)=un(B(i,1))+' x_1'
  63.     end
  64. end
  65. L=justify(L,'r');
  66. //traitement des variables x_2 jusqu'à x_n
  67. //si le coefficient de x_j=0 alors on ne met rien
  68. for j=2:n
  69.     K=string(zeros(p,1))
  70.     for i=1:p
  71.         K(i)=''
  72.         if signe(i,j)<>0 then
  73.                if signe(i,j)==-1 then K(i)='  '+un(B(i,j));
  74.                                  else bool=(sum(abso(i,1:j-1),'c')==0)|(signe(i,j)==-1);
  75.                                       K(i)=plus(bool)+un(B(i,j));
  76.                end
  77.         end
  78.     end
  79.     K=justify(K,'r');
  80.     for i=1:p
  81.         if signe(i,j)<>0 then K(i)=K(i)+' x_'+string(j);
  82.         end
  83.     end
  84.     L=L+justify(K,'r');
  85. end
  86. // second membre ....=y_i
  87. for i=1:p
  88.     if (sum(abso(i,:))==0) then L(i)='0';
  89.     end
  90.     L(i)=L(i)+'='+clean_complexe(Y(i))
  91. end
  92. L=justify(L,'r');
  93. endfunction
  94.  //******************************************************
  95. // système de 3 équations linéaires à 3 inconnues
  96. //******************************************************
  97.  
  98. A=[1 1 1; 1 -1 2; -1 2 1]
  99. y=[2;9;-2]
  100. affichage(A,y)
  101.  //**************************************************
  102. // Méthode de Gauss
  103. //**************************************************
  104. function [A,y]=permutation(A,y,i,l)
  105.     line_i = A(i, :)
  106.     A(i, :) = A(l, :)
  107.     A(l, :) = line_i
  108.    
  109.     y_i = y(i)
  110.     y(i) = y(l)
  111.     y(l)= y_i
  112. endfunction
  113.  
  114. function [A,y]=elimination(A,y,i,j)
  115.     [p,n]=size(A)
  116.     for k=i+1:p
  117.         y(k)= A(i,j)*y(k)-A(k,j)*y(i)
  118.         A(k,:)=A(i,j)*A(k,:)-A(k,j)*A(i,:)
  119.     end
  120. endfunction
  121.  
  122. function [A, y]=Gauss(A,y)
  123.     [p,n]=size(A)
  124.     i=1
  125.     j=1
  126.     while i<p && j<=n
  127.         l=i;
  128.         while A(l,j)==0
  129.             if l<p
  130.                 l=l+1
  131.             else
  132.                 if j<n
  133.                     j=j+1
  134.                     l=1
  135.                 else
  136.                    i=p
  137.                    j=n
  138.                 end
  139.             end
  140.         end
  141.         if i<>l
  142.             [A,y]=permutation(A,y,i,l)
  143.         end
  144.         [A,y]=elimination(A,y,i,j)
  145.         i=i+1
  146.         j=j+1
  147.         disp(affichage(A,y))
  148.     end
  149. endfunction
  150.  
  151. function x=solvetrisup(A,y)
  152.     //A matrice p*n triangulaire supérieure
  153.     //y vecteur colonne à p lignes
  154.     //x= solution de A*x=y ou [] sinon
  155. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement