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

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 Comment

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.

Sign in to comment.

Answers (1)

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 Comment

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.

Sign in to comment.

Asked:

on 3 Sep 2018

Commented:

Rik
on 6 Sep 2018

Community Treasure Hunt

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

Start Hunting!