フィルターのクリア

How to change color in Graph (jet)

13 ビュー (過去 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 Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by