# How to change color in Graph (jet)

15 visualizzazioni (ultimi 30 giorni)
Niklas Kurz il 17 Gen 2021
Risposto: Walter Roberson il 17 Gen 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.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Walter Roberson il 17 Gen 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')
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su File Operations in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by