0
$\begingroup$

I'm new to Mathematica and am running in to all sorts of silly difficulties with coding in it. I'm trying to calculate a function f[x, k], normalise it with another function n[k] and then calculate and plot a thrid function en[k] that depends on the first two. Unfortunately, Mathematica won't plot it, and I can't see what I did wrong. I'm sure it's a trivial mistake on my part and I'm sorry to have to ask. Here's my code:

L = 8;
f[x_, k_] := 
  Cos[Pi k/(2 L)]^2  Exp[(-(x - k)^2)/2] - Sin[Pi k/(2 L)]^2 (x - L) Exp[(-(x - k)^2)/2];
n[k_] := Integrate[f[u, k]^2, {u, -L, L}];
en[k_] := Integrate[f[v, k] (-f''[v, k] + (v - k)^2  f[v, k]), {v, -L, L}]/n[k];
Plot[en[k], {k, 0, L}]
$\endgroup$
3
  • 1
    $\begingroup$ I think you should specify the derivation variable in f''[v,k] $\endgroup$ Oct 24, 2013 at 23:11
  • $\begingroup$ Thanks, that did it. You deserve a kiss. $\endgroup$
    – Kris
    Oct 24, 2013 at 23:47
  • $\begingroup$ You don't mention the nominated kisser, but I'll take that as a compliment at any rate. $\endgroup$ Oct 24, 2013 at 23:57

1 Answer 1

2
$\begingroup$

First, as Belisarius points out, you need to indicate the variable for your second derivative. You also need to carry out your differentiation and integrations before you try to plot en[k] Also, please look at the help that is provided here, especially the discussions of the difference between = and :=.

L = 8;
f[x_, k_] :=
  Cos[Pi k/(2 L)]^2 Exp[(-(x - k)^2)/2] - Sin[Pi k/(2 L)]^2 (x - L) Exp[(-(x - k)^2)/2];
n[k_] = Integrate[f[u, k]^2, {u, -L, L}];
d2f[v_, k_] = D[f[v, k], {v, 2}];
en[k_] = Integrate[f[v, k] (d2f[v, k] + (v - k)^2 f[v, k]), {v, -L, L}]/n[k];
Plot[en[k], {k, 0, L}]

plot.png

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.