#08 (02/12/2024)

Solving a cubic equation

We want to find one of the roots of a cubic equation given by
x3 + x − 1 = 0
which is between 0 and 1 by an iterative method. Modify the equation above to
x = 1

1 + x2
Start with an appropriate initial value and do the iteration above until it is convergent. For example,

x0=0.5,     x1 = 1

1+0.52
=0.8,    x2 = 1

1+0.82
=0.609,    x3 = 1

1+0.6092
=0.728, …
The simplest (minimum) code may look like:
#include <stdio.h>

int main()
{
int i;
float x;

printf("Enter initial guess =");scanf("%f",&x);

for (i=0;i<100;i++)  /* insert your iteration code */

printf("%f\n",x);
return 0;
}

Larget integer in C

An integer in C (int) is represented by 4 bytes (1 byte = 8 bits). What is the largest integer that can be handled by C ?
Hint:

    (a) Conversion from binary to decimal:

    10112
    =
    1 ×23 + 0 ×22 + 1 ×21 + 1 ×20
    =
    8 + 0 + 2 + 1
    =
    1110.

    (b) If one bit is used as the sign, the maximum number using 4 bytes (32 bits) is
    n=1111111 11111111 11111111 111111112. (31 "1"s).
    Insteading of computing
    n = 1×230+1×229+1×228+ …+ 1×21+1×20,
    consider computing n+1 = 10000000 00000000 00000000 000000002,

    n
    =
    1×230+1×229+1×228+ …+ 1×21+1×20
    =
    (n+1) − 1
    =
    1×231−1
    =
    2147483647.

    (c) Exercise: If an integer is represented by 1 byte (8 bits), what is the maximum integer number with a sign ?

Plotting (GNUPlot)

gnuplot.gif
Download the binary GNUPlot (Version 4.4, only 1.2MB) for Windows. If you must have the latest version, download Version 6 (the current version) from here. Once downloaded, extract all the files into a subdirectory and run wgnuplot.exe from there.

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

An example of generating a data file from C and export the file to gnuplot to draw the graph.

#include <stdio.h>
#include <math.h>

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 thisprogram.c
$ a.out > junk.dat
$ gnuplot
gnuplot > plot "junk.dat" with lines
gnuplot > plot "junk.dat", 3*x+12

Transferring files between omega.uta.edu and local machine

Download WinSCP (latest here 6.1.2).
  1. New
  2. Hostname omega.uta.edu
  3. Username: NetID
  4. Password: your UTA password
  5. Login
WinSCP.jpg

From CMD line

If you know the name and the location of the file you want to transfer, you can do it directly.
  1. Win+R, cmd
  2. cd to_your_folder
  3. scp program.c abc1234@omega.uta.edu: (from a local machine to omega.uta.edu)
    or
  4. scp abs1234@omega.uta.edu:program.c . (from omega.uta.edu to the local machine)

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 (64 bit PC) or mergegif32.exe (32 bit old PC) 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



Footnotes:

1If your OS is x86 (32-bit) or other platforms, try compiling the source code yourself using gcc available here.


File translated from TEX by TTH, version 4.03.
On 13 Feb 2024, 21:38.