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 CenterFile Exchange에서 Octave에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by