Guest User

Untitled

a guest
Jan 23rd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.27 KB | None | 0 0
  1.  
  2. % Bisection method start!
  3. %   initial guess
  4. x1 = 4;
  5. x2 = 20;
  6.  
  7. % Bisection
  8. ERROR_ALLOWED = 0;                                          % 可接受的誤差
  9.  
  10. answer = 0;                                                    % 最終答案
  11. it_count = 0;                                               % 記錄迴圈次數
  12. while true
  13.     if(it_count ~= 0)
  14.         last_approx = temp_x;                               %將上一個近似儲存進來
  15.     end
  16.     temp_x = (x1+x2)/2;                                     % 二分之精隨
  17.     % 如果Ea小於0.01%,也就是0.0001  就可跳出!
  18.     if(it_count ~= 0 )
  19.         Ea = abs((temp_x-last_approx)/temp_x);
  20.         it_array(it_count) = it_count;
  21.         Ea_array(it_count) = Ea;        
  22.         if(Ea  < 0.0001)  
  23.             answer = temp_x                                     % 儲存最終答案並印出
  24.             disp(it_count)                                      % 印出迴圈次數
  25.             break;                                              % 跳出
  26.         end
  27.     end
  28.     if (testfunc(temp_x) * testfunc(x1) < 0)                % 判斷新的值應為下次進行二分法的左方還是右方
  29.         x2 = temp_x;                                        %
  30.     elseif (testfunc(temp_x) * testfunc(x2) < 0)            %     本段都是做上面的事情唷~ =w=
  31.         x1 = temp_x;                                        %
  32.     end
  33.     it_count = it_count + 1;                                % 迴圈次數+1  
  34. end
  35.  
  36. plot(it_array , Ea_array , 'k:diamond');                  %繪圖      
  37.  
  38.  
  39. % false position method start!
  40. %   initial guess
  41. x1 = 4;
  42. x2 = 20;
  43.  
  44. % Bisection
  45. ERROR_ALLOWED = 0;                                          % 可接受的誤差
  46.  
  47. answer = 0;                                                    % 最終答案
  48. it_count = 0;                                               % 記錄迴圈次數
  49. while true
  50.     if(it_count ~= 0)
  51.         last_approx = temp_x;                               %將上一個近似儲存進來
  52.     end
  53.     % falseposition的直線求解
  54.     temp_x = x2 - (testfunc(x2) * (x2-x1) / (testfunc(x2)-testfunc(x1)));                                    
  55.     hold on;
  56.     % 如果Ea小於0.01%,也就是0.0001  就可跳出!
  57.     if(it_count ~= 0 )
  58.         Ea = abs((temp_x-last_approx)/temp_x);
  59.         it_array(it_count) = it_count;
  60.         Ea_array(it_count) = Ea;        
  61.         if(Ea  < 0.0001)  
  62.             answer = temp_x                                     % 儲存最終答案並印出
  63.             disp(it_count)                                      % 印出迴圈次數
  64.             break;                                              % 跳出
  65.         end
  66.     end
  67.     if (testfunc(temp_x) * testfunc(x1) < 0)                % 判斷新的值應為下次進行二分法的左方還是右方
  68.         x2 = temp_x;                                        %
  69.     elseif (testfunc(temp_x) * testfunc(x2) < 0)            %     本段都是做上面的事情唷~ =w=
  70.         x1 = temp_x;                                        %
  71.     end
  72.     it_count = it_count + 1;                                % 迴圈次數+1  
  73. end
  74. plot(it_array , Ea_array , 'b:O');                          %繪圖
  75. legend('Bisection' , 'False-position')
  76.  
  77. xlabel('Iteration steps');
  78. ylabel('Ea');
Add Comment
Please, Sign In to add comment