MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Procedure to Function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg8143] Re: [mg8117] Procedure to Function
  • From: Allan Hayes <hay at haystack.demon.co.uk>
  • Date: Fri, 15 Aug 1997 23:41:54 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Robert Pratt <rpratt at math.unc.edu>
in [mg8117] Procedure to Function
writes

>>>>
I am having difficulty rewriting a short procedure as a single
function.Can anyone explain why I get the wrong answer using the  
function instead of the procedure?  Given a harmonic function  
u[x,y], I'm trying to find its harmonic conjugate v[x,y] (so that  
the Cauchy-Riemann equations hold).

PROCEDURE

Clear[u, v, x, y, g]
u = x^3 - 3 x y^2 + y;
v[x_, y_] := Integrate[D[u,x],y] + g[x];
g[x_] := Evaluate[g[x] /.DSolve[D[v[x, y],x] == D[u,y], g[x], x][[1]]];
v[x, y]

-x + 3 x^2 y - y^3 + C[1]

FUNCTION

Clear[u, v, x, y, g, HarmonicConjugate]
HarmonicConjugate[u_] :=
  (v[x_, y_] := Integrate[D[u,x],y] + g[x];
  g[x_] := Evaluate[g[x] /.DSolve[D[v[x, y],x] == D[u,y], g[x],  
x][[1]]];
  v[x, y])
HarmonicConjugate[x^3 - 3 x y^2 + y]

3 x^2 y - y^3 + x (-1 + 6 x y) + C[1]

>>>>>>>

Robert,

There seems to a typing error in your code
	DSolve[D[v[x, y],x] == D[u,y]
should, I think, be
	DSolve[D[v[x, y],x] == - D[u,y]
 	
But the interesting difference between procedural and functional  
forms looks as if is due to scoping causing the introduction of new  
variables:

When we evaluate
	HarmonicConjugate[x^3 - 3 x y^2 + y]
	
The use of 	
HarmonicConjugate[u_] := .....
causes x^3 - 3 x y^2 + y to be substituted for u on the right side.
The first problem is that when this substitution is being looked at  
for u in

v[x_, y_] := Integrate[D[u,x],y] + g[x];      (**)

it is recognised that the x and y in x^3 - 3 x y^2 + y would be  
within the scope of x an y on the left side (v[x_, y_]) and so,  
*before* the substitution is made, the occurrences of x and y in  
(**) are replaced by new variables x$ and y$ . So the definition  
actually stored is

v[x$_, y$_] := Integrate[D[x^3 - 3 x y^2 + y,x$],y$] + g[x$];

You can see this by looking at

TracePrint[HarmonicConjugate[x^3 - 3 x y^2 + y],_SetDelayed]

A similar problem arises from

g[x_] := Evaluate[g[x] /.DSolve[D[v[x, y],x] == D[u,y], g[x], x][[1]]]

If you evaluated these forms you will get the odd answer.

SOLUTION:

A general way of avoiding this kind of problem is to prevent the  
direct substition. Thus:

Clear[u, v, x, y, g, HarmonicConjugate]

HarmonicConjugate[u_] :=
  (uu=u;
		v[x_, y_] := Integrate[D[uu,x],y] + g[x];
  g[x_] := Evaluate[g[x] /.DSolve[D[v[x, y],x] == D[uu,y], g[x],  
x][[1]]];
  v[x, y])

HarmonicConjugate[x^3 - 3 x y^2 + y];

	x - 3*x^2*y - y^3 + C[1]
	
Here is another function, though it does not include the arbitrary  
constant in the result,

HarmonicConjugate2[u_,{x_,y_}]:=
	(# + Integrate[-D[#,x]-D[u,y],x])&[Integrate[D[u,x],y]]

HarmonicConjugate2[x^3 - 3 x y^2 + y,{x,y}]

	-x + 3*x^2*y - y^3
	
Allan

Allan Hayes
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk/training.html
voice:+44 (0)116 2714198
fax: +44 (0)116 2718642
Leicester,  UK








  • Prev by Date: AW: Mathematica question
  • Next by Date: RE: Expanding function names to
  • Previous by thread: AW: Mathematica question
  • Next by thread: Re: Procedure to Function