Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.86 KB | None | 0 0
  1. % Computer lab 2 & 3
  2. % Johan Larsson
  3. % Max Niia
  4.  
  5. % Function
  6. f = @(x) 54*x^6 + 45*x^5 - 102*x^4 - 69*x^3 + 35*x^2 + 16*x - 4;
  7.  
  8. % Plotting
  9. figure
  10. fplot(f, [-2 2])
  11. axis([-2 2 -40 40])
  12. grid on
  13.  
  14. % From the drawn graph we can estimate intervals which contains one root
  15. % [-1.5, -1.2], [-0.8, -0.5], [0.1, 0.3], [0.4, 0.65], [1.0, 1.3]
  16.  
  17. % Calculating the roots and keeping track of number of
  18. % steps taken to calculate root.
  19. % Store roots in r(x) and steps in xi(x)
  20. format long
  21. [r1, xi1] = secant(-1.5, -1.2);
  22. [r2, xi2] = secant(-0.8, -0.5);
  23. [r3, xi3] = secant(0.1, 0.3);
  24. [r4, xi4] = secant(0.4, 0.65);
  25. [r5, xi5] = secant(1.0, 1.3);
  26.  
  27. % To check if the roots are linear they have to satisfy the equation
  28. % f'(r) = 0
  29. l2 = vpa(secant_linearity(r2)) % f'(r) ~0
  30.  
  31. % Calculating the estimated value of alpha to check whether it's
  32. % super linear or not (page 61)
  33. [a1, alphas1] = super_linearity(xi1, r1)
  34. [a2, alphas2] = super_linearity(xi2, r2)
  35. [a3, alphas3] = super_linearity(xi3, r3)
  36. [a4, alphas4] = super_linearity(xi4, r4)
  37. [a5, alphas5] = super_linearity(xi5, r5)
  38.  
  39. % Roots 1, 3, 4 and 5 are superlinear since their alpha corresponds to
  40. % similar values of 1.62
  41.  
  42. error1 = generate_errors(xi1, r1);
  43. error2 = generate_errors(xi2, r2);
  44. error3 = generate_errors(xi3, r3);
  45. error4 = generate_errors(xi4, r4);
  46. error5 = generate_errors(xi5, r5);
  47.  
  48. % Drawing error2
  49. figure
  50. plot(error2);
  51.  
  52. % Ignoring the first and the last few values since convergence might not
  53. % take hold immidiately.
  54. % The second root diverges at values
  55. % xi(1) - x(5), for values at indexes >5 it converges
  56. % at a rate of E_(i + 1) = 0.617*E_(i) which makes it linear
  57.  
  58. % Verification that linearity holds
  59. factor = [];
  60. for i = 7:size(error2, 2) - 10
  61.     factor(i - 5) = error2(i + 1)/error2(i);
  62. end
  63.  
  64. c = 0;
  65. for i = 1:size(factor, 2)
  66.     c = c + factor(i);
  67. end
  68. c = c/size(factor, 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement