Why isn't my graph plotting correctly?

1 回表示 (過去 30 日間)
Monkeyman
Monkeyman 2019 年 11 月 12 日
コメント済み: Star Strider 2019 年 11 月 12 日
Hi, I think I must be being dense, I am attempting to plot in Matlab, but I am getting a plot that is very wrong.
km=linspace(0,10,100);
y=tan(km)+tanh(km);
plot(km,y,'color','red','LineWidth',1.5);
xlabel('$k_m$','Interpreter','latex');
ylabel('$f(x)$','Interpreter','latex');
title('Graph of $f(x)=tan(k_m)+tanh(k_m)$','Interpreter','latex')
Error.jpg
Please could someone explain what I have done wrong to obtain this:
Cheers
  3 件のコメント
Monkeyman
Monkeyman 2019 年 11 月 12 日
Error2.jpg
Adam
Adam 2019 年 11 月 12 日
I suspect the difference is simply that it is joining asymptotes together in the plot. If you explicitly insert NaNs in your data at the points of the asymptotes (or basically between the points either side of one) this should get rid of that, I think.

サインインしてコメントする。

回答 (1 件)

Star Strider
Star Strider 2019 年 11 月 12 日
Set the peaks values to NaN and they will not plot:
km=linspace(0,10,1000);
y=tan(km)+tanh(km);
[pks,locs] = findpeaks(y); % Locate Peaks Peaks
y(locs) = NaN; % Set Peaks To ‘NaN’
plot(km,y,'color','red','LineWidth',1.5);
xlabel('$k_m$','Interpreter','latex');
ylabel('$f(x)$','Interpreter','latex');
title('Graph of $f(x)=tan(k_m)+tanh(k_m)$','Interpreter','latex')
ylim([-6 +6])
grid
  2 件のコメント
Monkeyman
Monkeyman 2019 年 11 月 12 日
That's great, thank you. Is there a variation of the findpeaks that works for a function handle like below? I can't find one. I am trying to find the roots closest to certain values using x=fzero(f,km) but they are not compatible. Any help would be really appreciated!
f=@(km) tan(km)+tanh(km)
Star Strider
Star Strider 2019 年 11 月 12 日
To the best of my knowledge, findpeaks will not work on function handles. If you want to use fzero to find the zero-crossings, this is an appropriate approach:
km = linspace(0,10,100);
y = tan(km)+tanh(km);
[pks,locs] = findpeaks(y); % Locate Peaks Peaks
y(locs) = NaN; % Set Peaks To ‘NaN’
f=@(km) tan(km)+tanh(km);
midpt = fix(mean(diff(locs))/2);
for k = 1:numel(locs)
idx = min([numel(km) locs(k)+midpt]); % Prevents Indexing Beyond ‘km’ Length
zro(k) = fzero(f, km(idx));
end
It returns the zero-crossings in the ‘zro’ vector.
Experiment to get the result you want.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMeasurements and Feature Extraction についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by