Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x = linspace(-5, 5, 11)
- x=x';
- y = [-5.8643; -6.7445; -5.2378; -3.2868; -2.2393;...
- -0.5084; -1.2237; -0.7893; -4.8761; -11.0466; -20.0868]
- %Ax=b
- [A1, b1] = matrixer(x, y, 1);
- [A2, b2] = matrixer(x, y, 2);
- [A3, b3] = matrixer(x, y, 3);
- [A4, b4] = matrixer(x, y, 4);
- [A5, b5] = matrixer(x, y, 5);
- [A6, b6] = matrixer(x, y, 6);
- [A7, b7] = matrixer(x, y, 7);
- [A8, b8] = matrixer(x, y, 8);
- [A9, b9] = matrixer(x, y, 9);
- [A10, b10] = matrixer(x, y, 10);
- % without QR factorization
- coefficients1=flipud(inv(A1'*A1)*(A1'*b1));
- coefficients2=flipud(inv(A2'*A2)*(A2'*b2));
- coefficients3=flipud(inv(A3'*A3)*(A3'*b3));
- coefficients4=flipud(inv(A4'*A4)*(A4'*b4));
- coefficients5=flipud(inv(A5'*A5)*(A5'*b5));
- coefficients6=flipud(inv(A6'*A6)*(A6'*b6));
- coefficients7=flipud(inv(A7'*A7)*(A7'*b7));
- coefficients8=flipud(inv(A8'*A8)*(A8'*b8));
- coefficients9=flipud(inv(A9'*A9)*(A9'*b9));
- coefficients10=flipud(inv(A10'*A10)*(A10'*b10));
- xs = linspace(-5, 5);
- ys1 = polyval(coefficients1, xs);
- ys2 = polyval(coefficients2, xs);
- ys3 = polyval(coefficients3, xs);
- ys4 = polyval(coefficients4, xs);
- ys5 = polyval(coefficients5, xs);
- ys6 = polyval(coefficients6, xs);
- ys7 = polyval(coefficients7, xs);
- ys8 = polyval(coefficients8, xs);
- ys9 = polyval(coefficients9, xs);
- ys10 = polyval(coefficients10, xs);
- figure
- plot(x,y,'o')
- hold on
- grid on
- plot(xs,ys1)
- plot(xs,ys2)
- plot(xs,ys3)
- plot(xs,ys4)
- plot(xs,ys5)
- legend('exact points', 'n=1', 'n=2', 'n=3', 'n=4', 'n=5');
- legend('Location','best')
- title('Interpolating without QR factorization (1-5)')
- ylim([-15,2])
- hold off
- figure
- plot (x,y,'o')
- hold on
- grid on
- plot(xs,ys6)
- plot(xs,ys7)
- plot(xs,ys8)
- plot(xs,ys9)
- plot(xs,ys10)
- legend('exact points', 'n=6', 'n=7', 'n=8', 'n=9', 'n=10');
- legend('Location','best')
- title('Interpolating without QR factorization (6-10)')
- ylim([-15,2])
- hold off
- % calculating errors
- residuum1 = b1 - A1*coefficients1;
- residuum2 = b2 - A2*coefficients2;
- residuum3 = b3 - A3*coefficients3;
- residuum4 = b4 - A4*coefficients4;
- residuum5 = b5 - A5*coefficients5;
- residuum6 = b6 - A6*coefficients6;
- residuum7 = b7 - A7*coefficients7;
- residuum8 = b8 - A8*coefficients8;
- residuum9 = b9 - A9*coefficients9;
- residuum10 = b10 - A10*coefficients10;
- error1 = norm(residuum1);
- error2 = norm(residuum2);
- error3 = norm(residuum3);
- error4 = norm(residuum4);
- error5 = norm(residuum5);
- error6 = norm(residuum6);
- error7 = norm(residuum7);
- error8 = norm(residuum8);
- error9 = norm(residuum9);
- error10 = norm(residuum10);
- order2 =[6, 7, 8, 9, 10];
- order1 = [1, 2, 3, 4, 5];
- errors1 = [error1, error2, error3, error4, error5];
- errors2 = [error6, error7, error8, error9, error10]
- figure
- plot(order1,errors1, '--o')
- legend('Errors depending on order of polynomial')
- title('Errors without QR factorization (1-5)')
- figure
- plot(order2,errors2, '--o')
- legend('Errors depending on order of polynomial')
- title('Errors without QR factorization (6-10)')
- beep;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement