I want to piecewise plot a interlolation function. When I input the nodes like this
X = -1:0.1:1;N = length(X);
the graph is continuous like this
However When I input the nodes like this :
X = zeros(1,21);N = 21;for i = 1: 21X(i) = -1 + 2*(i-1)/(N-1);
there is an obvious gap in the graph like this :
I suppose these two codes are exactly the same, why the graphs are different??
the codes are as follows:
ticclcclearf = @(x)1./(1+25.*x.^2);syms x f_dri = diff(f(x));%X = -1:0.1:1;
%N = length(X);
X = zeros(1,21); N = 21; F = zeros(1, N); F_dri = zeros(1, N); for i = 1 : N X(i) = -1 + 2*(i-1)/(N-1); F(i) = f(X(i)); F_dri(i) = subs(f_dri, x, X(i)); end for j = 1 : N-1 M = zeros(4,4); M(1,1) = F(j); M(2,1) = F(j); M(3,1) = F(j+1); M(4,1) = F(j+1); M(2,2) = F_dri(j); M(3,2) = (F(j+1)-F(j))/(X(j+1)-X(j)); M(4,2) = F_dri(j+1); M(3,3) = (M(3,2)-M(2,2))/(X(j+1)-X(j)); M(4,3) = (M(4,2)-M(3,2))/(X(j+1)-X(j)); M(4,4) = (M(4,3)-M(3,3))/(X(j+1)-X(j)); f_inp = M(1,1)+(x-X(j))*M(2,2)+(x-X(j))^2*M(3,3)+(x-X(j+1))*(x-X(j))^2*M(4,4); a = X(j):0.01:X(j+1); b = subs(f_inp, x, a); G = plot(a, b, 'r', 'LineWidth', 2); hold on end saveas(gcf, 'pic_2', 'jpg'); % r = -1:0.01:1;
% f_ori = f(r);
% G(5) = plot(r, f_ori, 'k', 'LineWidth', 2);
legend(G,'N=21');toc
Best Answer