0
$\begingroup$

I want to plot the solution of a differential equation, which involves a piecewise function, the rectangle function UnitBox. As a result, the solution computed by mathematica is also a piecewise function.

a[z_, L_] := UnitBox[(z/L + 0.5)]
V''[z] + k^2 V[z] == 0.5 k^2 a[z, 1]^2
sol = DSolve[V''[z] + k^2 V[z] == 0.5 k^2 a[z, 1]^2, V[z], z]
V[z] /. sol[[1]] /. {k -> 1, C[1] -> 1, C[2] -> 2}
Plot[V[z], {z, -5, 5}]

But when I try to plot the solution it is not showing anything. In fact V[3] doesn't give me the value of the function. What have I done wrong and how to plot the solution?

$\endgroup$

1 Answer 1

2
$\begingroup$

You will probably see from Mathematica's Front End that the V[z] which you are attempting to plot has a blue color. This indicates that no values have been assigned to V[z] in the kernel. If there's nothing stored in V[z], it cannot be plotted. Use an intermediate symbol such as myFunc:

a[z_, L_] := UnitBox[(z/L + 0.5)]
V''[z] + k^2 V[z] == 0.5 k^2 a[z, 1]^2
sol = DSolve[V''[z] + k^2 V[z] == 0.5 k^2 a[z, 1]^2, V[z], z]
myFunc = V[z] /. sol[[1]] /. {k -> 1, C[1] -> 1, C[2] -> 2}
Plot[myFunc, {z, -5, 5}]

enter image description here

$\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.