Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Integrating f(x,y) on a certain line in XY plane

Subject: Integrating f(x,y) on a certain line in XY plane

From: Mohamed Nasr

Date: 8 May, 2011 11:09:04

Message: 1 of 16

Dear all,
I need to integrate f(x,y) on a line joining the 2 points (x1,y1) and (x2,y2) those points are known of course. so we are integrating f(x,y) on dl and the limits of integration are 2 points in XY plane which are (x1,y1) and (x2,y2)....
I failed to find something which does this in matlab functions (quad(s) and int) Does anyone know how to do it?

Thanks in advance

Subject: Integrating f(x,y) on a certain line in XY plane

From: John D'Errico

Date: 8 May, 2011 11:45:05

Message: 2 of 16

"Mohamed Nasr" wrote in message <iq5tkg$j6e$1@newscl01ah.mathworks.com>...
> Dear all,
> I need to integrate f(x,y) on a line joining the 2 points (x1,y1) and (x2,y2) those points are known of course. so we are integrating f(x,y) on dl and the limits of integration are 2 points in XY plane which are (x1,y1) and (x2,y2)....
> I failed to find something which does this in matlab functions (quad(s) and int) Does anyone know how to do it?
>
> Thanks in advance

No. You won't find a function that does this explicitly.
So what?

How do you define a line between two points? Do so
parametrically.

   P(t) = P0 + (P1 - P0)*t

As you can see, when t = 0, P(0) = P0. Likewise, when
t = 1, P(1) = P1. We have defined the line as a function
of the (non-dimensional) parameter t. Of course, when
t is less than zero or greater than 1, we are extrapolating
the line past the endpoints.

Regardless, if you have some function along the line
between those points, really the function is just a
function of the single parameter t. If you wish to
do an integration, it is now trivial.

John

Subject: Integrating f(x,y) on a certain line in XY plane

From: John D'Errico

Date: 8 May, 2011 12:45:20

Message: 3 of 16

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <iq5vo1$nch$1@newscl01ah.mathworks.com>...
> "Mohamed Nasr" wrote in message <iq5tkg$j6e$1@newscl01ah.mathworks.com>...
> > Dear all,
> > I need to integrate f(x,y) on a line joining the 2 points (x1,y1) and (x2,y2) those points are known of course. so we are integrating f(x,y) on dl and the limits of integration are 2 points in XY plane which are (x1,y1) and (x2,y2)....
> > I failed to find something which does this in matlab functions (quad(s) and int) Does anyone know how to do it?
> >
> > Thanks in advance
>
> No. You won't find a function that does this explicitly.
> So what?
>
> How do you define a line between two points? Do so
> parametrically.
>
> P(t) = P0 + (P1 - P0)*t
>
> As you can see, when t = 0, P(0) = P0. Likewise, when
> t = 1, P(1) = P1. We have defined the line as a function
> of the (non-dimensional) parameter t. Of course, when
> t is less than zero or greater than 1, we are extrapolating
> the line past the endpoints.
>
> Regardless, if you have some function along the line
> between those points, really the function is just a
> function of the single parameter t. If you wish to
> do an integration, it is now trivial.
>
> John

For example, do you wish to know the integral of
f(x,y) = x^2 + y^2, along the line connecting the
points, [1,7] and [-3,5]? Fist, decide if you wish to
do this analytically, or numerically.

P0 = [1 7];
P1 = [-3 5];
Poft = @(t) t(:)*P0 + (1-t(:))*P1;
numerical_result = quadgk(@(t) reshape(sum(Poft(t).^2,2),size(t)),0,1)
numerical_result =
          38.6666666666667
 
See that I've rewritten P in a form that is vectorized,
letting it work for any vector of points t. This is
necessary to let quadgk work.

If the solution is symbolic, I can only work with my
sympoly tools, since I lack the symbolic toolbox. No
problem here.

sympoly T
PofT = P0 + (P1 - P0)*T;
defint(sum(PofT.^2),'T',[0 1])
ans =
    38.6666666666667

Simple either way.
John

Subject: Integrating f(x,y) on a certain line in XY plane

From: Roger Stafford

Date: 8 May, 2011 20:05:05

Message: 4 of 16

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <iq6390$17d$1@newscl01ah.mathworks.com>...
> > "Mohamed Nasr" wrote in message <iq5tkg$j6e$1@newscl01ah.mathworks.com>...
> > > Dear all,
> > > I need to integrate f(x,y) on a line joining the 2 points (x1,y1) and (x2,y2) those points are known of course. so we are integrating f(x,y) on dl and the limits of integration are 2 points in XY plane which are (x1,y1) and (x2,y2)....
> > > I failed to find something which does this in matlab functions (quad(s) and int) Does anyone know how to do it?
> > >
> > > Thanks in advance
> ........
> sympoly T
> PofT = P0 + (P1 - P0)*T;
> defint(sum(PofT.^2),'T',[0 1])
> ans =
> 38.6666666666667
> Simple either way.
> John
- - - - - - - - - - -
  In taking John's advice you need to be careful what variable you wish to integrate with respect to. Your statement that "we are integrating f(x,y) on dl" is suggestive that you actually intend to integrate with respect to the distance measured along the line between the two points rather than John's 't' parameter. In that case you need to multiply your f(x,y) by dl/dt where 'l' is distance and 't'is John's parameter. This would give an answer of

 172.9225903

for John's line integral.

Roger Stafford

Subject: Integrating f(x,y) on a certain line in XY plane

From: Mohamed Nasr

Date: 8 May, 2011 20:58:05

Message: 5 of 16

"Roger Stafford" wrote in message <iq6t1h$ocu$1@newscl01ah.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <iq6390$17d$1@newscl01ah.mathworks.com>...
> > > "Mohamed Nasr" wrote in message <iq5tkg$j6e$1@newscl01ah.mathworks.com>...
> > > > Dear all,
> > > > I need to integrate f(x,y) on a line joining the 2 points (x1,y1) and (x2,y2) those points are known of course. so we are integrating f(x,y) on dl and the limits of integration are 2 points in XY plane which are (x1,y1) and (x2,y2)....
> > > > I failed to find something which does this in matlab functions (quad(s) and int) Does anyone know how to do it?
> > > >
> > > > Thanks in advance
> > ........
> > sympoly T
> > PofT = P0 + (P1 - P0)*T;
> > defint(sum(PofT.^2),'T',[0 1])
> > ans =
> > 38.6666666666667
> > Simple either way.
> > John
> - - - - - - - - - - -
> In taking John's advice you need to be careful what variable you wish to integrate with respect to. Your statement that "we are integrating f(x,y) on dl" is suggestive that you actually intend to integrate with respect to the distance measured along the line between the two points rather than John's 't' parameter. In that case you need to multiply your f(x,y) by dl/dt where 'l' is distance and 't'is John's parameter. This would give an answer of
>
> 172.9225903
>
> for John's line integral.
>
> Roger Stafford

Thanks John and Roger for your replies.
Regarding John's advice, John,you used a symmetric function...How can you apply your theory on the following function:

f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).

and XMID1 and YMID1 are known functions while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)...this line has the infinitesmal element dl.
 Waiting your reply

Subject: Integrating f(x,y) on a certain line in XY plane

From: John D'Errico

Date: 8 May, 2011 21:05:07

Message: 6 of 16

"Roger Stafford" wrote in message <iq6t1h$ocu$1@newscl01ah.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <iq6390$17d$1@newscl01ah.mathworks.com>...
> > > "Mohamed Nasr" wrote in message <iq5tkg$j6e$1@newscl01ah.mathworks.com>...
> > > > Dear all,
> > > > I need to integrate f(x,y) on a line joining the 2 points (x1,y1) and (x2,y2) those points are known of course. so we are integrating f(x,y) on dl and the limits of integration are 2 points in XY plane which are (x1,y1) and (x2,y2)....
> > > > I failed to find something which does this in matlab functions (quad(s) and int) Does anyone know how to do it?
> > > >
> > > > Thanks in advance
> > ........
> > sympoly T
> > PofT = P0 + (P1 - P0)*T;
> > defint(sum(PofT.^2),'T',[0 1])
> > ans =
> > 38.6666666666667
> > Simple either way.
> > John
> - - - - - - - - - - -
> In taking John's advice you need to be careful what variable you wish to integrate with respect to. Your statement that "we are integrating f(x,y) on dl" is suggestive that you actually intend to integrate with respect to the distance measured along the line between the two points rather than John's 't' parameter. In that case you need to multiply your f(x,y) by dl/dt where 'l' is distance and 't'is John's parameter. This would give an answer of
>
> 172.9225903
>
> for John's line integral.
>
> Roger Stafford

Oops, yes, I forgot to add in that part.

John

Subject: Integrating f(x,y) on a certain line in XY plane

From: John D'Errico

Date: 8 May, 2011 21:09:07

Message: 7 of 16

"Mohamed Nasr" wrote in message <iq704t$1av$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <iq6t1h$ocu$1@newscl01ah.mathworks.com>...
> > "John D'Errico" <woodchips@rochester.rr.com> wrote in message <iq6390$17d$1@newscl01ah.mathworks.com>...
> > > > "Mohamed Nasr" wrote in message <iq5tkg$j6e$1@newscl01ah.mathworks.com>...
> > > > > Dear all,
> > > > > I need to integrate f(x,y) on a line joining the 2 points (x1,y1) and (x2,y2) those points are known of course. so we are integrating f(x,y) on dl and the limits of integration are 2 points in XY plane which are (x1,y1) and (x2,y2)....
> > > > > I failed to find something which does this in matlab functions (quad(s) and int) Does anyone know how to do it?
> > > > >
> > > > > Thanks in advance
> > > ........
> > > sympoly T
> > > PofT = P0 + (P1 - P0)*T;
> > > defint(sum(PofT.^2),'T',[0 1])
> > > ans =
> > > 38.6666666666667
> > > Simple either way.
> > > John
> > - - - - - - - - - - -
> > In taking John's advice you need to be careful what variable you wish to integrate with respect to. Your statement that "we are integrating f(x,y) on dl" is suggestive that you actually intend to integrate with respect to the distance measured along the line between the two points rather than John's 't' parameter. In that case you need to multiply your f(x,y) by dl/dt where 'l' is distance and 't'is John's parameter. This would give an answer of
> >
> > 172.9225903
> >
> > for John's line integral.
> >
> > Roger Stafford
>
> Thanks John and Roger for your replies.
> Regarding John's advice, John,you used a symmetric function...How can you apply your theory on the following function:
>
> f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).
>
> and XMID1 and YMID1 are known functions while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)...this line has the infinitesmal element dl.
> Waiting your reply

There is nothing in what I did that relied on symmetry,
except that I chose something simple to evaluate for
demonstration purposes.

As long as you can evaluate a function at any point
on the line, absolutely nothing stops you from doing
exactly as I did. Admittedly, your function is not going
to be possible to do in symbolic terms.

John

Subject: Integrating f(x,y) on a certain line in XY plane

From: Mohamed Nasr

Date: 8 May, 2011 21:55:05

Message: 8 of 16

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <iq70pj$2o7$1@newscl01ah.mathworks.com>...
> "Mohamed Nasr" wrote in message <iq704t$1av$1@newscl01ah.mathworks.com>...
> > "Roger Stafford" wrote in message <iq6t1h$ocu$1@newscl01ah.mathworks.com>...
> > > "John D'Errico" <woodchips@rochester.rr.com> wrote in message <iq6390$17d$1@newscl01ah.mathworks.com>...
> > > > > "Mohamed Nasr" wrote in message <iq5tkg$j6e$1@newscl01ah.mathworks.com>...
> > > > > > Dear all,
> > > > > > I need to integrate f(x,y) on a line joining the 2 points (x1,y1) and (x2,y2) those points are known of course. so we are integrating f(x,y) on dl and the limits of integration are 2 points in XY plane which are (x1,y1) and (x2,y2)....
> > > > > > I failed to find something which does this in matlab functions (quad(s) and int) Does anyone know how to do it?
> > > > > >
> > > > > > Thanks in advance
> > > > ........
> > > > sympoly T
> > > > PofT = P0 + (P1 - P0)*T;
> > > > defint(sum(PofT.^2),'T',[0 1])
> > > > ans =
> > > > 38.6666666666667
> > > > Simple either way.
> > > > John
> > > - - - - - - - - - - -
> > > In taking John's advice you need to be careful what variable you wish to integrate with respect to. Your statement that "we are integrating f(x,y) on dl" is suggestive that you actually intend to integrate with respect to the distance measured along the line between the two points rather than John's 't' parameter. In that case you need to multiply your f(x,y) by dl/dt where 'l' is distance and 't'is John's parameter. This would give an answer of
> > >
> > > 172.9225903
> > >
> > > for John's line integral.
> > >
> > > Roger Stafford
> >
> > Thanks John and Roger for your replies.
> > Regarding John's advice, John,you used a symmetric function...How can you apply your theory on the following function:
> >
> > f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).
> >
> > and XMID1 and YMID1 are known functions while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)...this line has the infinitesmal element dl.
> > Waiting your reply
>
> There is nothing in what I did that relied on symmetry,
> except that I chose something simple to evaluate for
> demonstration purposes.
>
> As long as you can evaluate a function at any point
> on the line, absolutely nothing stops you from doing
> exactly as I did. Admittedly, your function is not going
> to be possible to do in symbolic terms.
>
> John
What I meant by symmetry is that you could express (X^2) and (y^2) both by (t^2) in your formula...if they were different like my example we wouldn't have been able to do that(I corrected some typing mistakes in my function below).

You made the change only on t from 0-->1 while in the following function we have both X,Y changing together:

 f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).
 
as XM and YM are known values while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)...this line has the infinitesmal element dl.
Can you please show me how to apply your theory on this equation?

Subject: Integrating f(x,y) on a certain line in XY plane

From: Roger Stafford

Date: 8 May, 2011 22:04:04

Message: 9 of 16

"Mohamed Nasr" wrote in message <iq704t$1av$1@newscl01ah.mathworks.com>...
> ..........
> f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).
>
> and XMID1 and YMID1 are known functions while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)...this line has the infinitesmal element dl.
> Waiting your reply
- - - - - - - - - -
  In the case of the function you have defined, Mohamed, there is an easily expressed analytic function which is the solution to your integral. You don't need matlab's numerical integration at all to solve this problem, (except perhaps the symbolic toolbox for obtaining this integral solution.)

  I will give an indefinite integral formula from my integral table textbook which could be used to derive the above solution in case you don't have the symbolic toolbox:

 int(log(x^2+a^2)) = x*log(x^2+a^2) - 2*x + 2*a*atan(x/a) + C

In your case the variable x here would represent the distance, l, measured along the line segment. If you want help in this latter endeavor, please let us know.

Roger Stafford

Subject: Integrating f(x,y) on a certain line in XY plane

From: Mohamed Nasr

Date: 8 May, 2011 23:29:04

Message: 10 of 16

"Roger Stafford" wrote in message <iq740k$9e4$1@newscl01ah.mathworks.com>...
> "Mohamed Nasr" wrote in message <iq704t$1av$1@newscl01ah.mathworks.com>...
> > ..........
> > f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).
> >
> > and XMID1 and YMID1 are known functions while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)...this line has the infinitesmal element dl.
> > Waiting your reply
> - - - - - - - - - -
> In the case of the function you have defined, Mohamed, there is an easily expressed analytic function which is the solution to your integral. You don't need matlab's numerical integration at all to solve this problem, (except perhaps the symbolic toolbox for obtaining this integral solution.)
>
> I will give an indefinite integral formula from my integral table textbook which could be used to derive the above solution in case you don't have the symbolic toolbox:
>
> int(log(x^2+a^2)) = x*log(x^2+a^2) - 2*x + 2*a*atan(x/a) + C
>
> In your case the variable x here would represent the distance, l, measured along the line segment. If you want help in this latter endeavor, please let us know.
>
> Roger Stafford

How will you tell matlab the coordinates of the line l in order to integrate on it?
Note that in this 2D problem I cannot just consider the length of the line of integration regardless of its position in space due to physics of problem. It is getting charge distribution on a contour which I already know its voltage so position of one part of body w.r.t. other part can totally change charge distribution...

Subject: Integrating f(x,y) on a certain line in XY plane

From: Roger Stafford

Date: 9 May, 2011 05:06:04

Message: 11 of 16

"Mohamed Nasr" wrote in message <iq7900$jj3$1@newscl01ah.mathworks.com>...
> How will you tell matlab the coordinates of the line l in order to integrate on it?
> Note that in this 2D problem I cannot just consider the length of the line of integration regardless of its position in space due to physics of problem. It is getting charge distribution on a contour which I already know its voltage so position of one part of body w.r.t. other part can totally change charge distribution...
- - - - - - - - - -
  Mohamed, you wrote this:

 "f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).
 
as XM and YM are known values while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)." I assumed when I read that that the quantities XM and YM where values held constant throughout the integration line between (X1,Y1) and (X2,Y2). If that is so, then as I said, there is a simple formula for that integral with no numerical integration required.

  However your statement "It is getting charge distribution on a contour" would appear to imply that XM and YM vary as you travel along the length of the line. If that is the case, then it is a different problem and your integral would probably require numerical integration.

  It would be helpful at this point if you could make an unambiguous statement as to whether the XM and YM in your expression do or do not vary along the length of the path. Also you should make clear that when you say "line" you mean a straight line, as contrasted with some curved path along a contour. Both John's and my comments have been based on the assumption that you meant a straight line. With a curved path it is a different world again. It is understood in mathematics that the word 'line' always refers to a straight line, not a curved path.

  Assuming that they do vary as functions of position (x,y) and that you are traveling along a straight line, call them XM(x,y) and YM(x,y). It is up to you to define these functions. Then your integrand could be expressed as a function of s as follows:

 f(x(s),y(s)) = 1/2*log((x(s)-XM(x(s),y(s)))^2+(y(s)-YM(x(s),y(s)))^2)

where s is the distance measured along the straight line from (x1,y1) to (x2,y2). The variables x and y depend on s according to the equations:

 x(s) = x1+a*s
 y(s) = y1+b*s

where

 a = (x2-x1)/d
 b = (y2-y1)/d
 d = sqrt((x2-x1)^2+(y2-y1)^2)

The integral you would then evaluate would be the integral of f(x(s),y(s)) taken with respect to distance s from s = 0 to s = d.

  If you are able to express XM(x,y) and YM(x,y) as valid matlab functions, then you could use quadrature functions such as quad or the like. Otherwise, you would probably have to use 'trapz' for your numerical integration making use of some sort of discrete values for XM(x,y) and YM(x,y) (probably requiring some interpolation.)

  I hope all this makes sense to you. I can do no better until you make very clear these ambiguities which I have mentioned above.

Roger Stafford

Subject: Integrating f(x,y) on a certain line in XY plane

From: Mohamed Nasr

Date: 9 May, 2011 12:59:07

Message: 12 of 16

"Roger Stafford" wrote in message <iq7sns$1j5$1@newscl01ah.mathworks.com>...
> "Mohamed Nasr" wrote in message <iq7900$jj3$1@newscl01ah.mathworks.com>...
> > How will you tell matlab the coordinates of the line l in order to integrate on it?
> > Note that in this 2D problem I cannot just consider the length of the line of integration regardless of its position in space due to physics of problem. It is getting charge distribution on a contour which I already know its voltage so position of one part of body w.r.t. other part can totally change charge distribution...
> - - - - - - - - - -
> Mohamed, you wrote this:
>
> "f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).
>
> as XM and YM are known values while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)." I assumed when I read that that the quantities XM and YM where values held constant throughout the integration line between (X1,Y1) and (X2,Y2). If that is so, then as I said, there is a simple formula for that integral with no numerical integration required.
>
> However your statement "It is getting charge distribution on a contour" would appear to imply that XM and YM vary as you travel along the length of the line. If that is the case, then it is a different problem and your integral would probably require numerical integration.
>
> It would be helpful at this point if you could make an unambiguous statement as to whether the XM and YM in your expression do or do not vary along the length of the path. Also you should make clear that when you say "line" you mean a straight line, as contrasted with some curved path along a contour. Both John's and my comments have been based on the assumption that you meant a straight line. With a curved path it is a different world again. It is understood in mathematics that the word 'line' always refers to a straight line, not a curved path.
>
> Assuming that they do vary as functions of position (x,y) and that you are traveling along a straight line, call them XM(x,y) and YM(x,y). It is up to you to define these functions. Then your integrand could be expressed as a function of s as follows:
>
> f(x(s),y(s)) = 1/2*log((x(s)-XM(x(s),y(s)))^2+(y(s)-YM(x(s),y(s)))^2)
>
> where s is the distance measured along the straight line from (x1,y1) to (x2,y2). The variables x and y depend on s according to the equations:
>
> x(s) = x1+a*s
> y(s) = y1+b*s
>
> where
>
> a = (x2-x1)/d
> b = (y2-y1)/d
> d = sqrt((x2-x1)^2+(y2-y1)^2)
>
> The integral you would then evaluate would be the integral of f(x(s),y(s)) taken with respect to distance s from s = 0 to s = d.
>
> If you are able to express XM(x,y) and YM(x,y) as valid matlab functions, then you could use quadrature functions such as quad or the like. Otherwise, you would probably have to use 'trapz' for your numerical integration making use of some sort of discrete values for XM(x,y) and YM(x,y) (probably requiring some interpolation.)
>
> I hope all this makes sense to you. I can do no better until you make very clear these ambiguities which I have mentioned above.
>
> Roger Stafford

Thanks Roger for your reply:
1-XM and YM are constant while integrating on X and Y which are the variable.
2-Limits of integration on X,Y are (X1,Y1) and (X2,Y2) as I said before.
3-To explain what I meant by contour I meant that every time we call that function in which I make the integral I use a new XM and YM...but through the same integral I use the same XM and YM so don't worry about this point as through the same integral... XM and YM are fixed.
4-My question now is if
 f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2)))=0.5*log((XM-X).^2+(YM-Y).^2)
how will you after that reach log((XM-X).^2) from the previous form to be able to apply your formula for integration?

Subject: Integrating f(x,y) on a certain line in XY plane

From: Torsten

Date: 9 May, 2011 14:46:27

Message: 13 of 16

On 9 Mai, 14:59, "Mohamed Nasr" <eng.mohamednasr2...@gmail.com> wrote:
> "Roger Stafford" wrote in message <iq7sns$1j...@newscl01ah.mathworks.com>...
> > "Mohamed Nasr" wrote in message <iq7900$jj...@newscl01ah.mathworks.com>...
> > > How will you tell matlab the coordinates of the line l in order to integrate on it?
> > > Note that in this 2D problem I cannot just consider the length of the line of integration regardless of its position in space due to physics of problem. It is getting charge distribution on a contour which I already know its voltage so position of one part of body w.r.t. other part can totally change charge distribution...
> > - - - - - - - - - -
> >   Mohamed, you wrote this:
>
> >  "f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2))).
>
> > as XM and YM are known values while X,Y are the variables to be integrated on the line connecting 2 points (X1,Y1) and (X2,Y2)."  I assumed when I read that that the quantities XM and YM where values held constant throughout the integration line between (X1,Y1) and (X2,Y2).  If that is so, then as I said, there is a simple formula for that integral with no numerical integration required.
>
> >   However your statement "It is getting charge distribution on a contour" would appear to imply that XM and YM vary as you travel along the length of the line.  If that is the case, then it is a different problem and your integral would probably require numerical integration.
>
> >   It would be helpful at this point if you could make an unambiguous statement as to whether the XM and YM in your expression do or do not vary along the length of the path.  Also you should make clear that when you say "line" you mean a straight line, as contrasted with some curved path along a contour.  Both John's and my comments have been based on the assumption that you meant a straight line.  With a curved path it is a different world again.  It is understood in mathematics that the word 'line' always refers to a straight line, not a curved path.
>
> >   Assuming that they do vary as functions of position (x,y) and that you are traveling along a straight line, call them XM(x,y) and YM(x,y).  It is up to you to define these functions.  Then your integrand could be expressed as a function of s as follows:
>
> >  f(x(s),y(s)) = 1/2*log((x(s)-XM(x(s),y(s)))^2+(y(s)-YM(x(s),y(s)))^2)
>
> > where s is the distance measured along the straight line from (x1,y1) to (x2,y2).  The variables x and y depend on s according to the equations:
>
> >  x(s) = x1+a*s
> >  y(s) = y1+b*s
>
> > where
>
> >  a = (x2-x1)/d
> >  b = (y2-y1)/d
> >  d = sqrt((x2-x1)^2+(y2-y1)^2)
>
> > The integral you would then evaluate would be the integral of f(x(s),y(s)) taken with respect to distance s from s = 0 to s = d.
>
> >   If you are able to express XM(x,y) and YM(x,y) as valid matlab functions, then you could use quadrature functions such as quad or the like.  Otherwise, you would probably have to use 'trapz' for your numerical integration making use of some sort of discrete values for XM(x,y) and YM(x,y) (probably requiring some interpolation.)
>
> >   I hope all this makes sense to you.  I can do no better until you make very clear these ambiguities which I have mentioned above.
>
> > Roger Stafford
>
> Thanks Roger for your reply:
> 1-XM and YM are constant while integrating on X and Y which are the variable.
> 2-Limits of integration on X,Y are (X1,Y1) and (X2,Y2) as I said before.
> 3-To explain what I meant by contour I meant that every time we call that function in  which I make the integral I use a new XM and YM...but through the same integral I use the same XM and YM so don't worry about this point as through the same integral... XM and YM are fixed.
> 4-My question now is if
>  f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2)))=0.5*log((XM-X).^2+(YM-Y).^2)
> how will you after that reach log((XM-X).^2) from the previous form to be able to apply your formula for integration?- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Did you try
http://www.integrals.wolfram.com
for
integral log((x1+a*x-xm)^2+(y1+b*x-ym)^2) dx
where
a = (x2-x1)/d
b = (y2-y1)/d
d = sqrt((x2-x1)^2+(y2-y1)^2)
?
Evaluate the expression in the limits x=0 to x=d and
you'll have an analytical expression for the definite integral -
no numerical integration required, as Roger already noticed.

Best wishes
Torsten.

Subject: Integrating f(x,y) on a certain line in XY plane

From: Roger Stafford

Date: 9 May, 2011 18:59:07

Message: 14 of 16

"Mohamed Nasr" wrote in message <iq8oeq$5tv$1@newscl01ah.mathworks.com>...
> ..........
> 4-My question now is if
> f(x,y)=log(abs(sqrt((XM-X).^2+(YM-Y).^2)))=0.5*log((XM-X).^2+(YM-Y).^2)
> how will you after that reach log((XM-X).^2) from the previous form to be able to apply your formula for integration?
- - - - - - - - - - - -
  I didn't say you could reduce your expression to log((XM-X).^2). What I said was that it could be reduced to the form log(x^2+a^2)) which is a very different entity.

  Briefly stated, the reasoning goes as follows. The expression (XM-X).^2+(YM-Y).^2, when X and Y are expressed in terms of the distance s from (X1,Y1) to (X2,Y2), can be written as something of the form

 (a*s+b)^2+(c*s+d)^2 = (a^2+c^2)*s^2 + 2*(a*b+c*d)*s + (b^2+d^2)

for the appropriate constants a, b, c, and d. If you complete the square on this quadratic in s, it then becomes

 (a^2+c^2) * ((s-e)^2+f^2)

where

 e = -(a*b+c*d)/(a^2+c^2)

and

 f = sqrt( (b^2+d^2)/(a^2+c^2)-e^2 )

with the quantity inside the square root being known to be positive. Hence the original logarithm expression becomes

 log((a^2+c^2)*((s-e)^2+f^2)) = log(a^2+c^2) + log((s-e)^2+f^2)

The first of these terms is a constant and if t = s-e is substituted, the second term is log(t^2+f^2) for which the integral is the formula I have previously given to you with t and f occurring in place of x and a in that formula.

  If this manipulation seems overly burdensome to you I would recommend that if possible you use the Symbolic Toolbox to do the integration symbolically to save labor.

Roger Stafford

Subject: Integrating f(x,y) on a certain line in XY plane

From: Roger Stafford

Date: 9 May, 2011 20:39:05

Message: 15 of 16

"Roger Stafford" wrote in message <iq9dhr$9bb$1@newscl01ah.mathworks.com>...
> I didn't say you could reduce your expression to log((XM-X).^2). What I said was that it could be reduced to the form log(x^2+a^2)) which is a very different entity.
> .........
- - - - - - - - -
  Mohamed, the explanation I gave you earlier could have been made much more intuitively evident. Hopefully the following is an improvement.

  You start with log(sqrt((X-XM)^2+(Y-YM)^2)) to be integrated along some straight line extending from (X1,Y1) to (X2,Y2). The quantity within the logarithm is the distance from a point (X,Y), which is moving along this line segment, to the point (XM,YM). Now find the closest point (X0,Y0) along the extended line to the point (XM,YM). The three points (X,Y), (XM,YM), and (X0,Y0) will form a right triangle, so by the theorem of Pythagoras we have

 (X-XM)^2+(Y-YM)^2 = (X-X0)^2+(Y-Y0)^2 + (X0-XM)^2+(Y0-YM)^2

If we call t the distance from (X,Y) to (X0,Y0) and f the distance from (X0,Y0) to (XM,YM), this can be written expressed as t^2+f^2.

This means we seek to integrate

 log(sqrt(t^2+f^2)) = 1/2*log(t^2+f^2)

along the line segment from (X1,Y1) to (X2,Y2). The indefinite integral of

 log(t^2+f^2)

with respect to t is given by the formula

 t*log(t^2+f^2) - 2*t + 2*f*atan(t/f) + C

which would allow you to express the definite integral in terms of the t values at (X1,Y1) and (X2,Y2) and the constant f. No need to do numerical integration.

  (I overlooked the fact that the a^2+c^2 quantity in my previous discussion is necessarily equal to 1, so the log(a^2+c^2) constant would have been zero.)

Roger Stafford

Subject: Integrating f(x,y) on a certain line in XY plane

From: Mohamed Nasr

Date: 9 May, 2011 20:58:05

Message: 16 of 16

"Roger Stafford" wrote in message <iq9jd9$po9$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <iq9dhr$9bb$1@newscl01ah.mathworks.com>...
> > I didn't say you could reduce your expression to log((XM-X).^2). What I said was that it could be reduced to the form log(x^2+a^2)) which is a very different entity.
> > .........
> - - - - - - - - -
> Mohamed, the explanation I gave you earlier could have been made much more intuitively evident. Hopefully the following is an improvement.
>
> You start with log(sqrt((X-XM)^2+(Y-YM)^2)) to be integrated along some straight line extending from (X1,Y1) to (X2,Y2). The quantity within the logarithm is the distance from a point (X,Y), which is moving along this line segment, to the point (XM,YM). Now find the closest point (X0,Y0) along the extended line to the point (XM,YM). The three points (X,Y), (XM,YM), and (X0,Y0) will form a right triangle, so by the theorem of Pythagoras we have
>
> (X-XM)^2+(Y-YM)^2 = (X-X0)^2+(Y-Y0)^2 + (X0-XM)^2+(Y0-YM)^2
>
> If we call t the distance from (X,Y) to (X0,Y0) and f the distance from (X0,Y0) to (XM,YM), this can be written expressed as t^2+f^2.
>
> This means we seek to integrate
>
> log(sqrt(t^2+f^2)) = 1/2*log(t^2+f^2)
>
> along the line segment from (X1,Y1) to (X2,Y2). The indefinite integral of
>
> log(t^2+f^2)
>
> with respect to t is given by the formula
>
> t*log(t^2+f^2) - 2*t + 2*f*atan(t/f) + C
>
> which would allow you to express the definite integral in terms of the t values at (X1,Y1) and (X2,Y2) and the constant f. No need to do numerical integration.
>
> (I overlooked the fact that the a^2+c^2 quantity in my previous discussion is necessarily equal to 1, so the log(a^2+c^2) constant would have been zero.)
>
> Roger Stafford

Thanks my friends I managed to get the desired results in the light of your efforts...Many thanks

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us