📄 demo_pol_bases.m
字号:
%% Polynomial Interpolation%x = [ 0; 1; -1; 2; -2];f = [ -5; -3; -15; 39; -9];n = size(x(:),1);z = (-2:0.01:2)';m = size(z,1);disp(' Display the polynomial bases used in interpolation ')disp(' with nodes ')disp(x')disp(' Hit return to continue')pause% MonomialsM = ones(m,n);for i = 1:n M(:,i) = z.^(i-1);end plot( z, M(:,1), '-'); hold onplot( z, M(:,2), ':'); hold onplot( z, M(:,3), '-.'); hold onplot( z, M(:,4), '--'); hold onplot( z, M(:,5), '-'); hold offxlabel('x')title('Monomial Basis')legend('M_1', 'M_2', 'M_3', 'M_4', 'M_5', -1)%print -deps monomials.epsdisp(' Hit return to continue')pause% Lagrange polynomialsL = ones(m,n);for i = 1:n for j = 1:n if( i ~= j ) L(:,i) = L(:,i).*(z-x(j))./(x(i)-x(j)); end endend plot( z, L(:,1), '-'); hold onplot( z, L(:,2), ':'); hold onplot( z, L(:,3), ':'); hold onplot( z, L(:,4), '--'); hold onplot( z, L(:,5), '--'); hold offxlabel('x')legend('L_1', 'L_2', 'L_3', 'L_4', 'L_5', -1)title('Lagrange Basis')legend('M_1', 'M_2', 'M_3', 'M_4', 'M_5', -1)%print -deps lag-pol.epsdisp(' Hit return to continue')pause% Newton polynomialsN = ones(m,n);for i = 1:n for j = 1:i-1 N(:,i) = N(:,i).*(z-x(j)); endend plot( z, N(:,1), '-'); hold onplot( z, N(:,2), ':'); hold onplot( z, N(:,3), '-.'); hold onplot( z, N(:,4), '-'); hold onplot( z, N(:,5), '--'); hold offxlabel('x')legend('N_1', 'N_2', 'N_3', 'N_4', 'N_5', -1)title('Newton Basis')%print -deps newt-pol.epsdisp(' Hit return to continue')pauseplot( z, L*f); hold onplot( x, f, '*'); hold offxlabel('x')title('Interpolation Polynomial')%print -deps int-pol.eps
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -