Plotting the difference of two functions and finding the roots - octave
1 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 Exchange 中查找有关 Octave 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!