Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. clear;
  2. clc;
  3.  
  4. // Instrukcja nr 4
  5. // krok 1
  6. a=-2;
  7. b=0.8;
  8. r=10;
  9. n_max=100;
  10. h=0.001;
  11.  
  12. // krok 2
  13.  
  14. function y=f(x);
  15. y=(x^2-3)*sin(x);
  16. endfunction;
  17.  
  18.  
  19. // krok 3
  20. function y=fp(x,h);
  21. y=(f(x+h)-f(x))/h;
  22. endfunction;
  23.  
  24. function y=fpp(x,h);
  25. y=(fp(x+h,h)-fp(x,h))/h;
  26. endfunction;
  27.  
  28. // krok 4
  29. n=1;
  30. x(1)=a;
  31.  
  32. if f(a)*f(b)<0 then
  33. while ~(fp(a,h)*fp(b,h)>0 & fpp(a,h)*fpp(b,h)>0);
  34. if n>n_max then
  35. n=n+1;
  36. x(n)=a+b/2;
  37. if abs (x(n)-x(n-1))<10^(-r) then
  38. exit_code=3;
  39. break;
  40. else
  41. if f(x(n))==0 then
  42. exit_code=4;
  43. break;
  44. else
  45. if f(a)*f(x(n))<0 then
  46. b=x(n);
  47. else
  48. a=x(n);
  49. end;
  50. end;
  51. end;
  52.  
  53. else
  54. exit_code=2
  55. end;
  56. end;
  57. else
  58. exit_code=1;
  59. end;
  60. //przejsice do rf
  61.  
  62. if exit_code==0 & n<n_max then
  63. n=n+1;
  64. if fp(a,h)*fpp(a,h)<0 then
  65. c=a;
  66. x(n)=b;
  67. else
  68. c=b;
  69. x(n)=a;
  70. end;
  71. while n<n_max
  72. n=n+1;
  73. x(n)=x(n-1)-f(x(n-1))*(c-x(n-1))/(f(c)-f(x(n-1)));
  74. if abs(x(n)-x(n-1))<10^(-r) then
  75. exit_code=3;
  76. break;
  77. end;
  78.  
  79. end;
  80. if exit_code==0 then
  81. exit_code=2;
  82.  
  83. end;
  84. else
  85. exit_code=2;
  86. end;
  87.  
  88. select exit_code
  89. case 1 then
  90. disp("niespełnione założenia metody połowienia");
  91. case 2 then
  92. disp("przekroczona maksymalna liczba iteracji= "+string(n_max));
  93. case 3 then
  94. disp("osiągnięta dokładność rzędu 10^("+string(-r)+")");
  95. disp("x("+string(n)+")="+string(x(n)));
  96. case 4 then
  97. disp("osiągnięto dokładną wartość pierwiastka");
  98. disp("x("+string(n)+")="+string(x(n)));
  99. else
  100. disp("nieznany bląd");
  101. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement