diff --git a/.gitignore b/.gitignore index a4e96abba310cab0968a899e9057e68a6c13c77f..19394bdde2d62e7293227837b274df36728a75b7 100644 --- a/.gitignore +++ b/.gitignore @@ -196,3 +196,10 @@ notebooks/*gnuxy src/*dataxy src/*gnuxy fortran.html +src/04_dataxy.data +src/04_gnuxy.plt +src/07_plot_newton.data +src/07_plot_newton.plt +src/08_ChristmasTree.f90 +src/10_eqdiff.f90 +src/10_eqdiff.py diff --git a/slides.md b/slides.md index b1a9450fc8f367cb344a51fd4879b3766e2a895d..c3df89975d3ac01f2fd8de870cd7d80b073aa07e 100644 --- a/slides.md +++ b/slides.md @@ -4,7 +4,7 @@ title: Introduction to structured programming with Fortran author: P.Y. Barriat backgroundImage: url('assets/back.png') _backgroundImage: url('assets/garde.png') -footer: 09/11/2023 | Introduction to structured programming with Fortran +footer: 13/11/2024 | Introduction to structured programming with Fortran _footer: "" paginate: true _paginate: false @@ -13,13 +13,13 @@ _paginate: false Introduction to structured programming with `Fortran`<!--fit--> === -https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran +https://forge.uclouvain.be/barriat/learning-fortran  ### Pierre-Yves Barriat -##### November 09, 2023 +##### November 13, 2024 ###### CISM/CÉCI Training Sessions diff --git a/slides.pdf b/slides.pdf index 91c1ea31e4f88989bde4be42084d488f76354e0a..0653bf16c304e11541d12f0581cabdefc753bcb1 100644 Binary files a/slides.pdf and b/slides.pdf differ diff --git a/src/00_hello_world.f90 b/src/00_hello_world.f90 new file mode 100644 index 0000000000000000000000000000000000000000..7bac53dfd77b98941e998abffe218c8bc0daba3a --- /dev/null +++ b/src/00_hello_world.f90 @@ -0,0 +1,6 @@ +! Display "Hello world!" +! +program hello_world + implicit none + write(*,*) "Hello world!" +end \ No newline at end of file diff --git a/src/01_arith.f90 b/src/01_arith.f90 new file mode 100644 index 0000000000000000000000000000000000000000..8755b0c4c64da55d73a0535bfaa1928bfb16a29c --- /dev/null +++ b/src/01_arith.f90 @@ -0,0 +1,86 @@ +! +! Program to demonstrate Arithmetic Assignments +! +program arith + implicit none + + ! Declare variables with explicit types and initialization + real, parameter :: r2_val = 2.0, r3_val = 3.0, r4_val = 4.0 + real :: r2 = r2_val, r3 = r3_val, r4 = r4_val, r5 = 5.0, r6 = 6.0 + real :: ans1, ans2, ans3 + + integer, parameter :: i2_val = 2, i3_val = 3, i4_val = 4 + integer :: i2 = i2_val, i3 = i3_val, i4 = i4_val, i5 = 5, i6 = 6 + integer :: ians1, ians2, ians3, ians4 + + ! Integer arithmetic operations + ians1 = i2 * i3 / i5 + ians2 = i3 / i5 * i2 + ians3 = i2 * (i3 / i5) + ians4 = (i3 / i5) * i2 + + write(*,*) 'Integer Arithmetic Results:' + write(*,*) '2 * 3 / 5 =', ians1 + write(*,*) '3 / 5 * 2 =', ians2 + write(*,*) '2 * (3 / 5) =', ians3 + write(*,*) '(3 / 5) * 2 =', ians4 + print * + + ! Real arithmetic operations + ans1 = r2 * r3 / r5 + ans2 = r3 / r5 * r2 + ans3 = (r3 / r5) * r2 + + write(*,*) 'Real Arithmetic Results:' + write(*,*) '2.0 * 3.0 / 5.0 =', ans1 + write(*,*) '3.0 / 5.0 * 2.0 =', ans2 + write(*,*) '(3.0 / 5.0) * 2.0 =', ans3 + print * + + ! Demonstrating operator precedence + ians1 = i2 + i5 * i3 ** i2 + ians2 = i5 * i3 ** i2 + i2 + ians3 = i3 ** i2 * i5 + i2 + + write(*,*) 'Operator Precedence in Integer Arithmetic:' + write(*,*) '2 + 5 * 3**2 =', ians1 + write(*,*) '5 * 3**2 + 2 =', ians2 + write(*,*) '3**2 * 5 + 2 =', ians3 + print * + + ! Mixing real and integer arithmetic + ans1 = r5 + i3 / i2 + write(*,*) 'Mixing Real and Integer Arithmetic:' + write(*,*) '5.0 + 3 / 2 =', ans1 + + ! Using constants in expressions + ans2 = 5.0 + 3 / 2 + write(*,*) 'Using Constants in Expressions:' + write(*,*) '5.0 + 3 / 2 =', ans2 + print * + + ! Division with real numbers + ans1 = r5 + i3 / r2 + ans2 = r5 + r3 / i2 + + write(*,*) 'Division with Real Numbers:' + write(*,*) '5.0 + 3 / 2.0 =', ans1 + write(*,*) '5.0 + 3.0 / 2 =', ans2 + print * + + ! Exponentiation associativity + ians1 = i5 ** i3 ** i2 + ians2 = (i5 ** i3) ** i2 + ians3 = i5 ** (i3 ** i2) + + write(*,*) 'Exponentiation Associativity:' + write(*,*) '5**3**2 =', ians1 + write(*,*) '(5**3)**2 =', ians2 + write(*,*) '5**(3**2) =', ians3 + print * + + ! Conclusion + write(*,*) 'When in doubt, use parentheses to ensure the desired order of operations.' + + stop +end program arith diff --git a/src/02_sphere.f90 b/src/02_sphere.f90 new file mode 100644 index 0000000000000000000000000000000000000000..7196aec924120076386d53536c7808eeb7dc1f97 --- /dev/null +++ b/src/02_sphere.f90 @@ -0,0 +1,24 @@ +PROGRAM sphere + implicit none + + real(8) pi,radius,volume,area + + write(*,*) 'Enter the value for the radius of a sphere.' + read(*,*) radius + + ! PI value + pi = + ! PI value + write(*,*) 'The value of pi is ', pi + + ! Air & volume + area = + volume = + ! Air & volume + + write(*,*) 'For a radius ', radius + write(*,*) 'the area of a sphere is ', area + write(*,*) 'and the volume is ', volume + + stop +end diff --git a/src/04_plot.py b/src/04_plot.py new file mode 100644 index 0000000000000000000000000000000000000000..c72fa35192326d7e61335e14b993bab1d4ab43c2 --- /dev/null +++ b/src/04_plot.py @@ -0,0 +1,46 @@ +import matplotlib.pyplot as plt + +########################################################################################### +import math + +# Define a function to generate x and y values manually +def generate_sin_data(): + x_values = [] + y_values = [] + for i in range(0,61): # Generate 61 points between 0 and 6 + x = 0.1 * i + y = math.sin(x) + x_values.append(x) + y_values.append(y) + print(i) + return x_values, y_values + +# Generate data +x_values, y_values = generate_sin_data() + +########################################################################################### +#import numpy as np +# +## Define the range for x and calculate sin(x) values +#x_values = np.linspace(0, 6, 61) # Generate 61 points between 0 and 6 +#y_values = np.sin(x_values) +# +########################################################################################### + +# Plotting the results using matplotlib +plt.plot(x_values, y_values, label='sin(x)', color='red') + +# Setting up the labels and title +plt.xlabel('x') +plt.ylabel('y') +plt.title('sin(x)') +plt.xlim([0, 6]) +plt.ylim([-1.2, 1.2]) + +# Display the plot +plt.legend() +plt.grid(True) +plt.show() + +# Optionally, save the plot to a file if desired +# plt.savefig('sinx_plot.png') \ No newline at end of file diff --git a/src/07_plot_newton.py b/src/07_plot_newton.py new file mode 100644 index 0000000000000000000000000000000000000000..caea6d109051a6f9df6b6bcb3781e47d1c14dc73 --- /dev/null +++ b/src/07_plot_newton.py @@ -0,0 +1,60 @@ +import matplotlib.pyplot as plt + +########################################################################################### +import math + +# Define a function to generate the x and y values for f(x) = x^3 + x - 10 +def generate_newton_data(): + x_values = [] + y_values = [] + for i in range(-40, 41): # Loop from -40 to 40 (to generate x-values from -4 to 4) + x = 0.1 * i + fx = x**3 + x - 10 # f(x) = x^3 + x - 10 + x_values.append(x) + y_values.append(fx) + return x_values, y_values + +# Generate the data +x_values, y_values = generate_newton_data() + +########################################################################################### +#import numpy as np +# +## Generate x values from -4 to 4 with 0.1 intervals using numpy +#x_values = np.arange(-4, 4.1, 0.1) # Values from -4 to 4 (inclusive) +# +## Compute the corresponding y values using numpy (f(x) = x^3 + x - 10) +#y_values = x_values**3 + x_values - 10 +# +########################################################################################### + +# Create the plot using matplotlib +plt.plot(x_values, y_values, label='f(x) = x^3 + x - 10', color='blue') + +# Set up plot configuration similar to the original Fortran version +plt.axhline(0, color='black',linewidth=1) # X-axis (y=0 line) +plt.axvline(0, color='black',linewidth=1) # Y-axis (x=0 line) +plt.grid(True) + +# Customize axis ranges and labels +plt.xlim([-4, 4]) +plt.ylim([-60, 60]) # Adjusted for better visibility of the curve + +# No labels on origin as in the original +plt.xticks([-4, -3, -2, -1, 1, 2, 3, 4]) # Custom x-axis ticks +plt.yticks([-60, -40, -20, 0, 20, 40, 60]) # Custom y-axis ticks + +# Adding arrows to the axes to match the gnuplot behavior +plt.annotate('', xy=(4, 0), xytext=(0, 0), + arrowprops=dict(facecolor='black', shrink=0.05)) +plt.annotate('', xy=(0, 60), xytext=(0, 0), + arrowprops=dict(facecolor='black', shrink=0.05)) + +# Display the plot +plt.title('Plot of f(x) = x^3 + x - 10') +plt.xlabel('x') +plt.ylabel('f(x)') +plt.show() + +# Optionally, save the plot to a file if desired +# plt.savefig('newton_plot.png') \ No newline at end of file diff --git a/src/10_eqdiff.txt b/src/10_eqdiff.txt new file mode 100644 index 0000000000000000000000000000000000000000..21664bcfcf2951051b2d31936168055cc05bf090 --- /dev/null +++ b/src/10_eqdiff.txt @@ -0,0 +1,12 @@ +Résolvez numériquement l’équation suivante pour une condition initiale u(t=0)=0 et un pas de temps delta_t = pi/50 +du/dt = A cos(t) + +PS: prenons A=1 + +Que se passe-t-il si le cosinus est évalué en +n*delta_t +(n+0.5)*delta_t +(n+1)*delta_t + +Approximation: +(U(n+1) - U(n)) / delta_t = A cos(n*delta_t) diff --git a/src/10_OLD.FOR b/src/11_OLD.FOR similarity index 100% rename from src/10_OLD.FOR rename to src/11_OLD.FOR