Azzera filtri
Azzera filtri

Subplot within for loop is duplicating/overlaying graphs.

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 25 Mar 2022
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.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by