Part One: 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...


Part One:


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


 {


   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






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 froma.out to a fileresult.dat. You can examine the contents ofresult.dat by themore 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 <> result.dat


$ more result.dat





Here the filedata.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 andfclose with a special keywordFILE (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, takesa (append),w (write) orr (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:


Draw a graph of sin(x
3) for - πx


Draw a graph of (sinx, sin 2x, sin 3x) for 0
x


Draw a 3-D plot ofx*x + siny






    1. Write a C program to generate the following table of



















f(x) =x -x
3/6 +x
5/120




fromx = 0 tox = 4.0 with an increment of 0.1, and store the result to a separate file,table.dat.








0.000000 0.0000000.100000 0.099833................................ 3.500000 0.7309903.600000 0.8628483.700000 1.0364973.800000 1.2575973.900000 1.5321844.000000 1.866667




    1. Transfer the above date file from omega.uta.edu to a local folder. Launchgnuplot to read the file,table.dat, and plot the data along with sinx. This way, you can find out how good approximating sinx by the first three terms of Taylor series is. The graph should look like



Hint:




    • UseWinSCP to transfer the file from your omega account to your home folder.

    • When you launch GnuPlot on your local PC, you need to have the default directory changed to the directory where you storedtable.dat. Look for  and enter your home directory.










plot "junk.dat", x**3-x

plots data from file along withx
3-x.






(EXTRA CREDIT) Make a GIF animation file from the pictures below:




    • Save each file to your network folder by Right Click, Save Picture as.

    • Download mergegif.exe (23K) (x64 version) and save the file to the same folder as above.



mergegif -5 01.gif 02.gif 03.gif 04.gif 05.gif > animation.gif


    • 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.


Save data1.txt and data2.txt in working folder of Octave


 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











=











-kv -u
3 +B cost,




(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.


Solve the following two simultaneous equations using Octave:




























æ

ç

ç

ç

è






















1











  4











8











  1





ö

÷

÷

÷

ø



æ

ç

ç

ç

è























x












y





ö

÷

÷

÷

ø



=



æ

ç

ç

ç

è























4











7













ö

÷

÷

÷

ø




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


{


x = (10-y-2*z)/7;


y = (8-x-3*z)/8.0;


z = (6-2*x-3*y)/9.0;


}



printf("x = %lf, y= %lf, z=%lf\n", x,y,z);


return 0;


}





Note the following syntax:










fprintf('x = %f, y= %f, z=%f\n', x,y,z);






Problem 6. Translate the following C code to equivalent Octave (Matlab) m-files. You need to prepare two m-files, one for a function m-file (f.m) and the other for a script m-file (Simpson.m). Then, run simpson.m in Octave. Please submit the two-m files and the output of Simpson.m.











/* Simpson's rule */


#include


#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



 for (i=2; i



    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 code:









#include


#include



double f(double t, double y)


{return y;}



main()


{


double h=0.1, t, y, k1,k2,k3,k4;


int i;



/* initial value */


t=0.0; y=1.0;



for (i=0; i


{



 printf("t= %lf rk= %lf exact=%lf\n", t, y, exp(t));


 k1=h*f(t,y);


 k2=h*f(t+h/2, y+k1/2.0);


 k3=h*f(t+h/2, y+k2/2.0);


 k4=h*f(t+h, y+k3);


 y= y+(k1+2.0*k2+2.0*k3+k4)/6.0;


 t=t+h;


}


return 0;


}



Show your FORTRAN code and example run.







May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here