Message Boards Message Boards

0
|
12257 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Assigning a variable a unit

Posted 9 years ago

Hi, I was wondering whether it is possible to assign a unit to a variable however keeping it symbolic. To clarify, I included the following example:

Module[{\[Lambda]0 = Quantity[850 10^-9, "Meters"], \[Sigma] = 
   Quantity[32 10^-9, "Meters"] , 
  g0 = Quantity[50 10^-2, ("Meters")^-1], \[Alpha]t = 
   Quantity[32.2 10^-2, ("Meters")^-1]}, 
 pts = NSolve[
   g0 E^(-(\[Lambda] - \[Lambda]0)^2/(2 \[Sigma]^2)) && \[Alpha]t == 
     0, {\[Lambda]}]]

When the previous code is executed, Mathematica can't determine the unit of Lambda. Using the Quantity-command to assign a unit to Lambda will also assign a value to it, hence, it stops being a variable. I'd appreciate it, if somebody would suggest a useful method to solve this problem. thanks

POSTED BY: Saf Al
4 Replies
Posted 9 years ago

Hi M.,

Your method works fine. I just entered the Quantity-Command in your Module to keep variables local, as follows:

Module[{}, WaveLenght = Quantity[\[Lambda], "Nanometers"]; 
 f[\[Lambda]_] = UnitConvert[Quantity[1, "SpeedOfLight"]]/WaveLenght; 
 f[\[Lambda]]]

Also, I realized that your units must be in Nanometer, if the example to be physically realistic. Thank you again.

POSTED BY: Saf Al

Hi,

I guess that is indeed possible.

\[Lambda] = Quantity[l, "Meters"];
Module[{}, f[\[Lambda]_] = UnitConvert[Quantity[1, "SpeedOfLight"]]/\[Lambda];f[\[Lambda]]]

If you then execute:

QuantityUnit[f[\[Lambda]]]

that will give you 1/Seconds.

Hope this helps,

M.

POSTED BY: Marco Thiel

Hi,

it appears that in principle the thing works, e.g.

Module[{\[Lambda]0 = Quantity[850 10^-9, "Meters"], \[Sigma] = 
   Quantity[32 10^-9, "Meters"], 
  g0 = Quantity[50 10^-2, ("Meters")^-1], \[Alpha]t = 
   Quantity[32.2 10^-2, ("Meters")^-1]}, 
 pts = NSolve[
   g0 E^(-(\[Lambda] - \[Lambda]0)^2/(2 \[Sigma]^2)) == 
    Quantity[1, "Meters"^-1], {\[Lambda]}]]

I think that your input is a bit difficult to understand. Take

NSolve[g0 E^(-(\[Lambda] - \[Lambda]0)^2/(2 \[Sigma]^2)) && \[Alpha]t == 0, {\[Lambda]}]]

The first bit

g0 E^(-(\[Lambda] - \[Lambda]0)^2/(2 \[Sigma]^2))

is not an equation at all. The second bit

\[Alpha]t == 0

seems to be an impossible requirement as you have defined

\[Alpha]t = Quantity[32.2 10^-2, ("Meters")^-1]

a bit earlier. So both the magnitude and the units do not match. Remember that 0 has no unit in your equation!, i.e. 0 is not the same as Quantity[0,"Meters"^-1].

The units must be consistent in your equations or Mathematica runs in to trouble.

Cheers,

Marco

POSTED BY: Marco Thiel
Posted 9 years ago

Hi Marco,

Thanks for your reply. Actually, I was trying to find the intersection points of a curve and a line through NSolve. Obviously, I made a mistake that made Mathematica completely deaf. So you are right, the corrected version of the example is the following:

Module[{\[Lambda]0 = Quantity[850 10^-9, "Meters"], \[Sigma] = 
   Quantity[32 10^-9, "Meters"] , 
  g0 = Quantity[50 10^-2, ("Meters")^-1], \[Alpha]t = 
   Quantity[32.2 10^-2, ("Meters")^-1], 
  Length =  Quantity[400 10^-6, "Meters"] , n = 3.6}, 
 pts = NSolve[
   g0 E^(-(\[Lambda] - \[Lambda]0)^2/(
     2 \[Sigma]^2)) == \[Alpha]t, {\[Lambda]}]]

However, I still wonder whether, it is possible to assign a unit to a variable without giving it a value. For example, when you run the following code:

Module[{}, 
 f[\[Lambda]_] = UnitConvert[Quantity[1, "SpeedOfLight"]]/\[Lambda]; 
 f[\[Lambda]]]

Mathematica shows the unit of the function in m/s. That's, of course, because Mathematica doesn't know the unit of Lambda. But, if it was possible to assign Lambda a Unit in Meters, then the unit of the function (f[Lambda]) will be correct, i.e in hertz. Also the function can stay symbolic for future applications. So do you think that such a thing is possible in Mathematica.

Thank you.

POSTED BY: Saf Al
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract