smziaurrashid

Finding Roots of an Equation

Dec 25th, 2016
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.24 KB | None | 0 0
  1. clc;
  2. clear;
  3.  
  4. % make a function file eee18.m %
  5. % function[y]=eee18(x)
  6. y=x^3+2*x-5;
  7. end %
  8.  
  9. % make another function file eee19.m %
  10. % function[y]=eee19(x)
  11. y=3*x+2;
  12. end %
  13.  
  14. disp('© ET-153071™')
  15. disp('>> Choose a Method');
  16. disp('1. Bisection');
  17. disp('2. False-position');
  18. disp('3. Newton-Raphson');
  19. disp('4. All in One');
  20. answer=input('>> Choosen Option: ');
  21. if answer==1
  22. x1=input('- Enter the value of x1:');
  23. x2=input('- Enter the value of x2:');
  24. y1=eee18(x1);
  25. y2=eee18(x2);
  26.  if (y1*y2)<0
  27.     disp('Initial values are correct');
  28.          for n=1:1:100
  29.         y1=eee18(x1);    
  30.         y2=eee18(x2);
  31.         xm=(x1+x2)/2;
  32.         y0=eee18(xm);
  33.        
  34.           if y0==0
  35.               break
  36.           end
  37.           if y1*y0>0
  38.             x1=xm;            
  39.           else  x2=xm;
  40.            
  41.           end
  42.          end
  43.         M=xm;
  44.     fprintf('Bisection Root: %f\n', M);
  45.     else
  46.     disp('Initial values are incorrect');
  47.  end
  48.          
  49.  
  50. elseif answer==2
  51.    
  52. x1=input('- Enter the value of x1:');
  53. x2=input('- Enter the value of x2:');
  54. y1=eee18(x1);
  55. y2=eee18(x2);
  56. if (y1*y2)<0
  57.     disp('Initial values are correct');
  58.  
  59.      for n=1:1:10
  60.         y1=eee18(x1);
  61.         y2=eee18(x2);
  62.         xr=x1-(y1*(x2-x1))/(y2-y1);
  63.         y0=eee18(xr);
  64.    if y0==0
  65.    break
  66.    elseif abs(y0)<0.00001
  67.        break
  68.    elseif y1*y0>0;
  69.             x2=xr;
  70.          
  71.    else
  72.        x1=xr;
  73.    end
  74.      end
  75.     N=xr;
  76.     fprintf('False Position Root: %f\n', N);
  77.     else
  78.     disp('Initial values are incorrect');
  79. end
  80.    
  81. elseif answer==3
  82. x1=input('Enter the value of x1:');
  83. y1=eee18(x1);
  84. y2=eee19(x1);
  85. if (y1*y2)<0
  86.     disp('Initial values are correct')
  87.      for n=1:1:100
  88.         y1=eee18(x1);
  89.         y2=eee19(x1);
  90.         xq=x1-(y1/y2);
  91.         y0=eee18(xq);
  92.        
  93.         if y0==0
  94.         break
  95.         end
  96.         x1=xq;
  97.      end
  98.     P=xq;
  99.     fprintf('Newton Root: %f\n', P);
  100.     else
  101.     disp('Initial values are incorrect');
  102. end
  103.  
  104. elseif answer==4
  105. x1=input('- Enter the value of x1:');
  106. x2=input('- Enter the value of x2:');
  107. y1=eee18(x1);
  108. y2=eee18(x2);
  109.  if (y1*y2)<0
  110.     disp('Initial values are correct');
  111.    
  112.     for n=1:1:100
  113.         y1=eee18(x1);    
  114.         y2=eee18(x2);
  115.         xm=(x1+x2)/2;
  116.         y0=eee18(xm);
  117.        
  118.           if y0==0
  119.               break
  120.           end
  121.           if y1*y0>0
  122.             x1=xm;            
  123.           else  x2=xm;
  124.            
  125.           end
  126.     end
  127.         M=xm;
  128.    
  129.         for n=1:1:10
  130.         y1=eee18(x1);
  131.         y2=eee18(x2);
  132.         xr=x1-(y1*(x2-x1))/(y2-y1);
  133.         y0=eee18(xr);
  134.    if y0==0
  135.    break
  136.    elseif abs(y0)<0.00001
  137.        break
  138.    elseif y1*y0>0;
  139.             x2=xr;
  140.          
  141.    else
  142.        x1=xr;
  143.    end
  144.        end
  145.     N=xr;
  146.        
  147.     for n=1:1:100
  148.         y1=eee18(x1);
  149.         y2=eee19(x1);
  150.         xq=x1-(y1/y2);
  151.         y0=eee18(xq);
  152.        
  153.         if y0==0
  154.         break
  155.         end
  156.         x1=xq;
  157.      end
  158.     P=xq;
  159.     Q=[M,N,P];
  160.     disp('Bisection False-Position Newton-Raphson')
  161.     disp('---------------------------------------')
  162.     disp(Q)
  163.    
  164.  else
  165.      disp('Initial values are incorrect');
  166.  end
  167.    
  168.  
  169. else
  170.     disp('Please, choose any option!');
  171. end
Advertisement