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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/940689/image.jpeg)
Thanks
댓글 수: 0
채택된 답변
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 Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!