Subplot within for loop is duplicating/overlaying graphs.

1 次查看(过去 30 天)
The problem I am having is that the second plot seems to be overlaying the first one as well.
clear
syms x
f(x)=1/cosh(x)
N=[4,14] %defines interpolating polynomial degree
for k=1:length(N) %iterates over the values in array N
for i=0:N(k) %iterates over each point
xcheb(i+1)=-0.5*cos((((2*i)+1)/(((2*N(k)))+2)*pi)); %creates array of Chebyshev points
fvals(i+1)=1/cosh(xcheb(i+1)); %creates array of values of f(x) at each Chebyshev point
end
temp=1; %initialises temporary variable used in the Lagrange method
poly(k)=0; %initialises poly; the integrating polynomial
for i=1:N(k)+1
for j=1:N(k)+1
if i~=j
temp=temp*(x-xcheb(j))/((xcheb(i)-xcheb(j)));
end
end
poly=poly+(fvals(i)*temp);
temp=1;
end
subplot(3,1,k)
fplot(poly,[-0.5,0.5],'r')
end
subplot(3,1,3)
fplot(f,[-0.5,0.5],'g')
Thanks

采纳的回答

Walter Roberson
Walter Roberson 2022-3-25
poly(k)=0; %initialises poly; the integrating polynomial
That makes poly a vector . On the second iteration of k, poly is going to have length 2.
poly=poly+(fvals(i)*temp);
Vector plus scalar equals vector, so the left side is going to be a vector.
fplot(poly,[-0.5,0.5],'r')
All elements of the vector of symbolic experssions are plotted.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by