Math in Industry - Alexiades
Lab 1
Warmup programming and plotting
For each assignment, you should create a directory (folder) and work in it,
so all files pertaining to the assignment will be in one place.
A good name for this assignment may be: Lab1 .
Always read the entire assignment before you start doing it...
1. Write a code that evaluates f(x) = a−bx3 at N equidistant points in [0,1],
and outputs the pairs x , f(x) in a file named "values".
Ask the user to enter a,b,N, or read them from a data file.
Try a = −1. , 0. , 1 , b = 0.1 , 1. , 5. , N=20
(Start aquiring good programming habits: In your code
enter your name, date, title, a brief description of what it does,
document the main steps, etc )
For plotting, I strongly recommend gnuplot , see links on course page. Very simple to use and powerful.
2. a. Use gnuplot to display the graph in each case
gnuplot> plot "values" with points
Check it with (for example, a=1 , b=0.5) :
gnuplot> plot [0:1] 1-0.5*x**3 with lines
You can plot both your "values" and this curve on the same plot:
gnuplot> plot "values" with points, 1-0.5*x**3 with lines
In fact, you can enter formulas in gnuplot, like:
gnuplot> f(x)=a-b*x**3 ; a=1 ; b=0.5 ; plot [0:1] f(x) w lines
then only need to modify a, b and replot, like:
gnuplot> a=0 ; b=-5 ; replot
[Better yet, can put these in a script file, see examples at bottom,
... gnuplot is very convenient and versatile... ]
b. Play with N, to see how many points you need to "trace out the curve"
(minimum number needed to capture the shape of the curve with just the points).
Most everything you need to know is in the How-To and On-line Resources links
on the M518 web page. Check them out!
Once you get a good fit on screen, if you want you may save the plot
into a .png or .ps file
(see
gnuplot - How to print for how to print and view .ps ).
3. Now run your code with: a=3, b=2, N=10.
Create a plain text file named Lab1.txt containing the following:
Name Lab1.txt Date
============================================== (separator line)
- your code
============================================== (separator line)
- the input and output from it (ONLY for N=10 !).
============================================== (separator line)
- What value of N worked well in Part 2.b ?
4. Upload "Lab1.txt" on Canvas (under Lab1).
5. Get into good habits: clean up any junk files
keeping only the useful files (code, input, output, lab1.txt).
All done !
Impressive examples of gnuplot scripts: trochoids
spiro2.gp.txt ,
spiro3.gp.txt
Try them!
Also see:
gplot basics
gplot scripts