finding the intersection point.....
hi everyone, i am facing a problem to find the intersection point of two lines. the code that i have written is as below:-
clc
clear
M=1800;
k=2*pi;
theta=linspace(0,2*pi,M+1);
N=2;
beta=pi/4;
d=1/4;
Y=k.*d.*cos(theta)+beta;
E= cos(theta);
AF= sin((N.*Y)./2)./(N.*sin(Y./2));
Pat=abs(E.*AF);
AFdb=10.*log10(Pat);
plot(theta.*180./pi,AFdb, theta.*180./pi, -3, 'r');
axis([0 360 -60 0])
xlabel('theta 0 to 2pi');
ylabel('Gain of the radiation pattern in dB');
grid on;
Does anyone know how can I find the point of intersection and display the value as well........i reli need help in matlab....thank you very much for anyone who can help me, really thanks....
clc
clear
M=1800;
k=2*pi;
theta=linspace(0,2*pi,M+1);
N=2;
beta=pi/4;
d=1/4;
Y=k.*d.*cos(theta)+beta;
E= cos(theta);
AF= sin((N.*Y)./2)./(N.*sin(Y./2));
Pat=abs(E.*AF);
AFdb=10.*log10(Pat);
plot(theta.*180./pi,AFdb, theta.*180./pi, -3, 'r');
axis([0 360 -60 0])
xlabel('theta 0 to 2pi');
ylabel('Gain of the radiation pattern in dB');
grid on;
Does anyone know how can I find the point of intersection and display the value as well........i reli need help in matlab....thank you very much for anyone who can help me, really thanks....
0
Comments
[y,i] = min(abs(line1-line2));
line1value = line1(i);
line2value = line2(i);
Line 2: y = cx + d
x = (b - d)/(a - c)
y = a[(b - d)/(a - c)] + b
or
y = c[(b - d)/(a - c)] + d
If a = c then there is no solution.
The line y = ax + b passing through two points (x1,y1), (x2,y2)
a = (y2 - y1)/(x2 - x1)
b = y1 - x1(y2 - y1)/(x2 - x1)
For two curves y=f1(x) and y=f2(x)
x will obtained from solving f1(x)=f2(x) and there can be multiple solutions (or none) depending on the interval of x. Then, y values can be obtained from either of the equations.
It's also quite possible to have functions that are difficult (if not impossible) to find the intersection of analytically and you must do it numerically...which is perhaps why he's asking the question in a matlab forum.
Hi shwaip, I am sorry to bother you again....but can you please tell me what is represented by y, i, line1 and line2 in your code. and from my code, what should i substitute the value? mayb I am asking a dumb question but I really need your help....I am too new to matlab......thanks a lot.....
The maths is awesome. They just won't work. Matlab doesn't see a line, instead it sees two matrices with a metric ton of numbers. Instead of graphing a line on paper, it's graphing a bunch of dots - and it's very unlikely that the dots from each 'line' will overlap perfectly. And it's not very easy to ask Matlab to just use its intuition to 'see' the line, you know? It sees dots and only dots.
Shwaip's code is a good way to deal with this - if you want to think of it graphically, he's creating a third graph that charts the DIFFERENCE between the lines against the X-axis, looks for the smallest value of the graph, then returns that X-value as an approximation for an intersection.
line1 should be your matrix of values for line 1, and similar for line 2.
i should be a counter for checking every single value in line1 and line2. i represents the points on the x axis.
y is a dummy variable that looks like it's going to store the minimum difference between line1value and line2value. It's NOT your y point of intersection.
I am sorry that I still very confusing of what should be my line1 and line2?
can you please help me look a my code?? I am needing your help.....I think you are having good command in matlab......thanks for your help....^^
graph both of your lines. on paper. Pick one of the lines. Select maybe 30 points on that line and write out their y-axis values, one by one, with commas separating each.
THAT'S what line1 is. That long list of y-axis values. line2 is the exact same thing, just on the other line.
Now, you wrote your code, so you should know what your y-axis values are - one of them's a horizontal line! I'm not going to point to your code and TELL you what they are, man! Shwaip's given you a really good method for finding an approximation of the intersection, and now you've got a definition of all the variables he used. Can you find a way to implement it?
thanks so much Myrmidon....I know I should nt rely on others to help me....I have written the code for line1 and line2 after reading the explaination from you,
line1=AFdb;
line2=-3.*ones(size(line1));
is it correct?? coz the answer i get is 1201....
<a href="http://www.flickr.com/photos/55106441@N03/5111015494/" title="sincos by mrg666, on Flickr"><img src="http://farm2.static.flickr.com/1251/5111015494_fc0a541af9.jpg" width="500" height="375" alt="sincos" /></a>
There are two intersections near x=1 and x=4
The code below plots the two curves and finds the intersections
The output is as follows
I have plotted what you have given in the first post
<a href="http://www.flickr.com/photos/55106441@N03/5111015546/" title="icr by mrg666, on Flickr"><img src="http://farm5.static.flickr.com/4131/5111015546_080b4c3326.jpg" width="500" height="375" alt="icr" /></a>
But the second curve is not there to find an intersection. Maybe you can follow the example to do something similar. You need to plot and check the intersections to supply the initial estimates of the iterative nonlinear solver since each estimate will lead you to the closest solution.
PS: I used Octave on a Mac which is a free program and largely compatible with Matlab.
Mirage.....I don't know how to express my thanks towards you.....really really thanks a lot for your help......you are the best.....
You are welcome. As I wrote earlier, you are just solving f1(x) - f2(x) = 0 for x where y = f1(x) and y = f2(x) are your curves. It is useful to plot first to see the intersections and start with close initial estimates. Good luck!