Functions of continuous variables

Hello, there!
I want to plot a function p(s) \times s, being s \in \mathbb{R}. My code is:

Pkg.add("Plots")
using Plots

p(s) = s/2 * exp(-s^2/4)

plot(s,p(s))

The problem is that when I run it, I got the following error message:

>undefVarError: s is not defined.

How do I define s as a real variable? In Julia, it is not unnecessary to define the type of a variable?

Welcome!

You don’t have to define the type of a variable, but you still have to define its value somehow. Otherwise s is just a label with no meaning at all.

However, in your case Plots.jl already knows how to plot a scalar-valued function, and all you have to do is pass in the function itself:

plot(p)

out

You can also plot an anonymous function:

plot(s -> s/2 * exp(-s^2/4))
3 Likes

Thank you very much!