![]() |
Intro to Scientific Computing |
![]() |
If gnuplot is running on your computer, end it by typing
Now that it's running again, type:
gnuplot> plot exp(-x**2/20) * sin(x)You may want to turn on the zero axis (remember how?). Later we'll learn how to set that as the default behavior at startup.
The plot you just made is of the function
where you used the common exp() and sin() functions.
A complete list of functions is here:
For example, gnuplot will plot the two lowest Bessel functions,
J0(x) and J1(x). From these two, the
higher order Bessel functions can be constructed. Bessel functions are
used in the solution of certain differential equations, particularly in
cylindrical coordinates, and turn up often in the physics of musical
instruments.
You can learn more about Bessel functions
here
Gnuplot also knows about imaginary numbers and certain
constants like
.
Try this:
gnuplot> print pi
gnuplot> w = 5.8
gnuplot> v = 3.0
gnuplot> print w/v
gnuplot> a = {2.0, 3.0}
gnuplot> b = {1.0, -2.0}
In the first line, you asked gnuplot to print the value of "pi". You can also use pi as a value in a range limit, as in plot [-pi:pi].
In the last two lines you defined two variables, a and b, as complex numbers. The first number in the bracket is the real part and the second of course, is the imaginary part.
What is the product ab? You should remember from junior high:
a b = (areal + i aimag)(breal + i bimag)
= areal breal - aimag bimag + i (areal bimag + aimag breal)
= 2*1 - 3*(-2) + i*(2*(-2) + 3*1)
= 8 - i
Now do:
Does gnuplot confirm your complex arithmetic?
If you are a bit rusty on complex numbers and how to manipulate them,
here are a couple links to brush up.
Since gnuplot has many builtin math functions, will store things in
variables, and will do calculations and print the result, you can use
it as a simple scientific calculator. Often, when I'm working on my
computer and don't want to grab my calculator (which I actually don't
enjoy using that much), I just fire up gnuplot and do a little
calculation there. Here's an example:
Suppose I want to compute the following quantity
when r = 2.5 and R = 3.9
You could simply start gnuplot and enter the following (
Much easier!. Really. Try it--have your mom type in the above while you compute the same thing on your caculator. I bet your mom wins.
Plot the two equations you used on the previous webpage,
Set the x range to [-0.5:2].
Turn on the zeroaxis.
Notice that 2x2 - 3x + 1 factors into
2(x - 0.5)(x - 1).
Thus the exression 2x2 - 3x + 1 has roots at x = 0.5 and x = 1. (a root is where the expression is equal to zero)
Is this corroborated by your plot? (Did you remember what a root of an expression is? [
In other words, you can see on your plot the places where the equation
2x2 - 3x + 1 = x
This equation is slightly more non-trivial to solve; you have to use
the quadratic formula, and the solutions are
Numerically, these solutions are at
x = 1.70711 and x = 0.29289
You can get a rough anwser by using the mouse. As you move the mouse
cursor around in the plot area, notice that the x, y values
of where the mouse cursor is, are printed in the bottom left corner of
the gnuplot window. This can give you a pretty good idea of the value
of something in your graph.
Here's another method.
We can zoom in and find where exactly the intersection lies.
Try the same thing by narrowing the x range around the other root.
What about a situation where you don't know the solution of the
equation by analytic (like the quadratic formula) methods? In this
case the only way to get the result is to use numerical methods.
This means that we use a computer to find an approximate (and hopefully very
accurate) solution.
Homework 5 is here
A simple calculator

gnuplot> r = 2.5
gnuplot> R = 3.9
gnuplot> result = 4*pi**2 * r**2/sqrt(R**2 - r**2)
gnuplot> print result
82.4300852943585
gnuplot>
Fun with functions
If it's not already running, start gnuplot.
x and 2x2 -3x + 1.
2x2 - 3x + 1 = 0
is satisfied (and you get the values: 0.5 and 1.0).
Now, the x values where the line intersects the parabola are given by the solutions to the equation:
Does this look correct from your graph?
If
you successively narrow the plot range, you can "zero in" in the
intersection point. Do the following (use the UP arrow to recall each
previous line):
gnuplot> plot [1.5:2] x, 2*x**2 - 3*x + 1
gnuplot> plot [1.7:1.71] x, 2*x**2 - 3*x + 1
gnuplot> plot [1.706:1.708] x, 2*x**2 - 3*x + 1
gnuplot> plot [1.707:1.7072] x, 2*x**2 - 3*x + 1
gnuplot> plot [1.7071:1.70712] x, 2*x**2 - 3*x + 1
Notice how the successive narrowing of the x range allows us to
find the x value of the intersection by reading it from the
x axis.
This probably seems pretty simple, and not very useful since we
already know the solution of the above equation from the quadratic formula.
Homework 5
Your third homework is to find the energy output of the Sun and the
solution of an "algebraically unsolvable" equation.