Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Computer lab 2 & 3
- % Johan Larsson
- % Max Niia
- % Function
- f = @(x) 54*x^6 + 45*x^5 - 102*x^4 - 69*x^3 + 35*x^2 + 16*x - 4;
- % Plotting
- figure
- fplot(f, [-2 2])
- axis([-2 2 -40 40])
- grid on
- % From the drawn graph we can estimate intervals which contains one root
- % [-1.5, -1.2], [-0.8, -0.5], [0.1, 0.3], [0.4, 0.65], [1.0, 1.3]
- % Calculating the roots and keeping track of number of
- % steps taken to calculate root.
- % Store roots in r(x) and steps in xi(x)
- format long
- [r1, xi1] = secant(-1.5, -1.2);
- [r2, xi2] = secant(-0.8, -0.5);
- [r3, xi3] = secant(0.1, 0.3);
- [r4, xi4] = secant(0.4, 0.65);
- [r5, xi5] = secant(1.0, 1.3);
- % To check if the roots are linear they have to satisfy the equation
- % f'(r) = 0
- l2 = vpa(secant_linearity(r2)) % f'(r) ~0
- % Calculating the estimated value of alpha to check whether it's
- % super linear or not (page 61)
- [a1, alphas1] = super_linearity(xi1, r1)
- [a2, alphas2] = super_linearity(xi2, r2)
- [a3, alphas3] = super_linearity(xi3, r3)
- [a4, alphas4] = super_linearity(xi4, r4)
- [a5, alphas5] = super_linearity(xi5, r5)
- % Roots 1, 3, 4 and 5 are superlinear since their alpha corresponds to
- % similar values of 1.62
- error1 = generate_errors(xi1, r1);
- error2 = generate_errors(xi2, r2);
- error3 = generate_errors(xi3, r3);
- error4 = generate_errors(xi4, r4);
- error5 = generate_errors(xi5, r5);
- % Drawing error2
- figure
- plot(error2);
- % Ignoring the first and the last few values since convergence might not
- % take hold immidiately.
- % The second root diverges at values
- % xi(1) - x(5), for values at indexes >5 it converges
- % at a rate of E_(i + 1) = 0.617*E_(i) which makes it linear
- % Verification that linearity holds
- factor = [];
- for i = 7:size(error2, 2) - 10
- factor(i - 5) = error2(i + 1)/error2(i);
- end
- c = 0;
- for i = 1:size(factor, 2)
- c = c + factor(i);
- end
- c = c/size(factor, 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement