jackdharma1994

06/05_giunta

May 6th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 9.45 KB | None | 0 0
  1.  
  2.     This is a Classroom License for instructional use only.
  3.     Research and commercial use is prohibited.
  4. >> A = randi([-20,20], 5, 5);
  5. >> b = randi([-20,20],5,1);
  6. >> xM = A\b;
  7. >> r = A* xM - b
  8.  
  9. r =
  10.  
  11.    1.0e-13 *
  12.  
  13.     0.1421
  14.          0
  15.    -0.1421
  16.          0
  17.     0.0711
  18.  
  19. >> residuo = norm(r)
  20.  
  21. residuo =
  22.  
  23.    2.1316e-14
  24.  
  25. >> cond(A)
  26.  
  27. ans =
  28.  
  29.    36.8035
  30.  
  31. >> % lu
  32. >> [L, U, P] = lu(A)
  33.  
  34. L =
  35.  
  36.     1.0000         0         0         0         0
  37.     1.0000    1.0000         0         0         0
  38.     0.7647   -0.3613    1.0000         0         0
  39.    -0.8824   -0.2122   -0.8816    1.0000         0
  40.     0.2941    0.7731   -0.6118    0.3445    1.0000
  41.  
  42.  
  43. U =
  44.  
  45.    17.0000   -9.0000   19.0000   -3.0000  -19.0000
  46.          0   28.0000  -20.0000   15.0000   37.0000
  47.          0         0  -35.7563   -7.2857   33.8992
  48.          0         0         0   11.1130   34.9700
  49.          0         0         0         0   -7.3269
  50.  
  51.  
  52. P =
  53.  
  54.      0     1     0     0     0
  55.      0     0     0     1     0
  56.      1     0     0     0     0
  57.      0     0     1     0     0
  58.      0     0     0     0     1
  59.  
  60. >> % PA = LU
  61. >> norm(P*A-LU)
  62. Undefined function or variable 'LU'.
  63.  
  64. Did you mean:
  65. >> norm(P*A-L*U)
  66.  
  67. ans =
  68.  
  69.    5.0243e-15
  70.  
  71. >> % risolvere Ax=b usando L,U,P
  72. >> % PAx = Pb, LUx = Pb --> Ly= PB, Ux = y
  73. >> % PAx = Pb, LUx = Pb --> Ly= Pb, Ux = y
  74. >>
  75. >> y = L\(P*b)
  76.  
  77. y =
  78.  
  79.    10.0000
  80.    -4.0000
  81.     1.9076
  82.     5.6564
  83.   -13.6305
  84.  
  85. >> xlu = U\y
  86.  
  87. xlu =
  88.  
  89.    -0.2072
  90.     2.2619
  91.     2.7995
  92.    -5.3451
  93.     1.8603
  94.  
  95. >> [xM xlu]
  96.  
  97. ans =
  98.  
  99.    -0.2072   -0.2072
  100.     2.2619    2.2619
  101.     2.7995    2.7995
  102.    -5.3451   -5.3451
  103.     1.8603    1.8603
  104.  
  105. >> norm(xM-xlu)
  106.  
  107. ans =
  108.  
  109.      0
  110.  
  111. >> % sbagliato
  112. >> % ultima PARTE
  113. >> dA = det(A)
  114.  
  115. dA =
  116.  
  117.   -1.3858e+06
  118.  
  119. >> A
  120.  
  121. A =
  122.  
  123.     13   -17   -14   -15     6
  124.     17    -9    19    -3   -19
  125.    -15     2    19    17    14
  126.     17    19    -1    12    18
  127.      5    19    12    19     7
  128.  
  129. >> @
  130.  @
  131.   |
  132. Error: Expression or statement is incomplete or incorrect.
  133.  
  134. >> % PA = LU   A=inv(P)*L*U
  135. >> inv(P)
  136.  
  137. ans =
  138.  
  139.      0     0     1     0     0
  140.      1     0     0     0     0
  141.      0     0     0     1     0
  142.      0     1     0     0     0
  143.      0     0     0     0     1
  144.  
  145. >> P
  146.  
  147. P =
  148.  
  149.      0     1     0     0     0
  150.      0     0     0     1     0
  151.      1     0     0     0     0
  152.      0     0     1     0     0
  153.      0     0     0     0     1
  154.  
  155. >> inv(P) - P'
  156.  
  157. ans =
  158.  
  159.      0     0     0     0     0
  160.      0     0     0     0     0
  161.      0     0     0     0     0
  162.      0     0     0     0     0
  163.      0     0     0     0     0
  164.  
  165. >> % A = P' * L*U  --> det(A)  =  det(P')*det(L)*det(U)
  166. >> det(P')
  167.  
  168. ans =
  169.  
  170.     -1
  171.  
  172. >> L
  173.  
  174. L =
  175.  
  176.     1.0000         0         0         0         0
  177.     1.0000    1.0000         0         0         0
  178.     0.7647   -0.3613    1.0000         0         0
  179.    -0.8824   -0.2122   -0.8816    1.0000         0
  180.     0.2941    0.7731   -0.6118    0.3445    1.0000
  181.  
  182. >> -prod(diag(U))
  183.  
  184. ans =
  185.  
  186.   -1.3858e+06
  187.  
  188. >> % calcolo l'inversa di A come sistema multiplo
  189. >> % A * inv(A) = I       ---->  A*X=I
  190. >> miainv = A\eye(size(A))
  191.  
  192. miainv =
  193.  
  194.     0.0418    0.0097   -0.0158   -0.0137    0.0573
  195.    -0.1371    0.0268    0.0092    0.1464   -0.2047
  196.    -0.1109    0.0588    0.0564    0.1396   -0.2169
  197.     0.2116   -0.0714   -0.0580   -0.2679    0.4295
  198.    -0.0420    0.0135    0.0470    0.1003   -0.1365
  199.  
  200. >> inv(A)
  201.  
  202. ans =
  203.  
  204.     0.0418    0.0097   -0.0158   -0.0137    0.0573
  205.    -0.1371    0.0268    0.0092    0.1464   -0.2047
  206.    -0.1109    0.0588    0.0564    0.1396   -0.2169
  207.     0.2116   -0.0714   -0.0580   -0.2679    0.4295
  208.    -0.0420    0.0135    0.0470    0.1003   -0.1365
  209.  
  210. >> norm(inv(A)-miainv)
  211.  
  212. ans =
  213.  
  214.    6.6138e-17
  215.  
  216. >> eye(3,3)
  217.  
  218. ans =
  219.  
  220.      1     0     0
  221.      0     1     0
  222.      0     0     1
  223.  
  224. >> norm(miainv-inv(A))
  225.  
  226. ans =
  227.  
  228.    6.6138e-17
  229.  
  230. >> % calcolare l'inversa colonna per colonna usando
  231. >> % la fattorizzazione LUP
  232. >> Inva=zeros(size(A));
  233. >> id = eye(size(A);
  234. for j=1:size(A,2)
  235.            InvA(:,j) = A\id(:,j);
  236. end
  237.  id = eye(size(A);
  238.                  |
  239. Error: Unbalanced or unexpected parenthesis or bracket.
  240.  
  241. >> id = eye(size(A);
  242.  id = eye(size(A);
  243.                  |
  244. Error: Unbalanced or unexpected parenthesis or bracket.
  245.  
  246. >> id = eye(size(A));
  247. >> for j=1:size(A,2)
  248.            InvA(:,j) = A\id(:,j);
  249. end
  250. >> norm(inv(A)-InvA)
  251.  
  252. ans =
  253.  
  254.    6.6138e-17
  255.  
  256. >> id
  257.  
  258. id =
  259.  
  260.      1     0     0     0     0
  261.      0     1     0     0     0
  262.      0     0     1     0     0
  263.      0     0     0     1     0
  264.      0     0     0     0     1
  265.  
  266. >> id(:,0)
  267. Subscript indices must either be real positive integers or logicals.
  268.  
  269. >> id(:,2)
  270.  
  271. ans =
  272.  
  273.      0
  274.      1
  275.      0
  276.      0
  277.      0
  278.  
  279. >> % A casuale 2x2, generare 100 vettori sul cerchio unitario, trasformarli mediante A e .....
  280. >> A=rand(2,2); cond(A)
  281.  
  282. ans =
  283.  
  284.    24.2750
  285.  
  286. >> A
  287.  
  288. A =
  289.  
  290.     0.7060    0.2769
  291.     0.0318    0.0462
  292.  
  293. >> t = linspace(0,2*pi,200)
  294.  
  295. t =
  296.  
  297.   Columns 1 through 10
  298.  
  299.          0    0.0316    0.0631    0.0947    0.1263    0.1579    0.1894    0.2210    0.2526    0.2842
  300.  
  301.   Columns 11 through 20
  302.  
  303.     0.3157    0.3473    0.3789    0.4105    0.4420    0.4736    0.5052    0.5368    0.5683    0.5999
  304.  
  305.   Columns 21 through 30
  306.  
  307.     0.6315    0.6630    0.6946    0.7262    0.7578    0.7893    0.8209    0.8525    0.8841    0.9156
  308.  
  309.   Columns 31 through 40
  310.  
  311.     0.9472    0.9788    1.0104    1.0419    1.0735    1.1051    1.1367    1.1682    1.1998    1.2314
  312.  
  313.   Columns 41 through 50
  314.  
  315.     1.2630    1.2945    1.3261    1.3577    1.3892    1.4208    1.4524    1.4840    1.5155    1.5471
  316.  
  317.   Columns 51 through 60
  318.  
  319.     1.5787    1.6103    1.6418    1.6734    1.7050    1.7366    1.7681    1.7997    1.8313    1.8629
  320.  
  321.   Columns 61 through 70
  322.  
  323.     1.8944    1.9260    1.9576    1.9891    2.0207    2.0523    2.0839    2.1154    2.1470    2.1786
  324.  
  325.   Columns 71 through 80
  326.  
  327.     2.2102    2.2417    2.2733    2.3049    2.3365    2.3680    2.3996    2.4312    2.4628    2.4943
  328.  
  329.   Columns 81 through 90
  330.  
  331.     2.5259    2.5575    2.5891    2.6206    2.6522    2.6838    2.7153    2.7469    2.7785    2.8101
  332.  
  333.   Columns 91 through 100
  334.  
  335.     2.8416    2.8732    2.9048    2.9364    2.9679    2.9995    3.0311    3.0627    3.0942    3.1258
  336.  
  337.   Columns 101 through 110
  338.  
  339.     3.1574    3.1890    3.2205    3.2521    3.2837    3.3152    3.3468    3.3784    3.4100    3.4415
  340.  
  341.   Columns 111 through 120
  342.  
  343.     3.4731    3.5047    3.5363    3.5678    3.5994    3.6310    3.6626    3.6941    3.7257    3.7573
  344.  
  345.   Columns 121 through 130
  346.  
  347.     3.7889    3.8204    3.8520    3.8836    3.9152    3.9467    3.9783    4.0099    4.0414    4.0730
  348.  
  349.   Columns 131 through 140
  350.  
  351.     4.1046    4.1362    4.1677    4.1993    4.2309    4.2625    4.2940    4.3256    4.3572    4.3888
  352.  
  353.   Columns 141 through 150
  354.  
  355.     4.4203    4.4519    4.4835    4.5151    4.5466    4.5782    4.6098    4.6413    4.6729    4.7045
  356.  
  357.   Columns 151 through 160
  358.  
  359.     4.7361    4.7676    4.7992    4.8308    4.8624    4.8939    4.9255    4.9571    4.9887    5.0202
  360.  
  361.   Columns 161 through 170
  362.  
  363.     5.0518    5.0834    5.1150    5.1465    5.1781    5.2097    5.2413    5.2728    5.3044    5.3360
  364.  
  365.   Columns 171 through 180
  366.  
  367.     5.3675    5.3991    5.4307    5.4623    5.4938    5.5254    5.5570    5.5886    5.6201    5.6517
  368.  
  369.   Columns 181 through 190
  370.  
  371.     5.6833    5.7149    5.7464    5.7780    5.8096    5.8412    5.8727    5.9043    5.9359    5.9674
  372.  
  373.   Columns 191 through 200
  374.  
  375.     5.9990    6.0306    6.0622    6.0937    6.1253    6.1569    6.1885    6.2200    6.2516    6.2832
  376.  
  377. >> % A casuale 2x2, generare 200 vettori sul cerchio unitario, trasformarli mediante A e .....
  378. >> t = linspace(0,2*pi,200);
  379. >> x = cos(t); y = sin(t);
  380. >> hold on
  381. plot(x,y,'b'); axis equal;
  382. >> shg
  383. >> axis([-20 20 -20 20]);
  384. >> shg
  385. >> PCerchio = [x,y];
  386. >> PCerchio = [x;y];
  387. >> Ptransf = A*PCerchio;
  388. >> shg
  389. >> plot(Ptransf(1,:), Ptransf(2,:), 'r')
  390. >> plot(x,y,'b');
  391. >> hold on;
  392. >> plot(x,y,'b');
  393. >> plot(x,y,'b');
  394. >> plot(Ptransf(1,:), Ptransf(2,:), 'r')
  395. >> cond(A)
  396.  
  397. ans =
  398.  
  399.    24.2750
  400.  
  401. >> A
  402.  
  403. A =
  404.  
  405.     0.7060    0.2769
  406.     0.0318    0.0462
  407.  
  408. >> P=ginput(2)
  409.  
  410. P =
  411.  
  412.     0.3571    0.0497
  413.    -0.2143    0.0205
  414.  
  415. >> norm(P(1,:))/norm(P(2,:))
  416.  
  417. ans =
  418.  
  419.     1.6751
  420.  
  421. >> P=ginput(2)
  422. Error using ginput (line 84)
  423. Interrupted by figure deletion
  424.  
  425. >> plot(Ptransf(1,:), Ptransf(2,:), 'r')
  426. >> plot(x,y,'b');
  427. >> hold on
  428. >> plot(x,y,'b');
  429. >> plot(Ptransf(1,:), Ptransf(2,:), 'r')
  430. >> P=ginput(2)
  431.  
  432. P =
  433.  
  434.     0.7627    0.0497
  435.    -0.0207    0.0322
  436.  
  437. >> norm(P(1,:))/norm(P(2,:))
  438.  
  439. ans =
  440.  
  441.    19.9714
  442.  
  443. >> % costruire una matrice 5x5 singolare
  444. >> % consiglio: ultima colonna è comb delle prime 4
  445. >> A = -2+4*rand(5,4);
  446. >> A = [A A*rand(4,1)]
  447.  
  448. A =
  449.  
  450.    -1.6115   -1.8622   -1.2525    1.0187   -1.9751
  451.     1.2938   -0.2450   -0.0409   -0.8959   -0.3124
  452.     0.7793   -0.4738   -0.2177    0.7188   -0.1076
  453.    -0.7316    1.0621    0.5853    0.6204    1.2151
  454.     1.8009    1.1808    0.8375   -1.3496    1.1471
  455.  
  456. >> rank(A)
  457.  
  458. ans =
  459.  
  460.      4
  461.  
  462. >> inv(A)
  463. Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND =  1.635734e-17.
  464.  
  465. ans =
  466.  
  467.    1.0e+15 *
  468.  
  469.     0.2616    0.3779   -0.0057    0.4011    0.1279
  470.     1.0954    1.5829   -0.0237    1.6797    0.5356
  471.     2.1095    3.0482   -0.0457    3.2348    1.0315
  472.     0.7482    1.0811   -0.0162    1.1473    0.3659
  473.    -2.1980   -3.1761    0.0476   -3.3705   -1.0748
  474.  
  475. >> det(A)
  476.  
  477. ans =
  478.  
  479.    2.1596e-17
  480.  
  481. >> format long
  482. >> det(A)
  483.  
  484. ans =
  485.  
  486.      2.159627605372215e-17
  487.  
  488. >>
Advertisement
Add Comment
Please, Sign In to add comment