# Plotting self-made functions [closed]

EDITED AS SUGGESTED

I have defined a function, which takes two values and returns a function n(x), like so

Function[d_, σ_] :=
Module[{h, ϵ0, m, e0, α, κ, β, a, ϕ, e, n},
h = 6.62606957*10^(-34);
ϵ0 = 8.8541878176*10^(-12);
m = 9.10938291*10^(-31);
e0 = 1.6*10^(-19);
α = (3/5 2^(-1/3) (3/π)^(2/3))*((ϵ0*h^2)/(m*(e0)^(5/3)))*(d^(-5/3)*σ^(-1/3));
κ = (5*α/3)*(1/2)^(2/3);
β = 4/(κ^(3/4)*Sqrt[5]);
a = 4/(β*Sinh[β/2]);
ϕ = (Cosh[β (0.5 - x)])/(β*Sinh[β/2]) - (1/β)*Coth[β/2];
e = -D[ϕ, x];
n = 0.5*D[ϕ, {x, 2}];
Return[n]]


Plotting n is no problem

Function[10^(-10), 0.01]
0.995427 Cosh[0.331805 (0.5 - x)]


This is n(x). It doesn't work if I use the function Function in the plot section.

Plot[Function[a,b],{x,0,1}]


and I get the message

0.000020428571428571424 is not a valid variable.


I think I understand how this happens but would like to bypass it as I don't want to copy and paste my n(x) separately every time for different values of "a" and "b". I'm a beginner, help much appreciated.

## closed as off-topic by m_goldberg, Sjoerd C. de Vries, Simon Woods, Karsten 7., Mr.Wizard♦Feb 10 '15 at 7:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Sjoerd C. de Vries, Simon Woods, Karsten 7., Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

• You've many syntax errors. Please go back to the docs. – Dr. belisarius Feb 9 '15 at 14:10
• Your first and perhaps biggest problem is that Function[a, b] = f(x) is complete nonsense as far as Mathematica is concerned. f(x) is equivalent to f*x and you can't assign to Function[a, b] (and you'll get an error if you try). – m_goldberg Feb 9 '15 at 14:16
• I apparently let myself be seriously misunderstood. I was using symbolic language. Of course I didn't define the function as seen above. In this case f(x) is come variant of the hyperbolic cosine. I just wanted to make things easier by schematically naming the x-dependant function as f(x). – drabus Feb 9 '15 at 15:08
• The best symbolic language to use in this forum is Mathematica code. So, please edit your question accordingly. The error probably arises because you have x defined as a constant value. – Oleksandr R. Feb 9 '15 at 15:11
• Your use of Function is still not according to syntax. Please look it up in the documentation. – Sjoerd C. de Vries Feb 9 '15 at 18:15

## 2 Answers

Simplifying some of your expressions:

f[d_, σ_, x_] := Module[{h, ϵ0, m, e0, α, κ, β, ϕ},
h  = 6.62606957*10^(-34);
ϵ0 = 8.8541878176*10^(-12);
m  = 9.10938291*10^(-31);
e0 = 1.6*10^(-19);
α  = (3/d/e0)^(5/3) h^2 ϵ0/(5*(2 σ)^(1/3) m Pi^(2/3));
κ  = (5 α/3) 2^(-2/3);
β  = 4/(κ^(3/4) Sqrt@5);
ϕ  = Cosh[β (1/2 - x)]/(β Sinh[β/2]) - (1/β) Coth[β/2];
D[ϕ, {x, 2}]/2]

Plot[f[10^(-10), 0.01, x] /. x -> u, {u, 0, 1}]


I don't see how your definition works as written. I get

SetDelayed::write: Tag Function in Function[d_,σ_] is Protected

because Function is built-in syntax. Maybe you had the function called something else in your code and renamed it for posting here? If so, please don't do that; make sure the code you post actually works.

Anyway, you don't necessarily have to change your function, apart from naming it something else, like function. You can just do

Plot[function[10^(-10), 0.01], {x, 0, 1}, Evaluated -> True]


and it works.

The problem is that Plot works by substituting numerical values of x and evaluating the function to get the vertical coordinate. So if it sets x = 0.002, say, it then has to evaluate e = -D[ϕ, 0.002], and 0.002 "is not a valid variable" to differentiate with respect to.

With Evaluated -> True, Plot first evaluates the expression symbolically to give 0.995427 Cosh[0.331805 (0.5 - x)], after which substituting a number for x` is no problem.