How to change color in Graph (jet)

25 vues (au cours des 30 derniers jours)
Niklas Kurz
Niklas Kurz le 17 Jan 2021
I copied and pasted the following code that describes a Fourier Series
% Define domain
dx = 0.001;
L = pi;
x = (-1+dx:dx:1)*L;
n = length(x); nquart = floor(n/4);
% Define function
f = x;
plot(x,f,'-k','LineWidth',1.5), hold on
% Compute Fourier series
CC = jet(20);
A0 = sum(f.*ones(size(x)))*dx;
fFS = A0/2;
for k=1:20
A(k) = sum(f.*cos(pi*k*x/L))*dx; % Inner product
B(k) = sum(f.*sin(pi*k*x/L))*dx;
fFS = fFS + A(k)*cos(k*pi*x/L) + B(k)*sin(k*pi*x/L);
plot(x,fFS,'-','Color',CC(k,:),'LineWidth',1.2)
end
legend('Function: f(x) = x',...
'Terms: N = 20','Interpreter','latex','Fontsize',16,...
'Location','northwest')
I noticed, that the color of "Terms" in legend is always blue (so k = 1). However, I want it to be the color of the kth Function, im my case k = 20.
How to achieve that? A common problem with coppied function.

Réponse acceptée

Walter Roberson
Walter Roberson le 17 Jan 2021
% Define domain
dx = 0.001;
L = pi;
x = (-1+dx:dx:1)*L;
n = length(x); nquart = floor(n/4);
% Define function
f = x;
h1 = plot(x,f,'-k','LineWidth',1.5);
hold on
% Compute Fourier series
CC = jet(20);
A0 = sum(f.*ones(size(x)))*dx;
fFS = A0/2;
for k=1:20
A(k) = sum(f.*cos(pi*k*x/L))*dx; % Inner product
B(k) = sum(f.*sin(pi*k*x/L))*dx;
fFS = fFS + A(k)*cos(k*pi*x/L) + B(k)*sin(k*pi*x/L);
hk = plot(x,fFS,'-','Color',CC(k,:),'LineWidth',1.2);
end
legend( [h1, hk], {'Function: f(x) = x',...
'Terms: N = 20'}, 'Interpreter', 'latex', 'Fontsize', 16,...
'Location', 'northwest')

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by