Eddie_1337

p1L9 - CN

Nov 28th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. clc
  2. clear
  3. rel = 1;
  4. while(rel)
  5. EPS = input('EPS = ');
  6. rel = 1;
  7. while(rel)
  8. while(rel)
  9. fprintf('Pentru [xi, xf], xi < xf:\n');
  10. xi = input('xi = ');
  11. xf = input('xf = ');
  12. while(xi >= xf)
  13. fprintf('Eroare. Reintroduceti valorile:');
  14. xi = input('xi = ');
  15. xf = input('xf = ');
  16. end
  17. he = input('he > 0, xi + he <= xf:\nhe = ');
  18. while(he <= 0 && xi + he <= xf)
  19. fprintf('Eroare, reintroduceti variabila he:\n');
  20. he = input('he = ');
  21. end
  22. xe = input('xe = ');
  23. ye = input('ye = ');
  24. xe = xi : he : xf;
  25. np = length(xe);
  26. if(xe(np) < xf)
  27. xe = [xe xf];
  28. np = np + 1;
  29. end
  30. ye = FCE(xe);
  31. plot(xe, ye);
  32. grid;
  33. rel = input('Pentru reluare introduceti 1, sau 0 pentru a continua: ');
  34. end
  35. rel = 1;
  36. while(rel)
  37. x0 = input('x0 = ');
  38. x1 = input('x1 = ');
  39. x2 = input('x2 = ');
  40. f0 = FCE(x0);
  41. f1 = FCE(x1);
  42. f2 = FCE(x2);
  43. iter = 0;
  44. xr = x2;
  45. fr = f2;
  46. while(abs(fr) > EPS)
  47. iter = iter + 1;
  48. if(x2 > x0)
  49. t = x2;
  50. x2 = x0;
  51. x0 = t;
  52. t = f2;
  53. f2 = f0;
  54. f0 = t;
  55. end
  56. if(x0 > x1)
  57. t = x0;
  58. x0 = x1;
  59. x1 = t;
  60. t = f0;
  61. f0 = f1;
  62. f1 = t;
  63. end
  64. if(x2 > x1)
  65. t = x2;
  66. x2 = x1;
  67. x1 = t;
  68. t = f2;
  69. f2 = f1;
  70. f1 = t;
  71. end
  72. h1 = x1 - x0;
  73. if(abs(h1) < EPS)
  74. fprintf('h1 - nul: algoritmul nu converge');
  75. return;
  76. end
  77. h2 = x0 - x2;
  78. r = h2 / h1;
  79. d = r * (r + 1) * (h1^2);
  80. if(abs(d) < EPS)
  81. fprintf('numitor nul pentru a');
  82. return;
  83. end
  84. a = (r * f1 - f0 * (1 + r) + f2)/d;
  85. b = (f1 - f0 - a * (h1^2))/h1;
  86. c = f0;
  87. delta = b^2 - 4 * a * c;
  88. if(delta <= 0)
  89. fprintf('radacini complexe sau confundate');
  90. return;
  91. end
  92. if(b >= 0)
  93. d = b + sqrt(delta);
  94. else
  95. d = b - sqrt(delta);
  96. end
  97. if(abs(d) < EPS)
  98. fprintf('numitor nul pentru xr');
  99. return;
  100. end
  101. xr = x0 - (2 * c) / d;
  102. fr = FCE(xr);
  103. if(xr > x0)
  104. x1 = xr;
  105. f1 = fr;
  106. else
  107. x2 = xr;
  108. f2 = fr;
  109. end
  110. iter
  111. xr
  112. fr
  113. end
  114. fprintf('numar total iteratii = %g', iter);
  115. fprintf('solutie xr = %g', xr);
  116. fprintf('valoare functie fr = %g', fr);
  117. rel = input('Pentru reluare de la introducerea valorilor x0, x1, x2, introduceti 1 sau 0 pentru continuare: ');
  118. end
  119. rel = input('Pentru reluare de la introducerea valorilor xi si xf introduceti 1 sau 0 pentru continuare: ');
  120. end
  121. rel = input('Introduceti 1 pentru reluare generala program sau 0 pentru inchidere');
  122. end
Advertisement
Add Comment
Please, Sign In to add comment