Subplot within for loop is duplicating/overlaying graphs.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Lewis
am 25 Mär. 2022
Kommentiert: Lewis
am 25 Mär. 2022
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 25 Mär. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Polynomials finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!