Advertisement
makispaiktis

Texnikes_Ergasia1_Thema1_Zitoumeno1

Nov 16th, 2020 (edited)
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.13 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % Πίνακας με τα διάφορα l
  5. l = 0.01;
  6. epsilon = [];
  7. counter = 0;
  8. for i = 0.1/1000 : 0.1/1000 : 4.9/1000
  9.     counter = counter + 1;
  10.     epsilon(counter) = i;
  11. end
  12.  
  13. counter2 = 0;
  14. calculationsList = [];
  15. for index = 1:length(epsilon)
  16.     counter2 = counter2 + 1;
  17.     calcs = numOfCalculations(l, epsilon(index));
  18.     calculationsList(counter2) = calcs;
  19. end
  20.  
  21. plot(calculationsList, 'o');
  22. title("f1 = (x-2)^2 - sin(x+3, l = 100/10000, epsilon = {1,..,49}/10000");
  23. xlabel("Value of epsilon / 10000");
  24. ylabel("Number of calculations needed");
  25.  
  26. function calculations = numOfCalculations(l, epsilon)
  27.     syms x
  28.     f1 = (x-2)^2 - sin(x+3);
  29.     akro1 = 2;  akro2 = 5;
  30.     % Πάντα πρέπει ε < l
  31.     counter = 0;
  32.     akra1 = [];    akra2 = [];      
  33.     calculations = 0;
  34.    
  35.     while (akro2 - akro1 > l)
  36.         counter = counter + 1;
  37.         akra1(counter) = akro1;
  38.         akra2(counter) = akro2;
  39.         % Βήμα 1
  40.         x1k = (akro1 + akro2) / 2 - epsilon;
  41.         x2k = (akro1 + akro2) / 2 + epsilon;
  42.         % Βήμα 2
  43.         value1 = subs(f1, x1k);
  44.         value2 = subs(f1, x2k);
  45.         calculations = calculations + 2;
  46.         if value1 < value2
  47.             % ακ+1 = ακ (δεν αλλάζει) και βκ+1 = χ2κ
  48.             akro2 = x2k;
  49.         else
  50.             % ακ+1 = χ1κ και βκ+1 = βκ (δεν αλλάζει)
  51.             akro1 = x1k;
  52.         end
  53.     end
  54.  
  55.     % Εδώ, έχουμε βρει το τελικό διάστημα οπού υπάρχει το x* = θέση ελαχίστου
  56.     % Πάντα μιλάμε για συγκεκριμένα l,ε που επιλέγω εγώ απαρχής.
  57.     counter = counter + 1;
  58.     akra1(counter) = akro1;
  59.     akra2(counter) = akro2;
  60.     % akra1
  61.     % akra2
  62.     display(' '); display(' '); display(' '); display(' ');
  63.     result = [];
  64.     result(1) = akra1(length(akra1));
  65.     result(2) = akra2(length(akra2));
  66.     display(strcat('x* between ', num2str(result(1)), ' and ', num2str(result(2))));
  67.     calculations
  68.     display('*******************************************************')
  69. end
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement