Advertisement
shadrin121

Sturm/secats/ewton/consecutel_improve

Jan 21st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. %Sturm's method
  2.  
  3. clc; clear; close all;
  4.  
  5. f = @(x) x.^3 - 5*x + 4;
  6. df = @(x) 3*x.^2 - 5;
  7. x = -4:0.01:4;
  8.  
  9. F = [1, 0, -5, 4];
  10. DF = [3, 0, -5];
  11.  
  12. L = length(F);
  13.  
  14. figure(1)
  15. plot(x,f(x)); hold on; grid on;
  16. plot(roots(F),f(roots(F)),'ro'); hold off;
  17.  
  18. a=0;
  19. b=4;
  20.  
  21. c_a = 0; %counter_a
  22. c_b = 0; %counter b
  23.  
  24. ax_1 = f(a);
  25. bx_1 = f(b);
  26.  
  27. ax_2 = df(a);
  28. bx_2 = df(b);
  29.  
  30. if ax_1*ax_2 < 0
  31. c_a = c_a + 1;
  32. end
  33.  
  34. if bx_1*bx_2 < 0
  35. c_b = c_b + 1;
  36. end
  37.  
  38. ax_1 = ax_2;
  39. bx_1 = bx_2;
  40.  
  41. for n=1:(L-2)
  42.  
  43. [q,r] = deconv(F, DF)
  44. L_x = length(F)-length(DF);
  45. r2 = -1.*r(end-L_x:end);
  46. ax_2 = polyval(r2, a);
  47. bx_2 = polyval(r2, b);
  48.  
  49. if ax_1*ax_2 < 0
  50. c_a = c_a + 1;
  51. end
  52.  
  53. if bx_1*bx_2 < 0
  54. c_b = c_b + 1;
  55. end
  56.  
  57. ax_1 = ax_2;
  58. bx_1 = bx_2;
  59.  
  60. F = DF;
  61. DF = r2;
  62.  
  63. end
  64.  
  65. How_many_roots = c_a - c_b
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. ````````````````````````````````
  89. %secant method 1
  90.  
  91. clc; clear; close all;
  92.  
  93. dx = 0.01;
  94. x = -10:dx:10;
  95.  
  96. f = @(x) x.^2-4;
  97. y=f(x);
  98. k = 100; %number of repetitions
  99. q = 5; %starting position
  100. a = 1; %frst limit
  101. b = 5; %second limit
  102.  
  103.  
  104. for n=1:k
  105. if f(q) == 0
  106. display("yep, that's the one")
  107. break
  108. elseif (f(a)*f(q))<0
  109. b = q;
  110. q = a - ((f(a)*(b-a))/(f(b)-f(a)));
  111. else
  112. a = q;
  113. q = b - ((f(b)*(b-a))/(f(b)-f(a)));
  114. end
  115.  
  116. if abs(0-f(q))<0.000000001
  117. q
  118. break
  119. end
  120. end
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. ``````````````````````````````````````````
  145.  
  146. %secant method 2
  147.  
  148. clc; clear; close all;
  149.  
  150. dx = 0.01;
  151. x = -10:dx:10;
  152.  
  153. f = @(x) x.^2-4;
  154.  
  155. k = 100; %number of repetitions
  156. a = 1; %frst limit
  157. b = 5; %second limit
  158.  
  159. gate = 0;
  160.  
  161. %coefficients=polyfit([0,1],[0,2],1)
  162. %roots(coefficients)
  163. for n=1:k
  164. cf = polyfit([a,b], [f(a),f(b)], 1);
  165. x0 = roots(cf);
  166. if gate == 0
  167. cf2 = polyfit([x0, b], [f(x0),f(b)], 1);
  168. a = roots(cf2);
  169. gate = 1;
  170. fin = a;
  171. else
  172. cf2 = polyfit([a, x0], [f(a),f(x0)], 1);
  173. b = roots(cf2);
  174. gate = 0;
  175. fin = b;
  176. end
  177. if abs(0 - f(fin))<0.0000000001
  178. fin
  179. break
  180. end
  181. end
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. `````````````````````````````````````````````````````````````
  193. %secant method 3
  194.  
  195. clc; clear; close all;
  196.  
  197. dx = 0.01;
  198. x = -100000:dx:100000;
  199.  
  200. f = @(x) x.^3-11*x.^2+14*x+80;
  201.  
  202. k = 1; %number of repetitions
  203. a = -6; %frst limit
  204. b = 10; %second limit
  205.  
  206. c = (a+b)/2
  207.  
  208. for n=1:k
  209.  
  210. f_ex = (f(c)+(sign(f(b))*sqrt((f(c)^2)-(f(a)*f(b)))))/f(b)
  211. xa = f(a)
  212. xb = f(b)*(f_ex^2)
  213. xc = f(c)*f_ex
  214. x1 = xc + (xc - xa)*(f(xc)*sign(f(xa)-f(xb)))/nthroot((f(xc)^2)-(f(xa)*f(xb)),2)
  215. b = x1;
  216. c = (a+b)/2;
  217. if abs(0-f(b))<0.0000001
  218. b
  219. break
  220. end
  221. b
  222. end
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. ``````````````````````````````````````````
  245. %Newton method
  246.  
  247. clc; clear; close all;
  248.  
  249. dx = 0.01;
  250. x = -10:dx:10;
  251. x2 = 1:dx:10;
  252.  
  253. f = @(x) x.^2 - 4;
  254. df1 = @(x) 2*x;
  255. df2 = @(x) 2+0*x;
  256.  
  257. k = 50; %number of repetitions
  258. a = 1; %frst limit
  259. b = 10; %second limit
  260.  
  261. for n=1:k
  262. if f(a)*df2(a) > 0
  263. x1 = a - (f(a)/df1(a));
  264. a = x1;
  265. else
  266. x1 = b - (f(b)/df1(b));
  267. b = x1;
  268. end
  269.  
  270. if abs(0-f(x1))<0.000000000001
  271. x1;
  272. break
  273. end
  274. end
  275.  
  276. figure(1)
  277. plot(x2,f(x2)); hold on; grid on;
  278. plot(x1,f(x1),'ro'); hold off;
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306. ````````````````````````````````
  307. %consecutively improved approximation
  308.  
  309. clc; clear; close all;
  310.  
  311. %f = exp(x) - (1/x)
  312. f2 = @(x) x - (1/4)*(exp(x)-(1/x))
  313. k = 1/4
  314.  
  315. x=3/4;
  316.  
  317. A=zeros(1,20);
  318. n=length(A);
  319.  
  320. for k=1:n
  321. A(k) = f2(x);
  322. x=f2(x)
  323. end;
  324.  
  325.  
  326.  
  327. figure(1);
  328. plot(A)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement