# Define and solve formula syntax problem

**URL:** <https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096>\
**Category:** Questions\
**Tags:** physics, algebra, equation-solving, numerical-computation\
**Created:** [November 7, 2021, 1:56pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096 "2021-11-07T13:56:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![schubert](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@schubert](https://community.wolfram.com/u/schubert)\
**Post date:** [November 7, 2021, 1:56pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096/1 "2021-11-07T13:56:20Z")

</div>

Hello,

I some problems with the Mathematica syntax for defining and solving equations

I have following equations:

```
electrondensity[x_, Z_, Avog_] := (7*Avog*Z)/(118) * (1/x)^3

electrondensitycrit[\[Lambda]_] := 1.1 10^21/\[Lambda]^2 

```

and try to solve them:

```
NSolve[electrondensity[x_, 12, Avog] == electrondensitycrit[10.6], x_]

```

This however works only if the underscores “_" are used for the variable x  
If “x” instead of "x_”, the solution is {}

Why is it? and why the following error message in case of “x\_” appears? (but still solves the equation):

> Solve::ratnz: Solve was unable to solve the system with inexact  
> coefficients. The answer was obtained by solving a corresponding exact  
> system and numericizing the result.

2nd question:

I want to define a formula:

 ![formula.PNG](https://global.discourse-cdn.com/wolfram/original/3X/8/d/8de20f415d0f9b084bef1c1eee9c1b9e5d70dd30.png)

Do I need to define the formula in this way? (indicating the variables?)

```
Kramer[Z_, \[Lambda]_, T_, x_] := 
 108*10^-4 *Z * 
  Log[4*T/100 *\[Lambda]*2 \[Pi]/(0.577*6.626*10^-34 *3*10^8)]/((T/
        100)^(3/2)*\[Lambda]^2 ) *((electrondensity[
       x])/(electrondensitycrit[\[Lambda]]))^2

```

The error appears:

> “Tag Times in (5.20617_10^-6\ [Lambda]^2  
> Log[6.57374_10^25\ \ [Lambda]]\ Z\_)[Z\_,[Lambda]_,T_,x\_] is  
> Protected”

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [November 7, 2021, 3:16pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096/2 "2021-11-07T15:16:23Z")

</div>

This works well enough for me:

```
electrondensity[x_, Z_, Avog_] := (7*Avog*Z)/(118)*(1/x)^3
electrondensitycrit[\[Lambda]_] := 1.1 10^21/\[Lambda]^2;
Solve[electrondensity[x, 12, Avog] == 
  electrondensitycrit[10.6], x, Reals]

```

It is somewhat tricky, because Mathematica may interpret the cubic root in the complex sense, while you may expect real values only. As for indicating the variables, it depends on what you want to do with the formula. Do you need only to display it, or for actual calculation?

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [November 7, 2021, 3:22pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096/3 "2021-11-07T15:22:01Z")

</div>

Also, beware of the expression `4*T/100 *\[Lambda]*2 \[Pi]`.

Mathematica reads it as `4*(T/100) *\[Lambda]*2 \[Pi]`, which may not be what you have in mind.

---

<div class="post-metadata">

**Author:** ![schubert](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@schubert](https://community.wolfram.com/u/schubert)\
**Post date:** [November 7, 2021, 3:37pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096/4 "2021-11-07T15:37:37Z")

</div>

Ok this works for me also now.

```
electrondensity[x_, Z_, Avog_] := (7*Avog*Z)/(118) * (1/x)^3
electrondensitycrit[\[Lambda]_] := 1.1 10^21/\[Lambda]^2
Konst[\[Lambda]_] := 
 4*T/(100 *\[Lambda]*2 \[Pi])/(0.577*6.626*10^-34 *3*10^8)

```

I want to integrate the following formula for x:

```
Kramer[Z_, \[Lambda]_, T_, x_] := 
 108*10^-4 *Z * 
  Log[Konst[\[Lambda]]]/((T/100)^(3/
        2)*\[Lambda]^2 ) *((electrondensity[
       x])/(electrondensitycrit[\[Lambda]]))^2

```

However, with defining this equation, the error:

> SetDelayed::write: Tag Times in (5.20617_10^-6 [Lambda]^2  
> Log[6.57374_10^25 [Lambda]] Z\_)[Z\_,[Lambda]_,T_,x\_] is Protected.

occurs with the output: $Failed

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [November 7, 2021, 3:50pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096/5 "2021-11-07T15:50:08Z")

</div>

I am getting no errors:

```
Kramer[Z_, \[Lambda]_, T_, x_] := 
  108*10^-4*Z*
   Log[Konst[\[Lambda]]]/((T/100)^(3/
         2)*\[Lambda]^2)*((electrondensity[x, Z, 
        Avog])/(electrondensitycrit[\[Lambda]]))^2;
Integrate[Kramer[Z, \[Lambda], T, x], x]

```

---

<div class="post-metadata">

**Author:** ![schubert](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@schubert](https://community.wolfram.com/u/schubert)\
**Post date:** [November 7, 2021, 3:54pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096/6 "2021-11-07T15:54:13Z")

</div>

Kramer[Z\_, [Lambda]_, T_, x\_] :=  
108_10^-4_Z\*  
Log[Konst[[Lambda]]]/((T/100)^(3/  
2)_[Lambda]^2)_((electrondensity[x, Z,  
Avog])/(electrondensitycrit[[Lambda]]))^2;  
Plot[Integrate[Kramer[12, 10.6, 30, x], x], {x, 0, 40}]

I get

> SetDelayed::write: Tag Times in (5.20617_10^-6 [Lambda]^2  
> Log[6.57374_10^25 [Lambda]] Z\_)[Z\_,[Lambda]_,T_,x\_] is Protected.

and the plot is empty

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [November 7, 2021, 6:25pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096/7 "2021-11-07T18:25:00Z")

</div>

You must give a numerical value `Avog` and decide what function is `Konst`. Also, you must wrap Integrate in `Evaluate`, to avoid the trap of the order of evaluation:

```
electrondensity[x_, Z_, Avog_] := (7*Avog*Z)/(118)*(1/x)^3;
electrondensitycrit[\[Lambda]_] := 1.1 10^21/\[Lambda]^2;
Kramer[Z_, \[Lambda]_, T_, x_] := 
  108*10^-4*Z*
   Log[Konst[\[Lambda]]]/((T/100)^(3/
         2)*\[Lambda]^2)*((electrondensity[x, Z, 
        Avog])/(electrondensitycrit[\[Lambda]]))^2;
Block[{Konst = Identity, 
  Avog = Quantity[1, "AvogadroConstant"] // QuantityMagnitude}, 
 Plot[Evaluate[Integrate[Kramer[12, 10.6, 30, x], x]], {x, 0, 40}]]

```

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [November 7, 2021, 6:30pm UTC](https://community.wolfram.com/t/define-and-solve-formula-syntax-problem/19096/8 "2021-11-07T18:30:10Z")

</div>

Probably because of preexisting conflicting definitions. Quit the kernel and start over.

```
Integrate[Kramer[12, 10.6, 30, x], x]
(* -((7.42291*10^-42 Avog^2 (50.0099 + Log[T]))/x^5) *)

```

`Avog` and `T` do not have numeric values so a `Plot` cannot be generated.
