How to change color in Graph (jet)

조회 수: 22 (최근 30일)
Niklas Kurz
Niklas Kurz 2021년 1월 17일
답변: Walter Roberson 2021년 1월 17일
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.

채택된 답변

Walter Roberson
Walter Roberson 2021년 1월 17일
% 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개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by