Advertisement
BORUTO-121

linear_system_solver(12)

Mar 6th, 2022
164
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sistem();
  2. function [solution]=sistem()
  3. N=input("Unesite broj jednacina sistema: ");
  4. A=zeros(N);
  5.  
  6. fprintf("Unesite matricu sistema:\n");
  7.  
  8. for i=1:N
  9.     for j=1:N
  10.         fprintf("Unesite koeficijente na mjestu (%d,%d): ",i,j);
  11.         A(i,j)=input("");
  12.     end
  13. end
  14.  
  15. fprintf("Unesite matricu jednakosti:\n");
  16. B=zeros(N,1);
  17.  
  18. for i=1:N
  19.     fprintf("Unesite element na mjestu (%d,%d): ",i,1);
  20.     B(i,1)=input("");
  21. end
  22.  
  23. solution=A\B;
  24.  
  25. if(N==2)
  26.     x=solution(1)-5:0.01:solution(1)+5;
  27.     y1=(B(1,1)-A(1,1)*x)/A(1,2);
  28.     y2=(B(2,1)-A(2,1)*x)/A(2,2);
  29.     plot(x,y1,'r');
  30.     hold on;
  31.     grid on;
  32.     plot(x,y2,'b');
  33.     plot(solution(1),solution(2),'o');
  34. elseif(N==3)
  35.     X=solution(1)-5:0.01:solution(1)+5;
  36.     Y=solution(2)-5:0.01:solution(2)+5;
  37.     [x,y]=meshgrid(X,Y);
  38.     z1=(B(1,1)-A(1,1)*x-A(1,2)*y)/A(1,3);
  39.     z2=(B(2,1)-A(2,1)*x-A(2,2)*y)/A(2,3);
  40.     z3=(B(3,1)-A(3,1)*x-A(3,2)*y)/A(3,3);
  41.     mesh(x,y,z1);
  42.     hold on;
  43.     mesh(x,y,z2);
  44.     mesh(x,y,z3);
  45.     plot3(solution(1),solution(2),solution(3),'o');
  46. end
  47.  
  48. end
Advertisement
RAW Paste Data Copied
Advertisement