finding the intersection point.....

edited October 2010 in Science & Tech
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....

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited October 2010
    what two lines are you trying to find the intersection of. It's pretty likely that you won't be able to find it exactly, however you can find the minimum absolute difference using

    [y,i] = min(abs(line1-line2));

    line1value = line1(i);
    line2value = line2(i);
  • edited October 2010
    Line 1: y = ax + b
    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.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited October 2010
    only works if you can represent your lines symbolically...and if they can be parameterized that way.
  • edited October 2010
    Okay, here is how to derive the symbolic representation

    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)
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited October 2010
    Do any of the equations in his code look like they are linear?
  • edited October 2010
    Line means linear, curve is nonlinear. OP asked about a line and I assumed it is linear instead of checking the code since it looks too cryptic for trying to understand.

    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.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited October 2010
    Yes, that is how it works when you have functions that generate the lines. If this is the case, you're not using matlab, you use pen/pencil or MAPLE if it's more complicated. (Without the symbolic toolbox,) you can't do it like that in matlab. Often times you have an arbitrary function that is given by time indicies and values at those times...which is when you use matlab (and the code that I gave him at the beginning).

    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.
  • edited October 2010
    I have contributed the math background which I thought would be helpful in using matlab more efficiently. Is math forbidden in matlab forum?
  • edited October 2010
    thanks shwaip and mirage.......thank you so much for replying me.....
  • edited October 2010
    shwaip wrote:
    what two lines are you trying to find the intersection of. It's pretty likely that you won't be able to find it exactly, however you can find the minimum absolute difference using

    [y,i] = min(abs(line1-line2));

    line1value = line1(i);
    line2value = line2(i);

    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.....
  • MyrmidonMyrmidon Baron von Puttenham California Icrontian
    edited October 2010
    mirage wrote:
    I have contributed the math background which I thought would be helpful in using matlab more efficiently. Is math forbidden in matlab forum?

    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.
  • edited October 2010
    Myrmidon wrote:
    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....^^
  • MyrmidonMyrmidon Baron von Puttenham California Icrontian
    edited October 2010
    do this on paper:

    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?
  • edited October 2010
    Myrmidon wrote:
    do this on paper:

    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....
  • edited October 2010
    Here is a simple example. Two curves sin(x) and cos(x) plotted as follows
    <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
    function y = f (x)
    y = sin(x) - cos(x);
    endfunction
    t = linspace(0,2*pi,100);
    y1 = sin(t);
    y2 = cos(t);
    plot(t, y1, t, y2);
    [x, fval, info] = fsolve (@f, [1])
    y1=sin(x)
    [x, fval, info] = fsolve (@f, [4])
    y2=sin(x)
    

    The output is as follows
    x =  0.78540
    fval = -1.0025e-13
    info =  1
    y1 =  0.70711
    x =  3.9270
    fval = -1.0530e-12
    info =  1
    y2 = -0.70711
    

    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.
  • edited October 2010
    mirage wrote:
    Here is a simple example. Two curves sin(x) and cos(x) plotted as follows
    5111015494_fc0a541af9.jpg
    There are two intersections near x=1 and x=4
    The code below plots the two curves and finds the intersections
    function y = f (x)
    y = sin(x) - cos(x);
    endfunction
    t = linspace(0,2*pi,100);
    y1 = sin(t);
    y2 = cos(t);
    plot(t, y1, t, y2);
    [x, fval, info] = fsolve (@f, [1])
    y1=sin(x)
    [x, fval, info] = fsolve (@f, [4])
    y2=sin(x)
    

    The output is as follows
    x =  0.78540
    fval = -1.0025e-13
    info =  1
    y1 =  0.70711
    x =  3.9270
    fval = -1.0530e-12
    info =  1
    y2 = -0.70711
    

    I have plotted what you have given in the first post
    5111015546_080b4c3326.jpg

    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.....
  • edited October 2010
    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!
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited October 2010
    fyi, you won't have fsolve without the optimization toolbox.
Sign In or Register to comment.