0% found this document useful (0 votes)
17 views

Presentation Slides

The document discusses different types of programming languages and their classification as high-level or low-level languages. It also provides examples of using various operators, control structures, and input/output formatting in C/C++ programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Presentation Slides

The document discusses different types of programming languages and their classification as high-level or low-level languages. It also provides examples of using various operators, control structures, and input/output formatting in C/C++ programs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 156

Programación C/C++

Prof. Sergio Miranda-Aranguren

October 30, 2022


Classification of Programming Languages

A programming language is a formal language, which comprises a


set of instructions that produce various kinds of output.

Programming languages are used in computer programming to


implement specific algorithms. Most programming languages
consist of instructions for computers.

A computer program is usually written by a computer


programmer and can be can be written in either high or low-level
languages, depending on the task and the hardware being used.

2 / 156
High level language

3 / 156
Low level language

4 / 156
Write a program to read a real number

1 #include <stdio.h>
2 #include <math.h>
3 int main()
4 {
5 float x;
6 printf("Enter a number ="); scanf("%f", &x);
7 printf("x= %f exp(x)=%f\n",x, exp(x));
8 return 0;
9 }

You have to use the -lm option when compiling:


$ gcc program.c -lm -o ejecutable.o
$ ./ejecutable.o

5 / 156
Operators

6 / 156
Increment-Decrement Operators

7 / 156
Aritmetic Operators

8 / 156
Logical Operators

9 / 156
Logical Operators Tableu

10 / 156
Relational Operators

11 / 156
Assigment Operators

12 / 156
Conditional Operators

13 / 156
Bitwise Operators

Bitwise AND operator &

1 #include<stdio.h>
2 int main ( )
3 {
4 int a = 12, b = 25;
5 printf("Output = %d", a&b);
6 return 0;
7 }

14 / 156
Bitwise Operators

Bitwise OR operator | Bitwise XOR operator ∧

1 #include <stdio.h> 1 #include <stdio.h>


2 int main() 2 int main()
3 { 3 {
4 int a = 12, b = 25; 4 int a = 12, b = 25;
5 printf("Output = %d", a|b); 5 printf("Output = %d", a^b);
6 return 0; 6 return 0;
7 } 7 }

15 / 156
Bitwise Operators

Right Shift Operator ≫


1 #include <stdio.h>
2 int main()
3 {
4 int num=212;
5 printf("Right shift by %d: %d\n", 4, num
>>4);
6 printf("Left shift by %d: %d\n", 4, num
<<4);
7 return 0;
Left Shift Operator ≪ 8 }

16 / 156
Misc Operators

17 / 156
Misc Operators Example

The following program demonstrate the use of & , ∗, and sizeof() operators:
1 #include<stdio.h>
2 int main ( )
3 {
4 int x = 10;
5 int ∗ptr = &x;
6 printf ( "Value of x = %d\n ",x );
7 printf ( "Address of x = %p\n", &x );
8 printf ( "Address of x = %p\n", ptr );
9 printf ( "Value of x = %d\n", ∗ptr );
10 printf ( "Size of the x = %ld ( in Bytes )\n", sizeof( x ) );
11 return 0;
12 }

18 / 156
Control Statments

19 / 156
IF Statment

The following program tells whether an integer entered from the keyboard
is between 1 and 100 or not:
1 #include <stdio.h>
2 int main()
3 {
4 int i;
5 printf("Enter an integer = ");
6 scanf("%d",&i);
7 if (i>1 && i<100){
8 printf("The number is between 1 and 100.\n"); }
9 else{
10 printf("The number is not in that range.\n"); }
11 return 0;
12 }

20 / 156
FOR Statment Example 1

for (initial value; condition ; counter increment){


statement1; statment2; }

The following program prints 0–9:

1 #include <stdio.h>
2 int main()
3 {
4 int i;
5 for (i=0; i< 10; i++) printf("i=%d\n",i);
6 return 0;
7 }

21 / 156
FOR Statment Example 2

The following program approximate ln 2.

It is known that
1 1 1 1
1− + − + − · · · = ln 2 (1)
2 3 4 5
Equation (1) can be written as:
∞ (−1)i+1
X
= ln 2
i=1
i

1 #include <stdio.h>
2 #include <math.h>
3 int main()
4 {
5 int i,n;
6 float sum=0.0;
7 printf("Enter # of iterations = ");
8 scanf("%d", &n);
9 for (i=1;i<=;i++){
10 sum = sum + pow(−1, i+1)/i; }
11 printf("Approximation of ln(2)= %f. ", sum);
12 printf("Exact value of ln(2)= %f.\n", log(2));
13 return 0;
14 }

22 / 156
FOR Statment Example 3

We want to find one of the roots of a cubic equation defined by

x3 + x − 1 = 0 (2)

which is between 0 and 1.

We can solve ussing an iterative method, modifying Eq. (2) to be read as:
1
x= .
1 + x2
The following program uses x = 1 as initial guess:
1 #include <stdio.h>
2 int main()
3 {
4 int i,n;
5 float x=1.0;
6 printf("Enter # of iterations = ");
7 scanf("%d", &n);
8 for (i=1;i<=n;i++){
9 x = 1/(1+x∗x); }
10 printf("Iteration #=%d, x=%f.\n", i, x);
11 return 0;
12 }

23 / 156
WHILE Statment

A while statement executes the statement(s) that follows while the


condition is true.

The following program outputs 10:


1 #include <stdio.h>
2 int main()
3 {
4 int i=0;
5 while (i<10){
6 i++; }
7 printf("%d\n",i);
8 return 0;
9 }

24 / 156
DO WHILE Statment

The do while loop is similar to the while loop except that the test occurs at
the end of the loop body. This guarantees that the loop is executed at least
once before continuing.

The following program keeps prompting until the user enters 0 or 1:


1 #include <stdio.h>
2 int main()
3 {
4 int input_value;
5 do
6 { printf("Enter 1 for yes, 0 for no :");
7 scanf("%d", &input_value);
8 } while (input_value != 1 && input_value != 0);
9 return 0;
10 }

25 / 156
SWITCH Statment

A switch statement can branch out to different tasks depending on the


value of the variable used.

You can achieve the same result using multiple if statements but using
switch simplifies the program flow.

The following program checks to see if the value entered is either 0 or 1


and if otherwise it prints the number is neither 0 nor 1. :
1 #include <stdio.h>
2 int main()
3 {
4 int i;
5 start:
6 printf("Enter an integer="); scanf("%d", &i);
7 switch(i)
8 {
9 case 0: printf("a is 0\n");break;
10 case 1: printf("a is 1\n");break;
11 default: printf("the number is neither 0 nor 1\n");goto start;
12 }
13 return 0;
14 }

26 / 156
MISCELLANEOUS REMARKS exit infinite loop

From time to time, the program you wrote may be trapped in an infinite
loop and nothing can be done except for closing the window or shutting
down the machine.

The following program generates an infinite loop:


1 #include <stdio.h>
2 int main()
3 {
4 int i;
5 for (i=1; i>0; i++) printf("loop");
6 return 0;
7 }

To exit from the infinite loop, enter control-C.

27 / 156
MISCELLANEOUS REMARKS output formatting

It is possible to modify the appearance of the output from the printf() .


The following program shows format control codes used in the printf() :
1 #include <stdio.h>
2 #include <math.h>
3 #define _USE_MATH_DEFINES
4 #define PI 3.14159265358979323846
5 int main()
6 {
7 float a=3.14;
8 printf("%f\n",a);
9 printf("%10f\n",a);
10 printf("%20f\n",a);
11 printf("%30f\n",a);
12 printf("%10.3f\n",a);
13 printf("%10.4f\n",a);
14 printf("%10.5f\n",a);
15 printf("%10.6f\n",a);
16 printf("%10.9lf\n",M_PI);
17 printf("%10.9lf\n",PI);
18 return 0;
19 }

The format, 10.6f means that 10 spaces from the beginning of the line are reserved
and the float variable is printed with 6 decimal places right justified. This formatting
option is purely cosmetic and does not change the actual value of the variable.
28 / 156
MISCELLANEOUS REMARKS return 0

Why does the return0; in int main() have to return 0?

Returning 0 is not absolutely necessary.

In fact, return −1; or return 2022; works just fine.

However, by returning 0 to the operating system when int main() exits, the
operating system knows that the process finished normally and continues
to run accordingly.

It does the operating system a favor by telling the operating system that it
does not have to bother to do anything special.

29 / 156
HOMEWORK 01

Assigment: write a C program that interactively reads the three


coefficients of a quadratic equation and compute the two roots.

The program must alert if there is no real root and compute the real or
complex roots in any case.

The quadratic equation is given by:

ax2 + bx + c = 0

and the two roots are expressed as:


p
−b ± b2 − 4ac
x=
2a

Note: sqrt is available in <math.h>. You need to compile the program with
the −lm option, i.e.,

$ gcc program.c -lm -o ejecutable.o


$ ./ejecutable.o

30 / 156
HOMEWORK 02a

Assigment: write a C program that Then compute:


interactively reads the four
coefficients of a cubic equation and a2 − 3b 2a3 − 9ab + 27c
Q≡ R≡
compute the three roots. 9 54
The program must alert if there is
three real roots or one real and two If Q and R are real (always true
complex roots and compute all when a; b; c are real) and R2 < Q3 ,
roots in any case. then the cubic equation has three
real roots. Find them by computing
The cubic equation is given by:  Æ 
θ = arccos R/ Q3
a3 x3 + a2 x2 + a1 x + a0 = 0
in terms of which the three roots
and can be expressed as: are:
x3 + ax2 + bx + c = 0 θ a
 
p
x1 = −2 Q cos −
where: 3 3
θ + 2π a
 
p
a = a2 / a3 , b = a1 / a3 and c = a0 / a3 . x2 = −2 Q cos −
3 3
θ − 2π a
 
p
x3 = −2 Q cos −
3 3 31 / 156
HOMEWORK 02b

To compute the complex roots, proceed as follow, first, if Q and R are both
real, define the variable:
h Æ i1/ 3
A = −sign(R) |R| + R2 − Q3

where the positive square root is assumed.

Next compute ¨
Q/ A if A ̸= 0
B=
0 if A = 0
in terms of which the three roots are:
a
x1 = (A + B) − real root
3
p
1 a 3
x2 = − (A + B) − + i (A − B) complex root
2 3 2
p
1 a 3
x3 = − (A + B) − − i (A − B) complex root
2 3 2

Deducción fórmulas de Cardano-Ferrari


32 / 156
Definition of function in C

The concept of functions in C is important as after all a C program is


defined as a set of functions.

You have already seen an example of C function, main() , which is always


executed first. That is the only special property with main() , which means
that any function in C must follow the same syntax as main() , i.e.,
declaration of the type argument(s) within the parentheses (leave it blank
if none), statements within { and } and return value as the last line in main() .

If the function does not need any return value, use void as the type of
return value.

The following program shows an example of the type, void.

1 #include <stdio.h>
2 void write()
3 {
4 printf("Hello, World!\n");
5 }
6 int main()
7 {
8 write();
9 return 0;
10 }

33 / 156
Function in C Example 2

The following program computes the function sign(x), for any real number
x.
1 #include <stdio.h>
2 int sign(float v)
3 {
4 if (v < 0) return −1;
5 if (v > 0) return 1;
6 return 0;
7 }
8 int main()
9 {
10 float x;
11 printf("\n Enter a number="); scanf("%f", &x);
12 printf("\n The sign function for number x=%f\n", x);
13 printf("\t is: \t sign(%f)=%d\n\n", x, sign(x));
14 return 0;
15 }

34 / 156
Locality of variables within a function

Variables used within a function are local , i.e., they do not retain the
values outside the function.
In the following program, the variable name, sum, is used for both f() and main() yet
sum used within f() does not propagate outside the function f() .

1 #include <stdio.h>
2 int f(int n)
3 {
4 int i,sum=0;
5 for (i=1;i<= n;i++) sum=sum+i;
6 return sum;
7 }
8 int main()
9 {
10 int i, n, sum=0;
11 printf("Enter the number of terms for the sum\n");
12 scanf("%d",&n);
13 for (i=1;i <= n;i++) sum=sum+i∗i;
14 printf("\n ================ The sums are ================\n");
15 printf("\n sum provided for main() function is:\n"); printf("\t sum = sum+i^2 = %d\n", sum);
16 printf(" sum provided for f() function is:\n"); printf("\t sum = sum + i = %d\n", f(n));
17 printf("\n ===============================================\n");
18 return 0;
19 }

The printed value of sum is the sum defined in main() even though a variable of the
same name is returned in the function, f().
35 / 156
Recursivity of functions

C functions can be used and defined recursively, which means that a C


function can call its own within the function. Using the recursive algorithm,
a program can be written compactly and efficiently.

Example 1: Fibonacci numbers


The Fibonacci numbers, an , are defined as,

an = an−1 + an−2 . where a1 = 1 and a2 = 1 (3)

In this way, starting with a1 and a2 , an for any number of n can be


computed by repeatedly applying Eq.(3).

36 / 156
The following program, determine the Fibonacci number, ussing recursivity
functions.
1 #include <stdio.h>
2 int fibo(int n)
3 {
4 if (n==1) return 1;
5 if (n==2) return 1;
6 return fibo(n−1) + fibo(n−2);
7 }
8 int main()
9 {
10 int i;
11 printf("Enter n = "); scanf("%d", &i);
12 printf("%d\n", fibo(i));
13 return 0;
14 }

37 / 156
Example 2:PnThe summation function
Compute i=1 i = 1 + 2 + 3 + 4 + · · · + n using the recursive algorithm.
If we define
sum(n) ≡ 1 + 2 + 3 + 4 + · · · + n,
the following relation holds:

sum(n) = sum(n − 1) + n, sum(0) = 0.


Pn
Using this recursive property, the following program can compute i=1
i,
without using the for statement.
1 #include <stdio.h>
2 int sum(int n)
3 {
4 if (n==0) return 0;
5 return n + sum(n−1);
6 }
7 int main()
8 {
9 int n;
10 printf("Enter n = ");
11 scanf("%d", &n);
12 printf("1+2+....+%d =%d \n", n, sum(n));
13 return 0;
14 }

38 / 156
RANDOM NUMBERS, RAND()

Random numbers can be generated by using rand() in <stdlib.h>.

The following program prints random numbers 10 times:


1 #include <stdio.h>
2 #include <stdlib.h>
3 int main()
4 {
5 int i;
6 for (i=0; i < 10; i++)
7 printf("%d\n", rand());
8 printf("\nMAX = %d\n", RAND_MAX);
9 return 0;
10 }

The function, rand() , returns an integer between 0 and RAND_MAX (system


dependent) which is defined in the header file, <stdlib.h>.

39 / 156
RANDOM NUMBERS, SRAND()

However, if we use rand() the same numbers are output again and again
each time the program is run. In order to generate a different sequence of
random numbers each time the program is running, we have to invoke
srand() function available in <stdlib.h> and must be used in conjunction with
rand().
1 #include <stdio.h>
2 #include <stdlib.h>
3 int main()
4 {
5 int i;
6 printf("Enter seed integer = ");
7 scanf("%d", &i);
8 srand(i);
9 printf("%d\n", rand());
10 return 0;
11 }

In the program above, srand(i) takes a seed number i and generates a


random number based on the value of i.

40 / 156
RANDOM NUMBERS, TIME()

Using the time() function defined in <time.h>, you can generate a different
seed number every time.

The function, time() , with the argument NULL returns the elapsed time since
00 : 00 : 00 GMT, January 1, 1970, measured in seconds. (This is the
birthday of UNIX!)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 int main()
5 {
6 srand(time(NULL));
7 printf("%d\n", rand());
8 return 0;
9 }

41 / 156
Generating Random Numbers in Various Ranges

If floating random numbers between 0 and 1 are desired instead of


between 0 and RAND_MAX , the following program is used:
1 include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 int main()
5 {
6 int i;
7 srand(time(NULL));
8 for (i=0; i< 10; i++) printf("%f\n", 1.0∗rand()/RAND_MAX);
9 return 0;
10 }

The following list shows examples of various ranges generated by rand():


1. rand() returns an integer between 0 and RAND_MAX.
2. 1.0∗rand()/RAND_MAX returns a floating number between 0 and 1.
3. 5.0∗rand()/RAND_MAX returns a floating number between 0 and 5.
4. 10.0∗rand()/RAND_MAX−5 returns a floating number between −5 and 5.
5. rand()%7 returns an integer of 0, 1, 2, 3, 4, 5, or 6.
6. rand()%7+10 returns an integer of 10, 11, 12, 13, 14, 15, or 16.

42 / 156
HOMEWORK 03: MONTE CARLO INTEGRATION

One application of simulation is Monte Carlo integration, where random


number generator are used to obtain an approximation to an integral.
Assigment: Build a Monte Carlo Integrator, to use with different functions:

Algorithm 1 Monte Carlo Integrator


1: procedure MonteCarlo(a, b, h, n)
2: Enter lower and upper limits:
(a, b)
3: Enter height of rectangle: h = d− c
4: Enter the number of trials: n
5: Declare x and y coordinates
6: Declare a counter variable count
7: Then generate random x and y
8: for i ≤ n do
9: x = rand() * fabs(b-a) + a
10: y = rand() * h
11: if y ≤ f (x) then
12: ++count
13: end if
14: end for
15: result = (b-a) * h * count/n
16: end procedure

43 / 156
DEFINITION OF ARRAYS
Vectors and matrices in linear algebra can be implemented in C as arrays.
An array in C can represent many elements by a single variable name.
An array is a variable which can represent multiple elements such as numbers and
characters.
The following program defines arrays, a, b, c , which represents three elements. The
values, 1.0, 2.0, and 3.0, are assigned to the first element, the second element and
the third element of a, b, c, respectively.
1 #include <stdio.h>
2 #define N 3
3 int main()
4 {
5 float a[3];
6 float b[N]={1.0, 2.0, 3.0};
7 float c[]={1.0, 2.0, 3.0};
8 float sum_a=0.0, sum_b=0.0, sum_c=0.0;
9 int i;
10 a[0]=1.0; a[1]=2.0; a[2]=3.0;
11
12 for (i=0;i <3;i++) sum_a = sum_a + a[i];
13 for (i=0;i <3;i++) sum_b = sum_b + b[i];
14 for (i=0;i <3;i++) sum_c = sum_c + c[i];
15
16 printf("\n The sum array a[] is = %f.\n", sum_a);
17 printf("\n The sum array b[] is = %f.\n", sum_b);
18 printf("\n The sum array c[] is = %f.\n", sum_c);
19 return 0;
20 } 44 / 156
MULTI-DIMENSIONAL ARRAYS

Arrays can be nested, i.e., they can take more than 1 indices.

Multidimensional arrays can represent matrices in linear algebra.

For example, the components of a 2 × 5 matrix, a , can be represented in C


as:

Note that the index begins with 0, not 1.

45 / 156
MULTI-DIMENSIONAL ARRAYS 2

The following program defines a 2 × 5 matrix, mat , given as:

and prints all the elements using double indices, i and j.


1 #include <stdio.h>
2 #define COL 5
3 #define ROW 2
4 int main()
5 {
6 int i,j;
7 float mat[ROW][COL]={{1.0 ,2.0 ,3.0, 4.0 ,5.0},{6.0, 7.0, 8.0, 9.0, 10.0}};
8 for (i=0;i<ROW;i++){
9 for (j=0;j<COL;j++){
10 printf("%f ", mat[i][j]);
11 }
12 printf("\n");
13 }
14 return 0;
15 }

46 / 156
STANDARD INPUT/OUTPUT REDIRECTION

Instead of entering data from the keyboard and displaying the output on
the computer screen, it is possible to re-direct input/output to/from other
devices such as files, printers, etc.
I/O redirections

If a.out alone is entered from the keyboard, all the output from a.out is
displayed on the screen. However, with a.out > result.dat, the output is saved
to an external file, result.dat , and nothing is shown on the screen.
$ gcc program.c -lm -o ejecutable.o
$ ./ejecutable.o > result.dat
If the program requires that the input must come from an existing external
file (data.dat) and the output must be saved to another external file
(result.dat), you can have both directions on the same line as:
$ gcc program.c -lm -o ejecutable.o
$ ./ejecutable.o < data.dat > result.dat
47 / 156
FILE HANDLING (FROM WITHIN A PROGRAM)

I/O redirection is operating system dependent (UNIX and DOS) and only
available when the C program is run from a command line.

To write/read an external file from within a C program, the file must be opened first
and must be closed after necessary operations on the file are done.

For opening/closing a file, the functions, fopen() and fclose() , with a special keyword
FILE (note the upper case) must be used.

Use the following syntax. The file variable, fp, is a pointer. This is a FILE pointer that
keeps track of the memory location of the file.

The function, fopen() , takes a (append), w (write), or r (read) as a possible argument.


The following program opens an external file, data.dat , for writing and writes
Hello, World! to the file.
1 #include <stdio.h>
2 int main()
3 {
4 FILE ∗fp;
5 fp=fopen("data.dat","w");
6 fprintf(fp,"Hello, World!\n");
7 fclose(fp);
8 return 0;
9 }
48 / 156
FILE HANDLING (FROM WITHIN A PROGRAM) 2

The following program reads three floating numbers separated by a space


from an external file, data.dat, and prints out their values on the screen:
1 #include <stdio.h>
2 int main()
3 {
4 FILE ∗fp;
5 float a,b,c;
6 fp=fopen("data.dat","r");
7 fscanf(fp,"%f %f %f", &a, &b, &c);
8 printf("%f %f %f", a,b,c);
9 fclose(fp);
10 return 0;
11 }

Multiple files can be opened for writing/reading as:


1 #include <stdio.h>
2 int main()
3 {
4 FILE ∗fp1, ∗fp2;
5 fp1=fopen("data1.dat","w");
6 fp2=fopen("data2.dat","w");
7 fprintf(fp1,"This is the first file.\n");
8 fprintf(fp2,"This is the second file.\n");
9 fclose(fp1); fclose(fp2);
10 return 0;
11 }

49 / 156
LINEAR INTERPOLATION

A common task in science is to formulate mathematical models to describe


the behavior of physical systems. The most common model is based on
the assumption of a linear relationship between x and y of the form:

y = mx + b, (4)

where b is the intercept with the y axis and m is the slop of the line
passing through the N data points (x1 , y1 ), (x2 , y2 ), (x3 , y3 ), · · · , (xN , yN ).
The values of m and b are determined so that the stright line passes
through the data points with less error.

50 / 156
LINEAR INTERPOLATION 2

The error is defined as the difference between the measured value and
predicted value. Since this value can be either positive or negative, it is
necessary to make it positive-definite by squaring the nominal value.
Usually this precedure is named the chi-square method. Therefore, the
total error,χ2 , is the sum of the square of the difference at each point
defined as:
XN
χ2 = (mxi + b − yi )2 (5)
i=1

Choose m and b so that χ2 be minimized. This can be achieved by partially


differentiating χ2 in Eq.(5) with respect to both m and b and set them to be
0 as:
∂χ2 ∂χ2
= 0, =0
∂m ∂b

51 / 156
LINEAR INTERPOLATION 3

This yields a set of two simultaneous equations for m and b as:


N
X
2 (mxi + b − yi )xi = 0
i=1
N
X
2 (mxi + b − yi )(+1) = 0
i=1

or
! ! !
N
X N
X N
X
xi2 m + xi b = xi yi
i=1 i=1 i=1
! ! !
XN XN XN
xi m + 1 b= yi
i=1 i=1 i=1

52 / 156
HOMEWORK 04a: LINEAR INTERPOLATION 3

Assigment: Show that the solution of the above simultaneous equations,


can be found ussing Cramer’s rule, to obtain:

N N N N
xi2
P P P P
xi yi xi xi yi
i=1 i=1 i=1 i=1

N
P N
P N
P
yi N xi yi
i=1 i=1 i=1
m= b= (6)
N N N N
xi2 xi2
P P P P
xi xi
i=1 i=1 i=1 i=1

N
P N
P
xi N xi N
i=1 i=1

53 / 156
LINEAR INTERPOLATION 4

There are several indicators of how well the data can be represented by a
stright line. One indicator is the correlation coefficient, wich can be
calculated from:
N
P
(xi − x̄)(yi − ȳ)
i=1
r=v v (7)
t N t N
uP uP
(xi − x̄)2 (yi − ȳ)2
i=1 i=1

with x̄ and ȳ the avarage value of xi and yi data points. The value of r
ranges from −1 to 1, but usually the value of r 2 is reported.

The correlation coefficient indicates the strength of the linear relationship


between x and y. A value of r 2 close to +1 indicates that there is a strong
linear relationship between x and y. Avalues of r 2 close to 0 indicates that
there is no relationship between x and y or that this relationship is
nonlinear.

54 / 156
READ VALUES FROM TWO COLUMNS

The following program shows how to read and write from/to a data file with
two columns.
1 #include <stdio.h>
2 #define N 10
3 int main()
4 {
5 int i;
6 float x[N+1], y[N+1];
7 float xi, yi;
8 FILE ∗fpin,∗fpout;
9
10 fpin=fopen("data.dat","r");
11 fpout=fopen("results.dat","w");
12
13 for (i=1;i<= N; i++){
14 fscanf(fpin, "%f\t %f\n",&xi,&yi);
15 x[i]=xi;
16 y[i]=yi;
17 fprintf(fpout, "%f\t %f\n",y[i],x[i]);
18 }
19
20 printf("\n read and write ready, have a good day! \n \n");
21
22 fclose(fpin);
23 fclose(fpout);
24
25 return 0;
26 }
55 / 156
HOMEWORK 04b: LINEAR INTERPOLATION 5

Assigment: Build a subroutine that makes a linear interpolation of the data


supply by your professor. This subroutine have to:
1. read data from a a file,
2. calculate the slope m and intercept point b of the linear regression,
using Ec.(6),
3. determine the correlation factor r 2 using Ecu.(7).
4. and write this results in a file, in the form of Ec.(4).

56 / 156
POINTERS

The concept of a pointer in C is probably the most challenging subject for a


C learner. However, there are only two new operators ( & and ∗ ) that need
to be learned in order to understand the pointer.

Ussually the only instances that a pointer is needed in numerical analysis


are:
1. working on matrices and vectors in linear algebra
2. or using a variable that is substituted for a function

Pointers are closely related to the hardware of a computer that runs a C


program. When a C compiler reads a source code, the compiler maps each
variable defined in the code to an appropriate location in RAM that holds
the value of that variable.

57 / 156
ADDRESS OPERATOR & AND DEREFERENCING OPERATOR ∗

1 #include <stdio.h>
2 int main()
3 {
4 float a=20.0;
5 float ∗pa;
6 pa=&a;
7 printf("address pointer pa = %p\n", pa);
8 printf("address pointer &a = %p\n", &a);
9 printf("value pointer ∗pa = %f\n",∗pa);
10 printf("value variable a = %f\n", a);
11 return 0;
12 }

In the program above, float ∗pa declares that pa is a pointer pointing to


variable a. The pa=&a statement means that the address ( &) of a is
assigned to pointer pa.

In the program above, ∗ before a pointer variable dereferencing the value


of a and works the same way as using a directly. Therefore, if pa is a pointer
pointing to a variable a, the use of a and ∗pa within a program is identical.

58 / 156
FUNCTION ARGUMENTS AND POINTERS

One of the usages of pointers is to modify the content of a variable


(parameter) passed through a function call.
1 #include <stdio.h>
2 void ten_times_normal(float a)
3 {
4 a = 10.0 ∗ a;
5 }
6 void ten_times_point(float ∗a)
7 {
8 ∗a = 10.0 ∗ ∗a;
9 }
10 int main()
11 {
12 float b=20, c=20;
13 ten_times_normal(b);
14 ten_times_point(&c);
15 printf(" values of b called by valeu b = %f\n", b);;
16 printf(" values of c called by reference c = %f\n", c);
17
18 return 0;
19 }

Note that the dummy variable, a , is declared as a pointer (∗a) and instead
of c, the address of c (&c) is passed to the function. In the function,
ten_times_point(), ∗a represents the content of the variable pointed by a.

59 / 156
FUNCTION ARGUMENTS CALLED BY REFERENCE

the following program uses a function, swap(), that takes two pointers as
arguments and exchanges the values of the two variables pointed by the
pointers:
1 #include <stdio.h>
2 void swap(float ∗pa, float ∗pb)
3 {
4 float tmp1, tmp2;
5 tmp1=∗pa;
6 tmp2=∗pb;
7 ∗pa =tmp2;
8 ∗pb =tmp1;
9 }
10 int main()
11 {
12 float a=10.0, b=50.0;
13 printf("Old a = %f and old b = %f\n",a,b);
14 swap(&a,&b);
15 printf("New a = %f and new b = %f\n", a,b);
16 return 0;
17 }

60 / 156
POINTERS AND ARRAYS

Handling matrices and vectors in linear algebra is the most relevant use of
pointers for scientific programming. When an array, a[3], the C compiler
actually interprets a as a pointer and reserves appropriate memory space.

Accessing array elements in two different ways.

In Table, ∗(a+2) means that the value (address) of the pointer, a , is


advanced by two units and the content at that address is referenced which
is equivalent to the value of a[2].

61 / 156
PASS AN ARRAY TO A FUNCTION

The following program use a function that takes an array as an input and
doubles all the elements in that array.
1 #include <stdio.h>
2 void twice_pointer(float ∗a)
3 {
4 int i;
5 for (i=0; i<3; i++) ∗(a+i) = 2.0 ∗ ∗(a+i);
6 }
7 void twice_normal(float a[3])
8 {
9 int i;
10 for (i=0; i<3; i++) a[i] = 2.0 ∗ a[i];
11 }
12 int main()
13 {
14 float b[3]={1.0, 2.0, 3.0};
15 float c[3]={1.0, 2.0, 3.0};
16 int i;
17 twice_pointer(b);
18 twice_normal(c);
19 printf("doubles all the elements in that array ussing ponters in function\n");
20 for (i=0; i<3; i++) printf("%f\t", b[i]); printf("\n");
21 printf("doubles all the elements in that array ussing array elements in function\n");
22 for (i=0; i<3; i++) printf("%f\t", c[i]); printf("\n");
23 return 0;
24 }

62 / 156
POINTERS AND TWO DIMENSIONAL ARRAY

We will assign the address of the first element of the array num to the
pointer ptr using the address of & operator.
The two dimensional array num will be saved as a continuous block in the memory.
So, if we increment the value of ptr by 1 we will move to the next block in the
allocated memory.
1 #include <stdio.h>
2 int main(void) {
3 int num[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // 2d array
4 int ROWS = 3, COLS = 4, i, j;
5 int ∗ptr = &num[0][0]; // pointer
6 // print the element of the array via pointer ptr
7 for (i = 0; i < ROWS; i++) {
8 for (j = 0; j < COLS; j++) {
9 printf("%d ", ∗(ptr + i ∗ COLS + j));
10 }
11 printf("\n");
12 }
13 return 0;
14 }

If we want to get the value at any given row, column of the array then we can use
the value at the address of ∗ operator and the following formula.
num[i][j] = ∗(ptr + (i x no_of_cols + j))
Where, num is a two dimensional array and i and j denotes the ith row and jth
column of the array, no_of_cols denotes the total number of column in the row of the
array and ptr holds the address of the first element of the array.
63 / 156
FUNCTION POINTERS

There is another type of pointer that can point to a function instead of a


variable. Such a pointer is called a function pointer. Using a function
pointer, it is possible to use a variable representing different functions.

A function pointer can be declared as


type_of_function (*func_name)(type_of_argument)
where is the type of a function to which the function points,
type_of_function
func_name is the name of the function pointer and type_of_argument is the type
of the argument in func_name.

64 / 156
FUNCTION POINTERS EXAMPLE

In the following code, func() is a function pointer which points to an actual


doubletype function that has a double type variable as the argument.
1 include <stdio.h>
2 #include <math.h>
3 #define _USE_MATH_DEFINES
4 double f1(double x)
5 {return x ; }
6 double f2(double x)
7 {return x∗x ; }
8 int main()
9 {
10 double (∗func)(double);
11 func=&f1;
12 printf("function pointer, points to f1(pi)=%lf\n", func(M_PI)) ;
13 func=&f2;
14 printf("function pointer, points to f2(pi)=%lf\n", func(M_PI));
15 func=&cos;
16 printf("function pointer, points to cos(pi)=%lf\n", func(M_PI));
17 return 0;
18 }

65 / 156
HOW TO HANDLE A STRING OF CHARACTERS

A text (a string of characters) in C is handled as a set of individual


characters, i.e., an array of characters. Therefore, an array has to be used
to represent a text (string). Since any array variable is a pointer as
discussed, the variable that represents a string is a pointer as well.
1 #include <stdio.h>
2 int main()
3 {
4 float a=2.0; char b=’A’;
5 printf("%f\n",a); printf("%c\n",b);
6 return 0;
7 }

In the program above, the float variable, a, represents a single value, 2.0.
The char variable, c, represents a single character, A.

66 / 156
HOW TO HANDLE A STRING OF CHARACTERS 2

1 #include <stdio.h>
2 int main()
3 {
4 float a[]={2.0, 3.0, 4.0, 5.0}; char b[]="Hello";
5 printf("%f\n",a[0]); printf("%c\n",b[0]);
6 return 0;
7 }

In the program above, an array, a, represents a set of four numbers,


and 5, the first number is printed. An array, b, represents a
2.0, 3.0, 4.0,
string of five characters, Hello and the first character is printed.

67 / 156
FORMAT %s

Use the %s format to represent the entire string instead of the %c format
that represents only one character.
1 #include <stdio.h>
2 int main()
3 {
4 char ∗a;
5 a="Hello, World!";
6 printf("%s\n",a);
7 return 0;
8 }

As is seen in the examples above, a single quotation mark (’) and a double
quotation mark (") work differently. The single quotation mark must be
used for one character while the double quotation mark must be used for a
string of characters.

68 / 156
FORMAT %s 2

1 #include <stdio.h>
2 int main()
3 {
4 char str[100];
5 printf("Enter a word = ");
6 scanf("%s", str);
7 printf("%s\n",str);
8 return 0;
9 }

Note that there is no & before str in the scanf() function as str is already a
pointer. Also, only “one word” is printed even though “several words” was
entered. This is because the format, %s , in scanf(), reads input until a space
is entered (a word). If two strings (two words) are to be read, %s %s must
be used.

69 / 156
STRING COPY/COMPARE

To copy a string to another string or to compare a string against another


string, it is best to use functions, strcpy() and strcmp(), available in <string.h>.
1 #include <stdio.h>
2 #include <string.h>
3 int main()
4 {
5 char c1[] = "ABCDE", c2[6];
6 char s[100];
7 printf("Enter \"ABCDE\"\t");
8 scanf("%s", s);
9 strcpy(c2,c1);
10 if (strcmp(s,c2) == 0) printf("\n ABCDE was entered correctly.\n");
11 else printf("\n Wrong. %s was entered.\n",s);
12 printf("\n c2=%s\n",c2);
13 return 0;
14 }

In the program above, the strcmp() function takes two strings as the
arguments and returns 0 if the two strings match. Note that you can
output " (double quotation mark) in printf() function by using the backslash
character.

70 / 156
STRING LENGTH

To find the length of the string, use the strlen() function:


1 #include <stdio.h>
2 #include <string.h>
3 int main()
4 {
5 char c[50];
6 printf("Enter string = ");
7 scanf("%s", c);
8 printf("You entered %s\n", c);
9 printf("Its length is %d\n", strlen(c));
10 return 0;
11 }

71 / 156
STRUCTURES

An array is a single variable that can represent many elements. However,


all the elements in an array must be of the same type. A structure is a
collection of one or more variables under a single name. These
variables can be of different types, and are selected from the structure by
name.
1 #include <stdio.h>
2 struct student{
3 int ID;
4 long int code;
5 char ∗name;
6 };
7 int main()
8 {
9 struct student ortiz={5, 20182135032, "ORTIZ MENDOZA JERSON HUMBERTO"},
10 garcia={14, 20192135006, "GARCIA GONZALEZ FABIAN CAMILO"};
11 printf("%d\n", ortiz.ID);
12 printf("%ld\n", ortiz.code);
13 printf("%s\n", garcia.name);
14 return 0;
15 }

Observe that a member in a structure can be accessed by using the dot (.).

72 / 156
ORGANIZE DATA FROM FILE IN A STRUCTURE

1 #include <stdio.h>
2 #include <string.h>
3 #define N 19
4 struct student{
5 int ID;
6 char ∗name;
7 long int code;
8 };
9 int main()
10 {
11 FILE ∗fp;
12 int x[N+1]; long int y[N+1]; char z[N+1][50];
13 int i;
14 fp=fopen("list.dat","r");
15 for (i=1;i<=N; i++){
16 fscanf(fp, "%d \t %ld \t %s \n", &x[i], &y[i], z[i]);
17 }
18 struct student listclass[N+1];
19 for (i=1;i<=N; i++){
20 listclass[i].ID = x[i];
21 listclass[i].code = y[i];
22 listclass[i].name = z[i];
23 }
24 printf("%d\n", listclass[5].ID );
25 printf("%s\n", listclass[5].name);
26 printf("%ld\n", listclass[5].code );
27 fclose(fp);
28 return 0;
29 }
73 / 156
HOMEWORK 05:

Assigment: Build a subroutine that organize a data files (provide by your


professor) into a structure and then compare them:
1. read the data from files,
2. organize data into a structure,
3. compare data from the structures,
4. inform user if the structures match or not.

74 / 156
Note on Numerical Errors

Remember that, the data type, float, was used for all real numbers where 4
bytes are allocated for each floating number. A float variable can represent
10−38 to 1038 which covers most of the practical range. However, this
range is translated into 6 to 8 decimal digits of precision and many
problems in science and engineering require more precision that this
range.
Consider the following code:
1 #include <stdio.h>
2 int main()
3 {
4 float s=0.0;int i;
5 for (i=0;i <10000;i++) s=s+0.1;
6 printf("%f\n",s);
7 return 0;
8 }

The intention of the program is to add 0.1 for 10000 times. The result should be
1000. However, the program outputs the following result: 999.902893.
The output is not 1000 but 999.902893 which is almost 1000 but when precision is
critical, this result is not acceptable. The error in this example comes from
conversion between decimal numbers and binary numbers. The decimal numbers in
the source code are converted to the binary numbers with a possibility of
conversion error and the binary numbers are converted back to the decimal
numbers adding another conversion error.
75 / 156
Cancellation Error

Consider again the following program: this program subtracts 123.45655


from 123.45678, the result should be 0.00023. However, the output from
the program is not what it is supposed to be. The result is 0.000229.
1 #include <stdio.h>
2 int main()
3 {
4 float a,b,c;
5 a=123.45678;
6 b=123.45655;
7 printf("%f\n",a−b);
8 return 0;
9 }

Both types of errors are inevitable and there is no way to completely eliminate such
errors. However, possible harm can be minimized by using double instead of float for
floating numbers. When a number is declared as double, it is assigned 8 bytes and
effectively increases the valid range. While the range of a float variable is ±10−38 to
±1038 with seven significant digits, the range of a double variable is ±10−308 to
±10308 with 15 significant digits.

In the previous programs, change float to double and also change %f to %lf
(long float) in the printf() function.

76 / 156
Not use float but use double

In numerical analysis, do not use float but use double exclusively. As


a variable declared as double occupies double the memory space (8 bytes)
of a float variable (4 bytes), the size of the resulting executable increases
but that’s a small price to pay.

77 / 156
Roots of f (x) = 0

The fundamental theorem of algebra states that an n − th order polynomial


equation has n roots including complex roots. This does not mean that all
the roots for polynomial equations can be obtained analytically in closed
form. In fact, a fifth-order polynomial equation and beyond has no formula
for their roots in closed form. This theorem known as the Galois theorem
was proven by a French mathematician, Évariste Galois (1811–1832),
when he was 19.

Two important algorithms to numerically solve a single equation, f (x) = 0,


are explained. They are:
1. the bisection method and
2. Newton-Raphson’s method.
The bisection method is guaranteed to obtain at least one root while
Newton’s method provides a faster algorithm to obtain roots.

78 / 156
BISECTION METHOD

The bisection method is based on the mean value theorem which states
that

Theorem
If f (x) is continuous over x1 ≤ x ≤ x2 and f ′ (x) exists over x1 < x < x2 and
f (x1 )f (x2 ) < 0, there is at least one point x between x1 and x2 such that
f (x) = 0.

Algorithm 2 Bisection Method


1: procedure Bisection(x1 , x2 )
2: Choose x1 and x2 such that
f (x1 )f (x2 ) < 0.
3: Set x3 ← (x1 + x2 )/ 2.
4: If f (x1 )f (x3 ) < 0 then, set x2 ← x3 .
5: Else set x1 ← x3 .
6: Repeat 3 − 5 until |x1 − x2 | < ε
(small threshold) or f (x3 ) = 0.
7: end procedure

79 / 156
NEWTON-RAPHSON METHOD
Newton-Raphson method, is the defacto standard of the algorithm to
obtain roots for f (x) = 0. Instead of specifying two points as in the
bisection method, Newton’s method requires only one point as initial
guess to start with.

As shown in Figure, Newton’s method begins with the initial guess of x1


which is chosen as close as possible to one of the roots for f (x) = 0. At
(x1 , f (x1 )), a tangent line is drawn as an approximation to f (x) and its
intercept point, x2 , with the x axis is deemed as the second approximation
which is supposed to be closer to the root. This iteration is repeated until
sufficient convergence is attained.
80 / 156
NEWTON-RAPHSON METHOD 1a

From Figure, the equation of a straight line that passes (a, b) with a slope
of m is expressed as
y − b = m(x − a)
Therefore, the tangent line that passes (x1 , f (x1 )) with the slope of f ′ (x1 ) is
expressed as
y − f (x1 ) = f ′ (x1 )(x − x1 )

81 / 156
NEWTON-RAPHSON METHOD 1b

The condition that this line intercepts with the x axis is

0 − f (x1 ) = f ′ (x1 )(x − x1 )

which can be solved for x as


f (x1 )
x = x1 −
f ′ (x1 )

82 / 156
NEWTON-RAPHSON METHOD 1c

Hence, the second approximation is expressed as


f (x1 )
x2 = x1 −
f ′ (x1 )

In general,
f (xn )
xn+1 = xn − (8)
f ′ (xn )
can be used as as an iterative formula to obtain the n + 1 approximation
from the n approximation.
Start with an initial guess of x1 which is sufficiently close to the root,
compute x2 , x3 , x4 , · · · from Eq.(8) and repeat this iteration until |xn+1 − xn |
is smaller than the set threshold.

83 / 156
NEWTON-RAPHSON METHOD 1d

Here is the algorithm for Newton’s method:

Algorithm 3 Newton-Raphson Method


1: procedure newton(x1 )
2: Pick initial guess, x1 .
3: Confirm that f ′ (x1 ) ̸= 0.
4: Repeat
f (x1 )
a: x2 = x1 − f ′ (x )
1
b: x1 = x2
5: Until |x1 − x2 | ≤ ε
6: end procedure

Note that Newton’s method fails when f ′ (xn ) is zero which makes the
denominator in Eq.(8) zero.

84 / 156
HOMEWORK 06

Assigment:
1. Implement the Bisection and Newton Rhapson methods.
2. Plot the function:
f (x) = x sin(x) + x sin(x2 ) − ex
Find all the roots in the interval [-4,-1]
2.1 using the bisection method.
2.2 using the Newton-Raphson method.
Rta: x1 = −3.49358 x2 = −3.05902 x3 = −2.60517 x4 = −1.46616
3. Plot the function:
ex − 3x = 0
Find all the roots in the interval [0,-2].
3.1 using the bisection method.
3.2 using the Newton-Raphson method.
Rta: x1 = 0.61906 x2 = 1.51213

85 / 156
NUMERICAL DIFFERENTIATION

It is always possible to analytically differentiate a given function no matter


how lengthy or complicated it may be as long as the function is given
explicitly.

The only occasion in which numerical differentiation is needed is when a


function is given numerically.

There are three basic schemes for numerical differentiation using three
neighboring points, Forward, Backward and Central difference.

86 / 156
FORWARD DIFFERENTIATION

Consider the Taylor series of f (x + h) expressed as, where h = ∆x.



X hn
f (x + h) = f (n) (x)
n=0
n!
h2 h3
= f (x) + f ′ (x) h + f ′′ (x) + f ′′′ (x) +··· (9)
2! 3!
≈ f (x) + f ′ (x) h

When we retain only the first two terms of the right-hand side.
Then f ′ (x) can be expressed as

f (x + h) − f (x)
f ′ (x) ≈ (10)
h
This is called the forward difference scheme.

87 / 156
BACKWARD DIFFERENTIATION

Consider the Taylor series of f (x − h) expressed as


X (−h)n
f (x − h) = f (n) (x)
n=0
n!
h2 h3
= f (x) − f ′ (x) h + f ′′ (x) − f ′′′ (x) +··· (11)
2! 3!
≈ f (x) − f ′ (x) h

Then f ′ (x) can be expressed as

f (x) − f (x − h)
f ′ (x) ≈ (12)
h
This is called the backward difference scheme.

88 / 156
CENTRAL DIFFERENTIATION

Equations Eq.(9) and Eq.(11) are listed again as

h2 h3
f (x + h) = f (x) + f ′ (x) h + f ′′ (x) + f ′′′ (x) +···
2! 3!
h2 h3
f (x − h) = f (x) − f ′ (x) h + f ′′ (x) − f ′′′ (x) +···
2! 3!
Subtracting Eq. Eq.(11) from Eq.(9) yields

h3
f (x + h) − f (x − h) = 2 f ′ (x) h + 2f ′′′ (x) +···
3!
Dropping the second term and beyond, f ′ (x) can be expressed as

f (x + h) − f (x − h)
f ′ (x) ≈ (13)
2h

89 / 156
SECOND-ORDER DERIVATIVE

If the second-order derivative of f (x) is desired, adding Equations Eq.(9)


and Eq.(11) listed again as

h2 h3
f (x + h) = f (x) + f ′ (x) h + f ′′ (x) + f ′′′ (x) +···
2! 3!
h2 h3
f (x − h) = f (x) − f ′ (x) h + f ′′ (x) − f ′′′ (x) +···
2! 3!
yields

f (x + h) + f (x − h) = 2 f (x) + f ′′ (x) h2 + · · ·

from which f ′′ (x) can be approximated as

f (x + h) + f (x − h) − 2f (x)
f ′′ (x) ≈ (14)
h2

90 / 156
EXAMPLE

The following code implements the forward, backward and central


difference schemeS, for the data:

1 #include <stdio.h>
2 #define N 11
3 int main()
4 {
5 double y[N]={0, 0.0998, 0.1986, 0.2955, 0.3894, 0.4794, 0.5646, 0.6442, 0.7173, 0.7833,
0.8414};
6 double forward[N], backward[N], central[N], h=0.1;
7 int i;
8 for (i=0;i<N ;i++) forward [i] = (y[i+1]−y[i ]) /( h);
9 for (i=1;i<N+1;i++) backward[i] = (y[i ]−y[i−1]) /( h);
10 for (i=1;i<N−1;i++) central [i] = (y[i+1]−y[i−1]) /(2∗h);
11 printf ("x | forward | backward | central \n−−−−−−−−−−−−−−−−−−−−−−−−−−−\n"
);
12 for (i=1;i<N−1;i++)
13 printf ("%lf %lf %lf %lf \n", i∗h, forward[i], backward[i], central[i]);
14 return 0;
15 }

91 / 156
NOTE ON FIRST AND LAS POINT DIFFERENCE

The central difference scheme is more accurate than the forward and
backward difference schemes.

Observe that the forward difference scheme cannot compute the last point
i=N, the backward diffence scheme cannot compute de first point i=0 and
central difference connot compute the last (i=N) and first points (i=0).

92 / 156
APPROXIMATE DIFERENCE OF FIRST POINT

Replace h for 2h in Eq.(9) yielding

4h2 8h3
f (x + 2h) = f (x) + 2hf ′ (x) + f ′′ (x) + f ′′′ (x) +··· (15)
2! 3!
The h2 term in Eq.(15) can be eliminated by subtracting Eq.(15) from 4
times of Eq.(9) as

4f (x + h) − f (x + 2h) = 3f (x) + 2hf ′ (x) + higher-order terms·

from which f ′ (x) can be solved as

4f (x + h) − f (x + 2h) − 3f (x)
f ′ (x) ≈ (16)
2h
Equation Eq.(16) has been used to compute the diference of the first point.

93 / 156
APPROXIMATE DIFERENCE OF LAST POINT

Replace h for 2h in Eq.(11) yielding

4h2 8h3
f (x − 2h) = f (x) − 2hf ′ (x) + f ′′ (x) − f ′′′ (x) +··· (17)
2! 3!
The h2 term in Eq.(17) can be eliminated by subtracting Eq.(17) from 4
times of Eq.(11) as

4f (x − h) − f (x − 2h) = 3f (x) − 2hf ′ (x) + higher-order terms·

from which f ′ (x) can be solved as

3f (x) − 4f (x − h) + f (x − 2h)
f ′ (x) ≈ (18)
2h
Equation Eq.(18) has the same order of accuracy as the central difference
scheme. The price that needs to be paid is that instead of two values of
f (x), three values must be used.

94 / 156
CENTRAL DIFFERECE FIRST AND LAST POINT

The following code implement the central difference scheme, including the
calculus for first and las points.

1 #include <stdio.h>
2 #define N 11
3 int main()
4 {
5 double y[N]={0, 0.0998, 0.1986, 0.2955, 0.3894, 0.4794, 0.5646, 0.6442, 0.7173, 0.7833,
0.8414};
6 double central[N], h=0.1;
7 int i;
8 for (i=1;i<N−1;i++) central[i] = (y[i+1]−y[i−1])/(2∗h);
9 central[0 ] = (4∗y[1]−y[2]−3∗y[0])/(2∗h); /∗ first point ∗/
10 central[N−1] = (3∗y[N−1]−4∗y[N−2]+y[N−3])/(2∗h); /∗ last point ∗/
11 printf ("i | \t x | \t central | \n−−−−−−−−−−−−−−−−−−−−−−−−−−−\n");
12 for (i=0;i<N;i++)
13 printf ("%d \t %lf \t %lf \n",i, i∗h, central[i]);
14 return 0;
15 }

95 / 156
HOMEWORK 07

Assigment: From the data supplied by your professor realize:

1. Plot the data.


(for your inform submit a graph)
2. Find the first derivative (velocity) of the data using the central scheme.
(for your inform submit the code and a graph of the data obtained)
3. Implement the second derivative Eq.(14) and find the acceleration of
the data.
(for your inform submit the code and a graph of the data obtained)

96 / 156
NUMERICAL INTEGRATION

Any function if given explicitly can be differentiated analytically but very


few functions can be integrated analytically which is why numerical
integration is more important than numerical differentiation.

While analytical integration is a difficult task in general, it is graphically


equivalent to computing the area surrounded by f (x), the x axis and the
lower and upper bounds of the integration.

97 / 156
RECTANGULAR RULE

As shown in Figure, the rectangular rule is to approximate by summing up


n rectangles over [a, b]. Depending on which side of the rectangle to be
chosen as the value of f (x), the left rectangular rule or the right
rectangular rule can be considered.

For the left rectangular rule, the integration, I, can be approximated by


I ≈ h f0 + h f1 + h f2 + h f3 + · · · + h fn−1
I ≈ h (f0 + f1 + f2 + f3 + · · · + fn−1 ) (19)
where h = [b − a]/ n is the step size and f0 , f1 , f2 , · · · fn−1 are the selected
values of the rectangles.
98 / 156
EXAMPLE

Considere the integral,


Z1
1
I= dx = π/ 4
0 1 + x2
As this integration can be carried out analytically and the exact value is
π/ 4, it can be used as a benchmark for the accuracy of each numerical
integration scheme.
The following program implement the rectangle rule using Eq.(19)
1 #include <stdio.h>
2 double f(double x)
3 {return 4.0/(1.0+x∗x) ; }
4 int main()
5 {
6 int i, n;
7 double a=0.0, b=1.0, h, s=0.0 ;
8 printf("Number of partitions = ");
9 scanf("%d", &n) ;
10 h = (b−a)/n ;
11 for (i= 0;i<n;i++) s = s + f(a + i∗h) ;
12 s=s∗h ;
13 printf("Result =%lf\n", s) ;
14 return 0;
15 }

99 / 156
TRAPEZOIDAL RULE

As shown in Figure, the trapezoidal rule approximates the integration, I, by a set of


trapezoids over the interval.

Remember that the area of a trapezoid is given by


a+b
Area = h
2
where a and b are the lengths of the parallel sides, h is the height.
In our case the sum of all the trapezoids is expressed as
h h h
I ≈ (f0 + f1 ) + (f1 + f2 ) + · · · + (fn−1 + fn )
2 2 2
h
I ≈ (f0 + fn ) + h (f1 + f2 + f3 + · · · + fn−1 ) (20)
2
100 / 156
TRAPEZOIDAL RULE CODE

The following code implement the trapezoidal scheme using Eq.(20):

1 #include <stdio.h>
2 double f(double x)
3 {return 4.0/(1.0+x∗x);}
4 int main()
5 {
6 int i, n ;
7 double a=0.0, b=1.0 , h, s=0.0, x;
8 printf("Enter number of partitions = ");
9 scanf("%d", &n) ;
10 h = (b−a)/n ;
11 for (i=1;i<=n−1;i++) s = s + f(a + i∗h);
12 s=h/2∗(f(a)+f(b))+ h∗ s;
13 printf("%20.12f\n", s) ;
14 return 0;
15 }

101 / 156
SIMPSON’S RULE

▶ In the rectangular rule, a segment of f (x) is approximated by a flat


line (a zero-order polynomial)
▶ and in the trapezoidal rule, it is approximated by a straight line with
a slope (a first-order polynomial).
▶ As is expected, the next level of refinement is to use a curved line (a
second-order polynomial) which leads to Simpson’s rule.

102 / 156
SIMPSON’S RULE 1a

In Figure, we show a second-order polynomial that passes the three points


as sought in the figure.

The curve that passes the three points (−h, f (−h)), (0, f (0)) and (h, f (h)) is
assumed to be
y = ax2 + bx + c
103 / 156
SIMPSON’S RULE 1b

Imposing the condition that this equation passes the three points yields

f (−h) = ah2 − bh + c
f (0) = c
f (h) = ah2 + bh + c

from which a, b and c can be solved as


f (h) + f (−h) − 2f (0)
a=
2h2
f (h) − f (−h)
b=
2h
c = f (0)

104 / 156
SIMPSON’S RULE 1c

Now the area between −h and h under the parabola, yet determines via
a, b and c, is given by
Zh Zh 
f (h) + f (−h) − 2f (0) 2 f (h) − f (−h)

2
(ax + bx + c) dx = x + x + f (0)
−h −h 2h2 2h
h
= (f (−h) + 4f (0) + f (h)) (21)
3
By applying this for each segment in the interval [a, b], one obtains

h
I≈ ((f0 + 4f1 + f2 ) + (f2 + 4f3 + f4 ) + · · · + (f2n−2 + 4f2n−1 + f2n ))
3
h
≈ (f0 + 4f1 + 2f2 + 4f3 + 2f4 + · · · + 2f2n−2 + 4f2n−1 + f2n )
3

105 / 156
SIMPSON’S RULE 1d

Note that the number of partitioning for Simpson’s rule must be an even
number (2n), then the partition size must be declared as h = (b − a)/ (2n).

For the coding purpose, it is convenient to rewrite the last equation as

h
I≈ (f0 + f2n )
3
h
+ 4 (f1 + f3 + f5 + · · · + f2n−1 ) (22)
3
h
+ 2 (f2 + f4 + f6 + · · · + f2n−2 )
3

106 / 156
HOMEWORK 08

Assigment:

1. Deduce the Simpson’s rule for a partition shown in Eq.(21). This is, do
the intregral.
2. Implement the Simpson’s rule in a C code to solve:
Z1
4
2
dx = π
0 1+x

3. Evaluate analytically
Z1
x ln(x) dx
0
4. Write a C program to numerically integrate the above using Simpson’s
rule. Note that ln x → −∞ as x → 0. So the challenge is how to handle
this seemingly singular point at x = 0.

107 / 156
SYSTEMS OF LINEAR EQUATIONS

A linear equation is an equality of the form


n
X
ai xi = y
i=1

where ai are scalars, xi are unknown variables in R, and y is a scalar.

Question: Determine which of the following equations is linear and which


is not.
1. 3x1 + 4x2 − 3 = −5x3
−x1 +x2
2. x3
=2
3. x1 x2 + x3 = 5

108 / 156
SYSTEMS OF LINEAR EQUATIONS 1a

A system of linear equations is a set of linear equations that share the


same variables. Consider the following system of linear equations:

a1,1 x1 + a1,2 x2 + · · · + a1,n−1 xn−1 + a1,n xn = y1


a2,1 x1 + a2,2 x2 + · · · + a2,n−1 xn−1 + a2,n xn = y2
······
am−a,1 x1 + am−1,2 x2 + · · · + am−1,n−1 xn−1 + am−1,n xn = ym−1
am,1 x1 + am,2 x2 + · · · + am,n−1 xn−1 + am,n xn = ym

where ai,j and yi are real numbers.

109 / 156
SYSTEMS OF LINEAR EQUATIONS 1b

The matrix form of a system of linear equations is Ax = y, where A is an


m × n matrix, A(i, j) = ai,j , y is a vector in Rm , and x is an unknown vector
in Rn . The matrix form is shown below:
a1,1 a1,2 . . . a1,n−1 a1,n x1 y1
    
 a2,1 a2,2 . . . a2,n−1 a2,n  x2   y2 
  ..  =  .. 
 . .. .. .. 
 . ..    
. . . . . . .
am,1 am,2 . . . am,n−1 am,n xn ym

If you carry out the matrix multiplication, you will see that you arrive back
at the original system of equations.

Question: Put the following system of equations into matrix form:

4x + 3y − 5z = 2
−2x − 4y + 5z = 5
7x + 8y = −3
x + 2z = 1
9 + y − 6z = 6

110 / 156
SYSTEMS OF LINEAR EQUATIONS 1c

Question: If rank(x) = n and rank(y) = m for which case we have, a unique


solution, no solution and infinitely solutions?
1. m > n
2. m = n
3. m < n

111 / 156
SYSTEMS OF LINEAR EQUATIONS 1d

We will discuss how to solve a system of equations which has a unique


solution.

Let us say we have n equations with n variables, Ax = y, as follows:

a1,1 a1,2 ... a1,n−1 a1,n x1 y1


    
a2,1 a2,2 ... a2,n−1 a2,n  x2  y2 
  ..  =  .. 
 . .. .. .. 
 . ..    
. . . . . . .
an,1 an,2 ... an,n−1 an,n xn yn

112 / 156
GAUSS–JORDAN ELIMINATION METHOD

Gauss–Jordan elimination solves systems of equations. It is a procedure to


turn A into a diagonal form such that the matrix form of the equations
becomes:    ′ 

1 0 0 0 x1 y1
0 1 0 0 x2   y2′ 
  =  y3′ 


0 0 1 0 x3 
0 0 0 1 x4 y4′
Where {y1′ , y2′ , y3′ , y4′ } are the solutions for {x1 , x2 , x3 , x4 }.

The following code is implementation of the Gauss-Jordan elimination


method. It should be noted that the index of an array in C begins at 0
while in linear algebra, the index of vectors and matrices begins with 1.
Therefore, when implementing equations in linear algebra in C, every
index needs to be shifted to the left by 1. In the following program, the
combination of the matrix A and the vector y can be entered into an array,
a[N][N+1], with N the number of equations.

113 / 156
GAUSS–JORDAN CODE

1 #include <stdio.h>
2 #define N 3
3 int main()
4 {
5 double a[N][N+1]={{2, −1, 1, 2},{−1, 3, 3, 3},{2, 1, 4, 1}};
6 double pivot,d;
7 int i,j,k;
8 for(k=0; k<N; k++)
9 {
10 pivot=a[k][k];
11 for(j=k; j<N+1; j++) a[k][j]=a[k][j]/pivot;
12 for(i=0; i<N; i++)
13 {
14 if(i != k)
15 {
16 d=a[i][k];
17 for(j=k; j<N+1; j++) a[i][j]=a[i][j]−d∗a[k][j];
18 }
19 }
20 }
21 for(i=0; i<N; i++) printf("x[%d]=%lf\n", i+1, a[i][N]);
22 return 0;
23 }

114 / 156
HOMEWORK 09

Assigment:

1. Modify the Gauss-Jordan code in a way that the user insert the
coefficients ai,j and yi,j and the program show in the screen tha
augmented matrix [ai,j | yi ]
2. Use the last modify Gauss-Jordan code to solve the following systems
of equations:
2.1
4x1 + 3x2 − 5x3 = 2
−2x1 − 4x2 + 5x3 = 5
8x1 + 8x2 = −3
Sol: x1 = 2.208, x2 = −2.583, x3 = −0.183.
2.2
x1 + 2x2 + x3 − x4 =5
3x1 + 2x2 + 4x3 + 4x4 = 16
4x1 + 4x2 + 3x3 + 4x4 = 22
2x1 + x3 + 5x4 = 15
Sol: x1 = 16, x2 = −6, x3 = −2, x4 = −3.

115 / 156
LU DECOMPOSITION

The LU decomposition (also known as the LU factorization) is a refinement


of the Gauss-Jordan elimination method that further reduces the number
of operations resulting in faster execution.
First, any matrix, A, can be uniquely factorized as

A = LU

where L and U are lower and upper triangular matrices whose components
are expressed as

l1,1 0 ... 0 0 u1,1 u1,2 ... u1,n−1 u1,n


   
 l2,1 l2,2 ... 0 0   0 u2,2 ... u2,n−1 u2,n 
L=
 .. .. .. .. ..  U=
 .. .. .. .. .. 
. . . . .

. . . . .

lm,1 lm,2 ... lm,n−1 lm,n 0 0 ... 0 um,n

This decomposition is called LU decomposition (or LU factorization) and


provides the most efficient way of solving simultaneous equations.

116 / 156
LU DECOMPOSITION 1a

Using the LU decomposition, in the linear sistem of equations, Ax = y is


written as
LUx = y (23)
To solve Eq.(23) for x, the following two steps are needed:

1. Define z ≡ U x and solve L z = y for z first.


2. After z is solved, solve U x = z for x.

It appears first that the two steps above to solve two equations require
more computation than solving a single equation of Ax = y. However,
finding L and U from A requires much less effort and solving for the two
equations is almost trivial.

117 / 156
LU DECOMPOSITION 1b

Now we are going to see the two steps in detail.


1. Solve L z = y for z first.
The value of z i.e., z1 , z2 , · · · , zn can be determined by forward
substitution in the following equations.
l1,1 z1 = y1
l2,1 z1 + l2,2 z2 = y2
l3,1 z1 + l3,2 z2 + l3,3 z3 = y3
······
ln,1 z1 + ln,2 z2 + · · · + ln,n−1 zn−1 + ln,n zn = yn
2. After z is solved, solve U x = z for x.
The value of x i.e., x1 , x2 , · · · , xn can be determined by backward
substitution in the following equations.
u1,1 x1 + u1,2 x2 + u1,3 x3 + · · · + u1,n−1 xn−1 + u1,n xn = z1
+u2,2 x2 + u2,3 x3 + · · · + u2,n−1 xn−1 + u2,n xn = z2
+u3,3 x3 + · · · + u3,n−1 xn−1 + u3,n xn = z3
······
un,n xn = zn
118 / 156
Procedure to Compute L and U

Here we are going to assume that the diagonal of U is 1 that is ui,i = 1 for
i = 1, 2, 3, · · · , n. This method is known as Crout’s decomposition method.
Then the first column of L and the first row of U are determined by,

li,1 = ai,1 for i = 1, 2, · · · , n


a1,j
u1,j = for j = 1, 2, · · · , n
l1,1

The second column of L and the second row of U are determined from the
relations

li,2 = ai,2 − li,1 u1,2 for i = 1, 2, · · · , n


a2,j − l2,1 u1,j
u2,j = for j = 1, 2, · · · , n
l2,2

119 / 156
Procedure to Compute L and U 1a

Next, third column of L and third row of U are determined in a similar way.
In general, li,j and ui,j given by
j−1
X
li,j = ai,j − li,k uk,j for i ≥ j li,j = 0 for i < j
k=1
j−1
P
ai,j − li,k uk,j
k=1
ui,j = for i < j ui,j = 0 for i > j
li,i
ui,i = 1

120 / 156
Algorithm of LU Decomposition Method

Algorithm 4 LU Decomposition Method


1: Read the matrix A = [ai,j ], i, j = 1, 2, · · · , n and the right hand vector y
2: Set ui,i = 1
3: LU decomposition, for i, j = 1, 2, 3, · · · , n compute the following
j−1
X
li,j = ai,j − li,k uk,j
k=1
j−1
P
ai,j − li,k uk,j
k=1
ui,j =
li,i

4: Solve the system L z = y by forward substitution


!
i−1
y1 1 X
z1 = , zi = yi − li,j zj for i = 2, 3, · · · , n
l1,1 li,i j=1

5: Solve the system U x = z by backward substitution


i+1
X
xn = zn , xi = zi − ui,j xj for i = n − 1, n − 2, · · · , 1
j=n

6: Print x1 , x2 , · · · , xn as solution.
121 / 156
LU DECOMPOSITION CODE 1a

1 #include<stdio.h>
2 int main()
3 {
4 int n,i,k,j,p;
5 double a[10][10],l[10][10],u[10][10],y[10],z[10],x[10],sum;
6 printf("Introduzca el orden (rango: nxn) de la matriz A:");
7 scanf("%d",&n);
8 printf("Introduzca los coeficientes de la matriz: ");
9 for(i=1;i<=n;i++) {
10 printf("\n fila −−> %d \t",i);
11 for(j=1;j<=n;j++) scanf("%lf",&a[i][j]);
12 }
13 printf("ingrese los elementos del vector y \n");
14 for(i=1;i<=n;i++) scanf("%lf",&y[i]);
15 //∗∗∗∗∗∗∗∗∗∗ Descomposicion LU ∗∗∗∗∗
16 for(k=1;k<=n;k++) {
17 u[k][k]=1;
18 for(i=k;i<=n;i++) {
19 sum=0;
20 for(p=1;p<=k−1;p++) sum+=l[i][p]∗u[p][k];
21 l[i][k]=a[i][k]−sum; }
22 for(j=k+1;j<=n;j++) {
23 sum=0;
24 for(p=1;p<=k−1;p++) sum+=l[k][p]∗u[p][j];
25 u[k][j]=(a[k][j]−sum)/l[k][k]; }
26 }

122 / 156
LU DECOMPOSITION CODE 1b

27 //∗∗∗∗ Muestra las matrices L y U ∗∗∗∗


28 printf("\n Matriz L \n ");
29 for(i=1;i<=n;i++) {
30 for(j=1;j<=n;j++)
31 printf("%9.3lf",l[i][j]);
32 printf("\n");
33 }
34 printf("\n Matriz U \n ");
35 for(i=1;i<=n;i++) {
36 for(j=1;j<=n;j++)
37 printf("%9.3lf",u[i][j]);
38 printf("\n");
39 }
40 //∗∗∗∗ Encuentra z, resuelve Lz=y ∗∗∗∗∗ (forward subtitution method)
41 for(i=1;i<=n;i++) {
42 sum=0;
43 for(p=1;p<i;p++)
44 sum+=l[i][p]∗z[p];
45 z[i]=(y[i]−sum)/l[i][i];
46 }

123 / 156
LU DECOMPOSITION CODE 1c

47 //∗∗∗∗ Encuentra x, resuelve Ux=z ∗∗∗∗ (backward subtitution method)


48 for(i=n;i>0;i−−) {
49 sum=0;
50 for(p=n;p>i;p−−)
51 sum+=u[i][p]∗x[p];
52 x[i]= z[i]−sum;
53 }
54 //∗∗∗∗ Muestra la solucion ∗∗∗∗
55 printf("\n El vector solucion es \n");
56 for(i=1;i<=n;i++) printf("%9.3lf \n",x[i]);
57 return 0;
58 }

124 / 156
HOMEWORK 10
Assigment: For the circuits shown in the figures, consider:
V1 = 6 V, V2 = 3 V, R1 = 10 Ω, R2 = 10 Ω, R3 = 5 Ω, R4 = 10 Ω,
R5 = 12 Ω, R6 = 5 Ω, R7 = 50 Ω, R8 = 5 Ω, R9 = 10 Ω.
1. Use the Kirchhoff’ laws to:
1.1 Determine the matrix equation of the circuit.
1.2 Solve the above system of equations (circuit matrix) for the currents
(I1 , I2 , I3 , I4 ), using the LU decomposition code.

125 / 156
GAUSS-SEIDEL METHOD (JACOBI METHOD)

The Gauss-Seidel method and the Jacobi method are iterative methods to
solve a set of certain types of simultaneous equations. Unlike the
Gauss-Jordan elimination method, there is no guarantee that the
iterations ever converge but no thorough programming is required and
the method can be also applied to certain nonlinear simultaneous
equations.

To illustrate these methods, consider the following three simultaneous


equations:
 7 x + y + 2 z = 10

x + 8y + 3z = 8 (24)

2x + 3y + 9z = 6
Equations Eq.(24) can be written as
10 − y − 2 z


 x=
7




 8 − x − 3z
y= (25)


 8

 6 − 2X − 3Y
z =

9
126 / 156
GAUSS-SEIDEL METHOD (JACOBI METHOD) 1a

Equations Eq.(25) are rewritten in the form of an iterative scheme as


10 − yn − 2 zn


xn+1 =
7




 8 − xn − 3 zn
yn+1 = (26)


 8

 6 − 2 xn − 3 yn
 zn+1 =

9
Equations Eq.(26) can be solved iteratively starting with x0 , y0 , z0 chosen
by initial guess. This scheme Eq.(26) is called the Jacobi method.
A slightly better iteration scheme is to use the most immediate preceding
values for the next approximation, i.e.,
10 − yn − 2 zn


xn+1 =
7




 8 − xn+1 − 3 zn
y = (27)
 n+1
 8


 6 − 2 xn+1 − 3 yn+1
 zn+1 =

9
This scheme Eq.(27) is called the Gauss-Seidel method.
127 / 156
GAUSS-SEIDEL METHOD (JACOBI METHOD) 1b

In C it is straightforward to implement the Gauss-Seidel method, as any


statement in C uses the most immediate value for variable substitutions.
In the following code, we implement the Gauss-Seidel method,
(x0 , y0 , z0 ) = (0, 0, 0) was chosen as the initial guess:

1 #include <stdio.h>
2 int main()
3 {
4 double x, y, z;
5 int i,n;
6 x=y=z=0.0;
7 printf("Enter # of iteration = ");
8 scanf("%d", &n);
9 for (i=0;i<n;i++)
10 {
11 x = (10−y−2∗z)/7;
12 y = (8−x−3∗z)/8.0;
13 z = (6−2∗x−3∗y)/9.0;
14 }
15 printf("x = %lf, y= %lf, z=%lf\n", x,y,z);
16 return 0;
17 }

128 / 156
CONVERGENCE OF GAUSS-SEIDEL METHOD

129 / 156
CONVERGENCE OF GAUSS-SEIDEL METHOD 1a

130 / 156
HOMEWORK 11

Assigment:
1. Solve the following set of nonlinear equations by the Gauss-Seidel
method:
27 x + ex cos y − 0.12 z = 3



−0.2 x2 + 37 y + 3 x z = 6

 2
x − 0.2 y sin x + 29 z = −4
Start with an initial guess of x = y = z = 1.
2. Write a computer program that applies the Gauss-Siedel method to
solve the following system of linear equations.

131 / 156
HOMEWORK 11 1b

3. Since the following linear systems has non-strictly diagonally dominant


matrix the Gauss-Seidel method diverge.
Implement a program that deal with the divergence of the
Gauss-Seidel method and show, that the method diverges for the
linear systems listed below, use as initial approximation
(x1 , x2 , x3 ) = (0, 0, 0).
clue: use an array to store the variables and implement some
threshold criteria to verify the convergence.

a)

b)

132 / 156
DIFFERENTIAL EQUATIONS (IVP)

The basic form of a differential equation for initial value problems can be
expressed as
dy
= f (t, y) y(t0 ) = y0 I.C (28)
dt
where y is an unknown function, t is the independent variable, and f (t, y) is
a function of t and y. Before equation along with the initial condition (I.C)
is called the initial value problem.
There are two types of differential equations, initial value problems and
boundary value problems. We are going to restricts our study only to
the scope of initial value problems and we are going to use Euler and the
Runge-Kutta methods to solve it.

133 / 156
INITIAL VALUE PROBLEM

Equation Eq.(28) can be interpreted graphically as shown in Figure. Solving


for y(x) in Eq.(28) is equivalent to drawing a curve starting at (t0 , y0 )
whose slope at (t1 , y1 ) is given as f (t1 , y1 ). Only the starting point and a
slope dy/ dt at an arbitrary point are provided.

134 / 156
EULER’S METHOD

Euler’s method is a basic method for solving initial value problems


numerically.
In Euler’s method, dy/ dt in Eq.(28) is approximated by the forward
difference as
dy yn+1 − yn
≈ (29)
dt h
Therefore, Eq.(28) is approximated as
yn+1 − yn
= f (t, y). (30)
h
Equation Eq.(30) can be written as

yn+1 = yn + hf (t, y), (31)

which can be used to predict yn+1 from yn and the slope at that point.

135 / 156
EXAMPLE 1

Consider the following initial value problem:


dy
=y y(0) = 1 I.C (32)
dt
Using the separation of variables, the exact solution is easily obtained as
y(t) = et .
The following C code implements Euler’s method for Eq.(32):
1 #include <stdio.h>
2 #include <math.h>
3 double f(double t, double y){ return y; }
4 int main()
5 {
6 double h=0.1, y, t;
7 int i;
8 t=0.0; y = 1.0;
9 printf("t \t Euler \t Exact\n");
10 for (i=0; i<= 10; i++){
11 printf("t= %lf %lf %lf\n", t, y, exp(t));
12 y=y+h∗f(t,y);
13 t=t+h;
14 }
15 return 0;
16 }

136 / 156
EXAMPLE 2: Lorenz Equations

Euler’s method can be used not only for a single differential equation but
for a set of simultaneous differential equations as well.
The Lorenz system is a system of ordinary differential equations first
studied by mathematician and meteorologist Edward Lorenz in 1963. It is
notable for having chaotic solutions for certain parameter values and
initial conditions. 
dx

 = P (y − x)
dt




 dy
= −x z + R x − y (33)


 dt

 dz
= xy − bz


dt
The Lorenz system are nonlinear differential equations which are
deterministic yet the behavior of the solution was neither periodic,
divergent nor convergent depending on how the parameters, P, R, and b,
and the initial values are chosen.

137 / 156
EXAMPLE 2: Strange Attractor/Chaos

The following code solves Eq.(33) using Euler’s method. The three parameters are
chosen as P = 10, b = 8/ 3 and R = 28 and the initial values are chosen as
x = y = z = 5. The step size is chosen as h = 0.01 and 105 iterations are performed.
1 #include <stdio.h>
2 #define P 10.0
3 #define b 8.0/3.0
4 #define R 28.0
5 double f1(double x, double y, double z){return P∗(y−x);}
6 double f2(double x, double y, double z){return −x∗z+R∗x−y;}
7 double f3(double x, double y, double z){return x∗y−b∗z;}
8 int main()
9 {
10 FILE ∗fp;
11 double h, t, x, y, z;
12 int i;
13 fp=fopen("lorentz.dat","w");
14 t=0.0; h=0.01; x=5.0; y=5.0; z=5.0; /∗ initial values ∗/
15 for (i=0; i< 100000; i++){
16 x=x+h∗f1(x,y,z);
17 y=y+h∗f2(x,y,z);
18 z=z+h∗f3(x,y,z);
19 fprintf(fp,"%lf \t %lf \t %lf\n", x, y, z);
20 t=t+h;
21 }
22 return 0;
23 }

138 / 156
Divergence of Neighbouring Trajectories

From numerical integration of the Lorenz system we know that the


motion/flow on the attractor exhibits sensitive dependence on initial
conditions. This means that two trajectories starting very close to each
other will rapidly diverge, and thereafter have totally different futures.

Sensitive dependence on I.C

Lorenz Attractor

139 / 156
HOMEWORK 12

1. Use the Strange Attractor code, and verify that the Lorenz’s system is
sensitive to initial condition. Prove this using five different Initial
Conditions and plotting the final results.
2. Implement Bouali attractor, defined by the following non linear
differential system.

dx

 = α(1 − y) x − β z
dt




 dy
= −γ (1 − x2 ) y (34)


 dt


 dz
 = μx
dt
Restricted to Initial Conditions x = y = z = 2. Bouali attractor exhibed
chaotic solution for α = 3, β = 2.2, γ = 1 and μ = 0.001.
Bouali attractor

140 / 156
HOMEWORK 12

3. Utilize the previous code that you used to solve the Bouali attractor
and verify that these system is also sensitive to initial condition. Prove
this using three different Initial Conditions and plotting the final results.
4. Implement one of the attractors that appear in the attached link
4.1 Solve the ODE ussing the Euler method
4.2 Use 3 different Initial Conditions (I.C)
4.3 Plot the results for each I.C
Link to STRANGE ATTRACTORS DYNAMIC MATHEMATICS

141 / 156
RUNGE-KUTTA METHOD

In numerical analysis, the Runge–Kutta methods are a family of implicit


and explicit iterative methods, which include the well-known routine called
the Euler method, used in temporal discretization for the approximate
solutions of ordinary differential equations. Runge–Kutta method is an
effective and widely used method for solving the initial-value problems of
differential equations. These methods were developed around 1900 by the
German mathematicians Carl Runge and Wilhelm Kutta. Modern
developments are mostly due to John Butcher in the 1960s.

142 / 156
RUNGE-KUTTA METHOD

Let an initial value problem be specified as follows:


dy
= f (t, y) y(t0 ) = y0 I.C
dt
The obvious approach is to integrate from tn to tn+1 = tn + h:
Z tn+1 Z1
y(tn+1 ) = y(tn ) + f (τ, y(τ)) dτ = y(tn ) + h f (tn + h τ, y(tn + h τ)) dτ
tn 0

and to replace the second integral by a quadrature.


Note: In numerical analysis, a quadrature rule is an approximation of the definite
integral of a function, usually stated as a weighted sum of function values at
specified points within the domain of integration.
Z1 Xn
f (x) dx ≈ wi f (xi ) with wi : weighing
0 i=1

143 / 156
THE METHOD

The outcome might have been “the method”


ν
X
yn+1 = yn + h bj f (tn + cj h, y(tn + cj h))
j=1

except that we do not know the value of y(tn + cj h) at the nodes


tn + c1 h, tn + c2 h, · · · , tn + cν h.
We must resort to an approximation!
We denote our approximation of y(tn + cj h) by ξj , where ξ1 = yn .

144 / 156
THE METHOD 1a

The idea behind explicit Runge–Kutta (ERK) methods is to express each


ξj , j = 2, 3, · · · , ν by updating yn with a linear combination of
f (tn , ξ1 ), f (tn + h c2 , ξ2 ), · · · , f (tn + cj−1 h, ξj−1 ). Specifically, we let

ξ 1 = yn
ξ2 = yn + h a2,1 f (tn , ξ1 )
ξ3 = yn + h a3,1 f (tn , ξ1 ) + h a3,2 f (tn + c2 h, ξ2 )
..
.
ν−1
X
ξν = yn + h aν,j f (tn + cj h, ξj ).
j=1

then the approximation to yn+1 can be written as:


ν
X
yn+1 = yn + h bj f (tn + cj h, ξj )
j=1

145 / 156
THE METHOD 1b

A compac way to writte the method is considering:

kj = f (tn + cj h, ξj )

given
ν
X
yn+1 = yn + h bj kj
j=1

where

k1 = y n
k2 = f (tn + c2 h, yn + h a2,1 k1 )
k3 = f (tn + c3 h, yn + h (a3,1 k1 + a3,2 k2 ))
..
.
kν = f (tn + cν h, yn + h(aν,1 k1 + aν,2 k2 + aν,3 k3 + · · · + aν,ν−1 kν−1 ))

146 / 156
THE METHOD 1c

To specify a particular method, one needs to provide the integer ν (the  


number of stages), and the coefficients ai,j , bj and cj . The matrix A = ai,j
is called the Runge–Kutta matrix, while the bj and cj are known as the
weights and the nodes. These data are usually arranged in a mnemonic
device, known as a Butcher tableau (after John Butcher):

cj A
bj

0
c2 a2,1
c3 a3,1 a3,2
.. ..
. ··· .
cν aν,1 aν,2 ··· aν,ν−1
b1 b2 ··· bν

147 / 156
RUNGE-KUTTA METHOD 4th ORDER

The most well-known method, due to Carl Runge, has 4th order and is
defined by the Butcher tableau and usually denoted as RK4.
0
1 1
2 2
1 1
2
0 2
1 0 0 1
1 1 1 1
6 3 3 6
Then the classical method is given by
4
X
yn+1 = yn + h bj kj
j=1
k1 k2 k3 k4
 
yn+1 = yn + h + + + (35)
6 3 3 6
or
k1 + 2 k2 + 2 k3 + k4
 
yn+1 = yn + h
6
148 / 156
RUNGE-KUTTA METHOD 4th ORDER 1a

RK4.
0
1 1
2 2
1 1
2
0 2
1 0 0 1
1 1 1 1
6 3 3 6
With

k1 = y n
h h
k2 = f (tn + , yn + k1 )
2 2
h h
k3 = f (tn + , yn + k2 )) (36)
2 2
k4 = f (tn + h, yn + h k3 ))

149 / 156
RUNGE-KUTTA METHOD 4th ORDER 1b

The following is a C code to implement the RK4 method for


dy
=y y(0) = 1 I.C (37)
dt
1 #include <stdio.h>
2 #include <math.h>
3 double f(double t, double y){return y;}
4 int main()
5 {
6 double h, t, y, y1, k1, k2, k3, k4;
7 int i;
8 FILE ∗fp;
9 h=0.01; t=0.0; y1=1.0; y=1.0; /∗ Initial Condition ∗/
10 fp=fopen("rk.dat","w");
11 for (i=0; i<=1000; i++)
12 {
13 k1=f(t,y);
14 k2=f(t+h/2.0, y+h∗k1/2.0);
15 k3=f(t+h/2.0, y+h∗k2/2.0);
16 k4=f(t+h , y+h∗k3 );
17 y= y+h∗(k1+2.0∗k2+2.0∗k3+k4)/6.0; /∗ RK4 ∗/
18 t=t+h;
19 y1 = y1 + h∗f(t,y1); /∗ EULER ∗/
20 fprintf(fp,"%lf %lf %lf %lf \n", t, y, y1, exp(t));
21 }
22 fclose(fp);
23 return 0;
24 }
150 / 156
THE RK4 METHOD FOR 2nd ORDER ODE

Consider the 2nd-order ODE:


d2 y dy
+y + 3 y = sin(t) y(0) = 1 y′ (0) = 1 I.C (38)
dt 2 dt
Variable substitution to form a system of ODEs: This 2nd order ODE
can be converted into a system of two 1st order ODEs by using the
following variable substitution:
dy
y1 = y y2 = y1 (0) = 1 y2 (0) = 1 I.C
dt
If we rewrite, the ODE Eq.(38), the system of 1st order ODEs becomes:
dy1
= y2 y1 (0) = 1 I.C
dt
dy2
= −y1 y2 − 3 y1 + sin(t) y2 (0) = 1 I.C
dt
then we can solve the system using RK4 method employing two sets of RK
constant (e.g., k, l) for each variable, as can be seen in the following code.
151 / 156
THE RK4 METHOD FOR 2nd ORDER ODE

1 #include <stdio.h>
2 #include <math.h>
3 double f1(double t, double y1, double y2){return y2;}
4 double f2(double t, double y1, double y2){return − y1∗y2 − 3.0∗y1 + sin(t);}
5 int main()
6 {
7 double h, y1, y2, t, k1, k2, k3, k4, l1, l2, l3, l4;
8 int i;
9 FILE ∗fp1, ∗fp2;
10 h=0.01; t=0.0; y1=1.0; y2=1.0; /∗ Initial Conditions ∗/
11 fp1=fopen("variable_y1.dat","w"); fp2=fopen("variable_y2.dat","w");
12 for (i=0; i<=10000; i++)
13 {
14 k1=f1(t,y1,y2);
15 l1 =f2(t,y1,y2);
16 k2=f1(t+0.5∗h, y1+0.5∗h∗k1, y2+0.5∗h∗l1);
17 l2 =f2(t+0.5∗h, y1+0.5∗h∗k1, y2+0.5∗h∗l1);
18 k3=f1(t+0.5∗h, y1+0.5∗h∗k2, y2+0.5∗h∗l2);
19 l3 =f2(t+0.5∗h, y1+0.5∗h∗k2, y2+0.5∗h∗l2);
20 k4=f1(t+h , y1+h∗k3 , y2+h∗l3);
21 l4 =f2(t+h , y1+h∗k3 , y2+h∗l3);
22 y1= y1+h∗(k1+2.0∗k2+2.0∗k3+k4)/6.0;
23 y2= y2+h∗(l1+2.0∗l2+2.0∗l3+l4)/6.0;
24 t=t+h;
25 fprintf(fp1,"%lf \t %lf \n", t, y1);
26 fprintf(fp2,"%lf \t %lf \n", t, y2);
27 }
28 printf("\n succsesful, data writted in files \n \n");
29 return 0;
30 }
152 / 156
FORCED DAMPED OSCILLATOR

A damped harmonic oscillator is one that, in addition to the restoring


force given by Hooke’s law, experiences a viscous friction force
proportional to velocity.

m a = −k x − λ v λ : viscosity

If this damped oscillator is subjected to an additional external force,


generally time dependent, the oscillator is said to be forced

m a = −k x − λ v + F(t)

153 / 156
FORCED DAMPED OSCILLATOR

if the external force is oscillating

m a = −k x − λ v + Fo cos (ωf t) (39)

where the oscillation frequency of the force does not have to coincide with
the natural frequency of the harmonic oscillator
v
tk
u
ωf ̸= ω0 =
m

We express the equation of motion Eq.(39) in the form of a differential


equation that describes the forced damped oscillations

d2 x dx F0 k λ
+ 2γ + ω2o x = cos (ωf t) ω2o = 2γ = (40)
dt 2 dt m m m
where ωo : is the natural angular frequency, ωf : is the angular frequency
of the oscillating force, γ : is the damping constant, under the condition
γ < ωo .

154 / 156
HOMEWORK 13
Assigment: Forced damped oscillator
1. Transform the forced damped oscillations equation Eq.(40) in a system
of two diferential equation of 1st order, when presenting your work
show the calculations.
2. Implement a code that solve numerically the previos system using the
RK4 method.
3. For the following activities, graph the position (x) and velocity (v) until
time t = 10, for all items consider that the natural angular frecuency is
ω0 = 100 and mass is m = 1.
3.1 Forced damped oscillations:
x(0) v(0) F0 ωf γ type
0 0 5 60 20 Forced damped
0 0 5 100 20 Resonant
3.2 Forced oscillations:
x(0) v(0) F0 ωf γ type
0 0 5 60 0 Forced
0 0 5 100 0 Resonant
3.3 Damped oscillations:
x(0) v(0) F0 ωf γ type
1 0 0 0 60 Damped
3.4 Armonic oscillations:
x(0) v(0) F0 ωf γ type
1 0 0 0 0 Armonic
155 / 156
HOMEWORK 13

Assigment RK 3/ 8 method: The RK4 method is the most common rule


used, however there is another 4th order RK method, deduced by Wilhelm
Kutta, known as the 3/ 8 rule. It is a little less efficient and a little more
accurate. This method is summarized in the following Butcher tableau.
0
1 1
3 3
2
3
− 31 1
1 1 −1 1
1 3 3 1
8 8 8 8

4. Write the steps for this rule, that is show the equivalence of equations
Eq.(35) and (36) for RK 3/ 8 rule
5. Write a code to implement the RK 3/ 8 rule and solve the diferential
equation, until t = 10:
dy 5 t2 − y
= y(0) = 1 I.C
dt et+y
6. Show a graph of yor results for y = y(t) until time t = 10.
156 / 156

You might also like