Part One: 1. Plotting (GNUPlot) The easiest way to draw graphs based on numerical data generated by your C program is to export the data to external plotting software. Excel is a business oriented...

We are working in Unix , C, and Octave


Part One: 1. Plotting (GNUPlot) The easiest way to draw graphs based on numerical data generated by your C program is to export the data to external plotting software. Excel is a business oriented spreadsheet program that may not be appropriate for fine-tuning the details of plotting for science/engineering (and it costs $$$). Freely available software, gnuplot, meets such requirements. Download the binary GnuPlot (Version 4.4) (1.2MB) for Windows. Most of Unix systems including already have gnuplot installed. The Lab PCs also have gnuplot installed. Mac version here (for Lion). Another Mac version here (for OS-X, you need to compile). Some of the commands that are often used: gnuplot > set title "My graph" gnuplot > plot x**3-x-1 gnuplot > plot sin(x) with dots (w d) gnuplot > plot sin(x) with impulse (w i) gnuplot > plot [-5:5] sin(x)/(x**2+1) gnuplot > plot [-pi:pi] sin(x), sin(2*x), sin(3*x) gnuplot > set xlabel "My x axis" gnuplot > set ylabel "My y axis" gnuplot > plot [-4:4] [0:10] x/exp(x) gnuplot > splot [-pi:pi] [-2*pi:3*pi] sin(x*y) gnuplot > plot [-4:4] [0:10] x/exp(x) gnuplot> set isosamples 100 gnuplot> set hidden3d gnuplot> set contour base gnuplot> splot [0:pi][0:pi] sin(x*y) gnuplot> gnuplot > quit Here is an example of generating a data file from C and export the file to gnuplot to draw the graph. #include #include int main() { int i; float x; for (i=0; i<10; i++)="" {="" x="0.1*i;" printf("%f="" %f\n",="" x="" ,="" sin(x));="" }="" return="" 0;="" }="" $="" gcc="" -lm="" thisproram.c="" $="" a.out=""> junk.dat $ gnuplot gnuplot > plot "junk.dat" with lines gnuplot > plot "junk.dat", 3*x+12 Saving GnuPlot graphs in GIF format/GIF animation For a graphic viewer, IrfanView is recommended. To create a GIF animation, download this tiny file, mergegif.exe (32bit) or mergegif64.exe (64bit) 1 Issue the following commands in GnuPlot to create three GIF files. cd "c:/tmp" set term gif size 320,240 set output "1.gif" plot sin(x) set output "2.gif" plot sin(2*x) set output "3.gif" plot sin(3*x) quit After that, open a DOS window and issue $ cd \tmp $ mergegif -l0 1.gif 2.gif 3.gif > animation.gif $ start animation.gif Creating GifAnimation from within GnuPlot cd "c:/tmp" set term gif animate set output "animation.gif" plot sin(x) plot sin(2*x) plot sin(3*x) quit 2. File handling (from DOS/Shell prompt) IO Redirection (Standard input/output redirection) Unix shells (csh/tcsh/bash on most of Unix platforms) and the DOS window have the capability of I/O redirection. Instead of entering data from the keyboard and displaying the computation result on the computer screen, it is possible to re-direct input/output to/from other devices such as files, printers etc.... Notion Meaning $program > filename output to file $program >> filename append output to file $program < filename get="" input="" from="" file="" the="" commands="" below="" save="" the="" output="" from a.out to="" a="" file result.dat.="" you="" can="" examine="" the="" contents="" of result.dat by="" the more command.="" $="" gcc="" junk.c="" $="" a.out=""> result.dat $ more result.dat If the program requires input from the keyboard and outputs the result to the screen, you can redirect them from/to files as $ gcc junk2.c $ a.out < data.dat=""> result.dat $ more result.dat Here the file data.dat contains the necessary data to be fed to the program. The following command (DOS window) creates a file filelist.dat that lists all the files in the current directory. c:\dir > filelist.dat File handling (politically correct way) To write/read files from a C program, the files must be opened first and must be closed after necessary operations are carried out. For opening/closing files, the functions, fopen and fclose with a special keyword FILE (note the upper case) must be used. #include int main() { FILE *fp; fp = fopen("filename","w"); /* write something on fp */ fclose(fp); return 0; } The function, fopen, takes a (append), w (write) or r (read) as possible arguments. #include int main() { FILE *fp; fp=fopen("junk.dat","w"); fprintf(fp,"Hello!\n"); fclose(fp); return 0; } #include int main() { FILE *fp; float a,b,c; fp=fopen("junk.dat","r"); fscanf(fp,"%f %f %f", &a, &b, &c); printf("%f %f %f", a,b,c); fclose(fp); return 0; } You can open multiple files for writing/reading: #include int main() { FILE *fp1, *fp2; float a,b,c; fp1=fopen("junk1.dat","w"); fp2=fopen("junk2.dat","w"); fprintf(fp1,"This is the first file.\n"); fprintf(fp2,"This is the second file.\n"); fclose(fp1); fclose(fp2); return 0; } 1. By reviewing the lab note, do the following: 1. Draw a graph of sin(x3) for - π <>< π. 1.="" draw="" a="" graph="" of="" (sin x,="" sin 2x,="" sin 3x)="" for="" 0=""><>< 2π together.="" 1.="" draw="" a="" 3-d="" plot="" of x*x +="" sin y="" 1.="" 1.="" write="" a="" c="" program="" to="" generate="" the="" following="" table="" of="" f(x)=" x - x3/6" + x5/120="" from x ="0" to x ="4.0" with="" an="" increment="" of="" 0.1,="" and="" store="" the="" result="" to="" a="" separate="" file, table.dat.="" 0.000000="" 0.000000="" 0.100000="" 0.099833="" ................="" ................="" 3.500000="" 0.730990="" 3.600000="" 0.862848="" 3.700000="" 1.036497="" 3.800000="" 1.257597="" 3.900000="" 1.532184="" 4.000000="" 1.866667="" 1.="" transfer="" the="" above="" date="" file="" from="" omega.uta.edu="" to="" a="" local="" folder.="" launch gnuplot to="" read="" the="" file, table.dat,="" and="" plot="" the="" data="" along="" with="" sin x.="" this="" way,="" you="" can="" find="" out="" how="" good="" approximating="" sin x by="" the="" first="" three="" terms="" of="" taylor="" series="" is.="" the="" graph="" should="" look="" like="" hint:="" 0.="" use winscp to="" transfer="" the="" file="" from="" your="" omega="" account="" to="" your="" home="" folder.="" 0.="" when="" you="" launch="" gnuplot="" on="" your="" local="" pc,="" you="" need="" to="" have="" the="" default="" directory="" changed="" to="" the="" directory="" where="" you="" stored table.dat.="" look="" for  and="" enter="" your="" home="" directory.="" 0.="" plot="" "junk.dat",="" x**3-x="" plots="" data="" from="" file="" along="" with x3- x.="" 1.="" (extra="" credit)="" make="" a="" gif="" animation="" file="" from="" the="" pictures="" below:="" 0.="" save="" each="" file="" to="" your="" network="" folder="" by="" right="" click,="" save="" picture="" as.="" 0.="" download mergegif.exe (23k)="" (x64="" version)="" and="" save="" the="" file="" to="" the="" same="" folder="" as="" above.="" 0.="" mergegif="" -5="" 01.gif="" 02.gif="" 03.gif="" 04.gif="" 05.gif=""> animation.gif 0. Try different delay time (-n). If your OS is other than win32, try compiling the source code yourself using gcc available at this site. Part Two: Problem 1. Solve the following differential equation y¢ = y,     y(0) = 1, by: 1. Exactly (analytically). 2. Euler's method. 3. Runge-Kutta method. Plot their results in a single graph in GnuPlot. Use 0≤t≤1.0 and h = 0.1. Use the following syntax in GnuPlot: plot "data1.txt", "data2.txt", exp(x) where "data1.txt" contains data from the Euler method, "data2.txt" contains data from the R-K method. As shown below, you can also plot their results in a single graph using Octave (see Pages 7-11 of Lecture notes Part IV in “Modules”), which is optional. i) Save data1.txt and data2.txt in working folder of Octave ii) Use the following codes in Command Window of Octave: x=[0:0.1:1] dataA = load ("data1.txt"); dataB = load ("data2.txt"); plot (dataA (:,1), dataA (:,2), dataB (:,1), dataB (:,2), x, exp(x)); Problem 2.  a). Numerically solve the following differential equations using the Euler method du dt = v (1) dv dt = - k v - u3 + B cos t, (2) with k = 0.1,     B = 11.0, and plot the result using GnuPlot. Change the parameters to k = 0.4,     B = 20.0 and show the graph as well. You can use the initial condition as u(0) = 1,     v(0) = 1. Also, use h = 0.01,     i = 10000. b). Solve Eqs. (1) and (2) using Octave, and plot the result using GnuPlot. Do you get similar results using Euler’s method and Octave? Problem 3. (a) Solve the following two simultaneous equations using Octave: æ ç ç ç è 1 4 8 1 ö ÷ ÷ ÷ ø æ ç ç ç è x y ö ÷ ÷ ÷ ø = æ ç ç ç è 4 7 ö ÷ ÷ ÷ ø (b) Calculate using quadv and trapz, respectively. Please submit your (interactive or a script m-file) codes and output of Octave. Problem 4. Provide the plots generated by the following highlighted commands: x=[0:0.1:10]; %initial value, increment, final value y=sin(x); plot(x, sin(x)); %calls software Gnuplot to plot x=linspace(0,2, 20) % between 0 and 2 with 20 divisions plot(x, sin(x)) %%%%%%%%%%%%%%%%%% x=[0: 0.2 : 10]; y=1/2 * sin(2*x) ./ x; xlabel('X-axis'); ylabel('sin(2x)/x'); plot(x, y); close; %%%%%%%%%%%%%%%%% t=[0: 0.02: 2*pi]; plot(cos(3*t), sin(2*t)) %%%%%%%%%%%%%%%%%%%% x=[0: 0.01: 2*pi]; y1=sin(x); y2=sin(2*x); y3=sin(3*x); plot(x, y1, x, y2,x, y3); %%%%%%%%%%%%%% xx=[-10:0.4:10]; yy=xx; [x,y]=meshgrid(xx,yy); z=(x .^2+y.^2).*sin(y)./y; mesh(x,y,z) surfc(x,y,z) contour(x,y,z) close Problem 5. i) Translate the following C code to equivalent Octave m-file, and ii). run this m-file in Octave. Please submit the m-file and its output. #include int main() { double x, y, z; int i,n; x=y=z=0.0; printf("Enter # of iteration = "); scanf("%d", &n); for (i=0; i #include double f(double x) {return 4.0/(1.0+x*x);} int main() { int i, n ; double a=0.0, b=1.0 , h, s1=0.0, s2=0.0, s3=0.0, x; printf("Enter number of partitions (must be even) = "); scanf("%d", &n) ; h = (b-a)/(2.0*n) ; s1 = (f(a)+ f(b)); for (i=1; i<2*n; i="i+2)" s2="s2" +="" f(a="" +="" i*h);="" for="" (i="2;"><2*n; i=i+2) s3 = s3 + f(a + i*h); printf("%lf\n", (h/3.0)*(s1+ 4.0*s2 + 2.0*s3)) ; return 0; } problem 7. translate the following c code to a fortran i="i+2)" s3="s3" +="" f(a="" +="" i*h);="" printf("%lf\n",="" (h/3.0)*(s1+="" 4.0*s2="" +="" 2.0*s3))="" ;="" return="" 0;="" }="" problem="" 7.="" translate="" the="" following="" c="" code="" to="" a="">
Nov 11, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here