Plotting the difference of two functions and finding the roots - octave

1 回表示 (過去 30 日間)
Candice Edwards
Candice Edwards 2018 年 9 月 3 日
コメント済み: Rik 2018 年 9 月 6 日
I have the two functions, m1(t)=sin(2πt+0.4π)/2+0.03 and m2(t)=sin(2πt)cos(2πt). I need to plot the difference of these two functions over the interval [0,1] and use the fzero command to find the roots. Any help solving this specifically using octave would be greatly appreciated.
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 3 日
Not a question about MATLAB. We do not attempt to track the differences between MATLAB and octave. If you need something specific to octave then you need to use an octave resource not here.

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

回答 (1 件)

Rik
Rik 2018 年 9 月 3 日
編集済み: Rik 2018 年 9 月 3 日
You can just define a new anonymous function and use that as an input to fzero, just like you would in Matlab. Octave fzero doc.
The code below uses fzero to find all zero-crossings, although it might fail due to the numerical approach. To my knowledge this is not different between Matlab and Octave. (and also, as Walter noted, you shouldn't really post questions about Octave on a Matlab forum (as they are competitors in some sense)).
m1=@(t)sin(2*pi*t+0.4*pi)/2+0.03;
m2=@(t)sin(2*pi*t).*cos(2*pi*t);
mdiff=@(t)m1(t)-m2(t);
figure(1),clf(1)
fplot(mdiff,[0 1])
title('plot of difference')
t_vec=linspace(0,1,1000);
signs=sign(mdiff(t_vec));
cross_indices=find(diff(signs));%not guaranteed to find all zero-crossings
results=zeros(size(cross_indices));
for n=1:numel(results)
results(n)=fzero(mdiff,t_vec([cross_indices(n) cross_indices(n)+1]));
end
results=unique(results);
vals=[results;mdiff(results)];
fprintf('mdiff(%.3f)=%.3f\n',vals)
  1 件のコメント
Rik
Rik 2018 年 9 月 6 日
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

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

カテゴリ

Help Center および File ExchangeOctave についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by