Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. clear;
  2. clc;
  3.  
  4. a=-2;
  5. b=-0.8;
  6. r=10;
  7. n_max=100;
  8. h=0.001;
  9. metoda=input("podaj numer metody 1/2/3 ")
  10.  
  11. select metoda
  12. case 1
  13. disp("wybrano metodę Newtona");
  14. case 2
  15. disp("wybrano metodę Steffensena");
  16. case 3
  17. disp("wybrano metodę Halleya");
  18. end
  19.  
  20. function y=f(x)
  21. y=x*exp(sin(x))+cos(x);
  22. endfunction
  23.  
  24. function y=fp(x,h)
  25. y=(f(x+h)-f(x))/h;
  26. endfunction
  27.  
  28. function y=fpp(x,h)
  29. y=(fp(x+h,h)-fp(x,h))/h;
  30. endfunction
  31.  
  32. function y=g(x)
  33. y=(f(x+f(x))-f(x))/f(x);
  34. endfunction
  35.  
  36. n=0;
  37. exit_code=0;
  38.  
  39. if(f(a)*f(b)<0 & fp(a,h)*fp(b,h)>0 & fpp(a,h)*fpp(b,h)>0) then
  40. if(f(a)*fpp(a,h)>0) then
  41. x(1)=a;
  42. n=1;
  43. else
  44. x(0)=b;
  45. n=1
  46. end
  47.  
  48. for n=2:n_max
  49. select metoda
  50. case 1 then //metoda Newtona
  51. x(n)=x(n-1)-f(x(n-1))/g(x(n-1));
  52. case 2 then //metoda Steffensena
  53. x(n)=x(n-1)-(f(x(n-1))/g(x(n-1)));
  54. case 3 then //metoda Halleya
  55. l=f(x(n-1))*fp(x(n-1),h);
  56. m1=(fp(x(n-1),h))^2;
  57. m2=f(x(n-1))*fpp(x(n-1),h)/2;
  58. x(n)=x(n-1)-l/(m1-m2)
  59. end
  60. err=abs(x(n)-x(n-1))*abs(f(x(n)))/abs(f(x(n))-f(n-1))
  61. if err<10^(-r)
  62. exit_code=3
  63. break;
  64. //while(n<n_max)
  65. //n=n+1;
  66. //x(n)=x(n-1)-(f(x(n-1))/g(x(n-1)));
  67. //if(((abs(x(n)-x(n-1))/abs(f(x(n))-f(x(n-1))))))<10^-r) then
  68. // exit_code=3;
  69. //break;
  70. //end
  71. //if(n>n_max)
  72. // exit_code=2
  73. //break;
  74. end
  75. end
  76. else
  77. exit_code=1;
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement