Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&Athis Q&A.

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&A.

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&A.

Removed restrictions on arguments, added complex contour integral
Source Link
Jens
  • 98.4k
  • 7
  • 217
  • 541
SyntaxInformation[ lineIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, "ArgumentsPattern" -> {_, _, _}}; lineIntegrate[r_?VectorQ, f_Functionf_, {t_, tMin_, tMax_}] := Module[{param, localR}, localR = r /. t -> param; Integrate[(f[localR, #] Sqrt[#.#]) &@D[localR, param], {param, tMin, tMax}]] lineIntegrate[{Cos[t], Sin[t]}, 1 &, {t, 0, 2 Pi}] (* ==> 2 Pi *) 
work[r_dwork[r_, tangent_] := {0, 0, r[[3]]}.tangent lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, work[#1dwork[#1, #2/Sqrt[#2.#2]] &, {t, 0, 2 Pi}] (* ==> 0 *) 

In this example, I defined the differential work dwork as a function that takes the unit tangent as its second argument. To get the unit tangent from the derivative of the curve, I need to pass it the combination #2/Sqrt[#2.#2] where #2 stands for the derivative (which is the second argument provided inside lineIntegrate.

A perhaps more readable definition achieving the same result would be:

dwork2[r_, derivative_] := {0, 0, r[[3]]}.Normalize[derivative]; lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, dwork2, {t, 0, 2 Pi}] (* ==> 0 *) 

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&A.

Edit 2 to add complex contour integral in analogous way

For completeness, here is the analogous definition of a complex contour integral. As in the real line integral, the path is specified in terms of a curve parameter t that is treated as a local variable, but now it's a complex function. The integrand in this application is just a function of the complex position:

SyntaxInformation[ contourIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, "ArgumentsPattern" -> {_, _, _}}; contourIntegrate[z_, f_, {t_, tMin_, tMax_}] := Module[{param, localZ}, localZ = z /. t -> param; Integrate[f[localZ] D[localZ, param], {param, tMin, tMax}]] f[z_] := 1/z contourIntegrate[Cos[t] + I Sin[t], f, {t, 0, 2 Pi}] (* ==> 2 I Pi *) 

The test shows that for an integral of $1/z$ around a circle containing the origin, the residue theorem holds.

SyntaxInformation[ lineIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, "ArgumentsPattern" -> {_, _, _}}; lineIntegrate[r_?VectorQ, f_Function, {t_, tMin_, tMax_}] := Module[{param, localR}, localR = r /. t -> param; Integrate[(f[localR, #] Sqrt[#.#]) &@D[localR, param], {param, tMin, tMax}]] lineIntegrate[{Cos[t], Sin[t]}, 1 &, {t, 0, 2 Pi}] (* ==> 2 Pi *) 
work[r_, tangent_] := {0, 0, r[[3]]}.tangent lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, work[#1, #2/Sqrt[#2.#2]] &, {t, 0, 2 Pi}] (* ==> 0 *) 

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&A.

SyntaxInformation[ lineIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, "ArgumentsPattern" -> {_, _, _}}; lineIntegrate[r_?VectorQ, f_, {t_, tMin_, tMax_}] := Module[{param, localR}, localR = r /. t -> param; Integrate[(f[localR, #] Sqrt[#.#]) &@D[localR, param], {param, tMin, tMax}]] lineIntegrate[{Cos[t], Sin[t]}, 1 &, {t, 0, 2 Pi}] (* ==> 2 Pi *) 
dwork[r_, tangent_] := {0, 0, r[[3]]}.tangent lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, dwork[#1, #2/Sqrt[#2.#2]] &, {t, 0, 2 Pi}] (* ==> 0 *) 

In this example, I defined the differential work dwork as a function that takes the unit tangent as its second argument. To get the unit tangent from the derivative of the curve, I need to pass it the combination #2/Sqrt[#2.#2] where #2 stands for the derivative (which is the second argument provided inside lineIntegrate.

A perhaps more readable definition achieving the same result would be:

dwork2[r_, derivative_] := {0, 0, r[[3]]}.Normalize[derivative]; lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, dwork2, {t, 0, 2 Pi}] (* ==> 0 *) 

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&A.

Edit 2 to add complex contour integral in analogous way

For completeness, here is the analogous definition of a complex contour integral. As in the real line integral, the path is specified in terms of a curve parameter t that is treated as a local variable, but now it's a complex function. The integrand in this application is just a function of the complex position:

SyntaxInformation[ contourIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, "ArgumentsPattern" -> {_, _, _}}; contourIntegrate[z_, f_, {t_, tMin_, tMax_}] := Module[{param, localZ}, localZ = z /. t -> param; Integrate[f[localZ] D[localZ, param], {param, tMin, tMax}]] f[z_] := 1/z contourIntegrate[Cos[t] + I Sin[t], f, {t, 0, 2 Pi}] (* ==> 2 I Pi *) 

The test shows that for an integral of $1/z$ around a circle containing the origin, the residue theorem holds.

More explanations
Source Link
Jens
  • 98.4k
  • 7
  • 217
  • 541

Here's a quick function that does a real line integral:

SyntaxInformation[ lineIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, "ArgumentsPattern" -> {_, _, _}}; lineIntegrate[r_?VectorQ, f_Function, {t_, tMin_, tMax_}] := Module[{param, localR}, localR = r /. t -> param; Integrate[(f[localR, #] Sqrt[#.#]) &@D[localR, param], {param, tMin, tMax}]] lineIntegrate[{Cos[t], Sin[t]}, 1 &, {t, 0, 2 Pi}] (* ==> 2 Pi *) 

The second argument is a function to be evaluated at points along the curve. This function in turn can take two arguments: the position $\vec{r}$ and the derivative of the position with respect to the parameter, $d\vec{r}/dt$.

One could modify this definition so the function takes only the curve parameter as the argument, but I settled on this so that I can calculate things like work integrals easily. For example, take a closed path in 3D, and show that the work along it under a conservative force $\vec{F} = (0, 0, z)$ vanishes:

ParametricPlot3D[{Cos[t], Sin[t], Sin[2 t]}, {t, 0, 2 Pi}, PlotStyle -> Tube[.01]] 

path

work[r_, tangent_] := {0, 0, r[[3]]}.tangent lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, work[#1, #2/Sqrt[#2.#2]] &, {t, 0, 2 Pi}] (* ==> 0 *) 

Edit to address questions in comment

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&A.

Here's a quick function that does a real line integral:

SyntaxInformation[ lineIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, "ArgumentsPattern" -> {_, _, _}}; lineIntegrate[r_?VectorQ, f_Function, {t_, tMin_, tMax_}] := Module[{param, localR}, localR = r /. t -> param; Integrate[(f[localR, #] Sqrt[#.#]) &@D[localR, param], {param, tMin, tMax}]] lineIntegrate[{Cos[t], Sin[t]}, 1 &, {t, 0, 2 Pi}] (* ==> 2 Pi *) 

The second argument is a function to be evaluated at points along the curve. This function in turn can take two arguments: the position $\vec{r}$ and the derivative of the position with respect to the parameter, $d\vec{r}/dt$.

One could modify this definition so the function takes only the curve parameter as the argument, but I settled on this so that I can calculate things like work integrals easily. For example, take a closed path in 3D, and show that the work along it under a conservative force $\vec{F} = (0, 0, z)$ vanishes:

ParametricPlot3D[{Cos[t], Sin[t], Sin[2 t]}, {t, 0, 2 Pi}, PlotStyle -> Tube[.01]] 

path

work[r_, tangent_] := {0, 0, r[[3]]}.tangent lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, work[#1, #2/Sqrt[#2.#2]] &, {t, 0, 2 Pi}] (* ==> 0 *) 

Here's a quick function that does a real line integral:

SyntaxInformation[ lineIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}}, "ArgumentsPattern" -> {_, _, _}}; lineIntegrate[r_?VectorQ, f_Function, {t_, tMin_, tMax_}] := Module[{param, localR}, localR = r /. t -> param; Integrate[(f[localR, #] Sqrt[#.#]) &@D[localR, param], {param, tMin, tMax}]] lineIntegrate[{Cos[t], Sin[t]}, 1 &, {t, 0, 2 Pi}] (* ==> 2 Pi *) 

The second argument is a function to be evaluated at points along the curve. This function in turn can take two arguments: the position $\vec{r}$ and the derivative of the position with respect to the parameter, $d\vec{r}/dt$.

One could modify this definition so the function takes only the curve parameter as the argument, but I settled on this so that I can calculate things like work integrals easily. For example, take a closed path in 3D, and show that the work along it under a conservative force $\vec{F} = (0, 0, z)$ vanishes:

ParametricPlot3D[{Cos[t], Sin[t], Sin[2 t]}, {t, 0, 2 Pi}, PlotStyle -> Tube[.01]] 

path

work[r_, tangent_] := {0, 0, r[[3]]}.tangent lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, work[#1, #2/Sqrt[#2.#2]] &, {t, 0, 2 Pi}] (* ==> 0 *) 

Edit to address questions in comment

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&A.

Corrected wrong version integrate fn.
Source Link
Jens
  • 98.4k
  • 7
  • 217
  • 541
Loading
Modified integrand arguments
Source Link
Jens
  • 98.4k
  • 7
  • 217
  • 541
Loading
Source Link
Jens
  • 98.4k
  • 7
  • 217
  • 541
Loading