CS 1713, Introduction to Computer Science Assignment 6, Spring 1998 Due March 5, 1998 Graphing a Function Write a C program which will sketch the graph of a function. Input: For the function your program should read an integer n from the terminal and draw the graph of the following function f(x) = (2/p)[sin(x) - (1/2)sin(2x) + (1/3)sin(3x) + . . . + (-1)n+1(1/n)sin(nx)], 0 £ x £ p. This is not as bad as it looks, and just requires a simple loop to compute. The series (called a Fourier series) converges to the line y = (1/p)x, for 0 £ x £ p. For n = 3, the series takes the form f(x) = (2/p)[sin(x) - (1/2)sin(2x) + (1/3)sin(3x)], 0 £ x £ p. You could initially work with n = 3, and eventually turn in a run for n =20. Output: The graph should have the positive y-axis displayed horizontally and the positive x- axis vertically (so that one gets the normal configuration by turning the printout 90 degrees counter-clockwise). Assume the printer uses 10 chars/inch horizontally and 6 chars/inch vertically. (Printers vary.) Suppose we decide to represent 1 unit on the y-axis by 5 inches or 50 characters, or 1/50 units per character. Then 1 unit on the x-axis should also be 5 inches or 5´6 = 30 lines, or 1/30 units per line. Thus the y-axis should use 61 characters to display y values from 0.0 to 1.2, while the x-axis uses 100 lines to display x values from 0.0 to 3.333. (The x values for successive lines are 0, 1/30, 2/30, 3/30, . . . , 100/30.) The points of the graph itself should be marked with a "+" as shown below. Both axes should have scaling marks exactly as shown. (The graph shown is only the first portion of what you should produce. Your graph will not look exactly like the one below, but it should be similar.) 0 0.2 0.4 0.6 0.8 1.0 1.2 |---------|---------|---------|---------|---------|---------|---> Y 0.0-* |* |* |* | + | + 0.2- + | + | + | + | + | + 0.4- + | + | + Notes: 1. The first x value is 0, and each subsequent x value is obtained by adding 1/30 to the old value. Each character along the y-axis represents a distance of 1/50. 2. To decide where to put the "+" character, just multiply the y = f(x) value by 50.0 and round to the nearest integer. Put "+" that many spaces over horizontally from the x-axis. Thus in outline, the basic part of the program to print just the graph looks like: for x values 0, 1/30, 2/30, 3/30, . . . , 100/30 { calculate j = (int) (50.0*f(x)); output j blanks, followed by a '+', followed by a newline; } 3. The calculation of f(x) must be done using a C function, with a value returned. C functions must be used to output the initial y-axis and to output a line. (The line output function must have an integer parameter giving the position for "+".) 4. (Clipping) In case the y value, when multipied by 50, gives a value outside the graph, i.e., y is either £ 0 or ³ 1.2, then you should plot a "*" either in character position 1 (if £ 0) or in position 61 (if ³ 1.2). Notice that on the example picture above, the initial numbers were so close to zero that, when truncated, they had to be clipped. Extras for experts: 1. This graph is particularly interesting for values of x near p. You could try a larger scale so that y values from 0.8 to 1.2 are plotted, using 60 character positions, or 0.4 inches = 60 chars, or 1 unit = 150 charcters , or 1 unit = 15 inches, or 1/150 units per character. Along the x-axis, 1 unit will be 15 inches, or one unit will be 90 lines. Thus the x values go in steps of 1/90. Plot only x values near p, say x from 2.6666 to 3.3333, or x values from 240/90 to 300/90 in steps of 1/90. Now to scale the y-value at each step, you must multiply by 150 and subtract 0.8*150 = 120. Try n = 25, n = 50, and n = 100. 2. There are various ways to draw nicer-looking graphs of this function. Many applications available on computers will draw graphs of functions. For example, the Mathematica program drew the following graph of the function of this assignment for n = 10. You could try out your program first with n = 3, then with n = 10 to see if it resembles the graph below. Finally run the program with n = 20 to hand in. (In the graph below, one unit on the x-axis is about half the size of one unit on the y-axis.) n = 10 n = 50 n = 100 f[x_,n_] := Block[{sum = 0.0}, For[i = 0, i < n, i++, sum += (-1)^i (1/(i+1)) N[Sin[(i+1)x],10] ] ; (2/Pi)sum ]