Finding where plots may cross with octave / matlab

亡梦爱人 提交于 2019-11-28 05:26:43

问题


I have several data points that are plotted below and I would like to find the frequency value when the amplitude value crosses 4. I've included an example along with the data points in the example below. I've circled the answer graphically but I'm not sure how to compute it mathematically and get all the values for the frequencies I desire. How can I do this with octave / matlab? Also is there a mathematical term for what I'm trying to do?

In this example I'm trying to get 5 frequencies (but this is just an example) I know two answers are 30 and 80 but not sure how to get the rest. The full list could be thousands. I'm using octave 3.8.1

clear all,clf, clc,tic
%graphics_toolkit gnuplot %use this for now it's older but allows zoom
freq=[20,30,40,50,60,70,80];
amp_orig=[2,4,3,7,1,8,4];
amp_inv=[6,4,5,1,7,0,4];


plot(freq,amp_orig,'-bo')
hold on
plot(freq,amp_inv,'-r*')
xlabel ("Frequency");
ylabel ("Amplitude");

Thanks


回答1:


Try this function from the File Exchange, it seems to work just fine in Octave. x0 are the frequencies of interest:

>> [x0,y0,iout,jout] = intersections(freq,amp_orig,freq,amp_inv)
x0 =

   30.000
   30.000
   42.500
   55.000
   64.286
   80.000

y0 =

   4.0000
   4.0000
   4.0000
   4.0000
   4.0000
   4.0000

iout =

   2.0000
   2.0000
   3.2500
   4.5000
   5.4286
   7.0000

jout =

   2.0000
   2.0000
   3.2500
   4.5000
   5.4286
   7.0000



回答2:


Should you have access to Matlabs Mapping Toolbox, your problem can be solved easily using the function polyxpoly(). It will find the intersections of two graphs. They do not even have to be lines, also polygons work. Here is an example for your case:

%graphics_toolkit gnuplot %use this for now it's older but allows zoom
freq=[20,30,40,50,60,70,80];
amp_orig=[2,4,3,7,1,8,4];
amp_inv=[6,4,5,1,7,0,4];

plot(freq,amp_orig,'-bo')
hold on
plot(freq,amp_inv,'-r*')
xlabel ('Frequency');
ylabel ('Amplitude');

%// find and add intersections
[xi,yi] = polyxpoly(freq,amp_orig,freq,amp_inv)
plot(xi, yi,'ro','markersize',20) %// draw the red circles automatically :-)

The result looks like this



来源:https://stackoverflow.com/questions/25984168/finding-where-plots-may-cross-with-octave-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!