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

1 vue (au cours des 30 derniers jours)
Candice Edwards
Candice Edwards le 3 Sep 2018
Commenté : Rik le 6 Sep 2018
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 commentaire
Walter Roberson
Walter Roberson le 3 Sep 2018
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.

Connectez-vous pour commenter.

Réponses (1)

Rik
Rik le 3 Sep 2018
Modifié(e) : Rik le 3 Sep 2018
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 commentaire
Rik
Rik le 6 Sep 2018
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Octave dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by