Valentin Tihomirov
Valentin Tihomirov

Reputation: 1

How do I plot a function?

I have defined f1(x) = 10 - 9 * x. But, when I try plot [x=0:1] f1 I get undefined variable: f1 despite print f1(.5) gives me 5.5 and plot [x=0:1] 10 - 9 * x plots a nice graph. Why cannot you plot a function?

Upvotes: 0

Views: 233

Answers (1)

Matthew
Matthew

Reputation: 7590

You are not telling gnuplot how to compute the function in the plot command. Use

plot [x=0:1] f1(x)

When you don't include the (x) part, gnuplot assumes that you are referring to a variable (a constant) named f1, and thus throws the undefined variable error, as it can't find it.

In fact, you can have both a variable and a function with that same name (although I wouldn't recommend it - it's confusing), so gnuplot can't just assume that you want the function. Additionally, if it was a function of a different variable, or of more than one variable, it would have no way of guessing how to compute it during the plot command. Thus, you have to instruct gnuplot of how the function is computed.

See help functions for full details.

Upvotes: 1

Related Questions