Skip to content
Extraits de code Groupes Projets
Valider ae5c3da1 rédigé par PY Barriat's avatar PY Barriat
Parcourir les fichiers

Upgrade examples

parent d99b3d83
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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
......@@ -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
![h:150](assets/fortran_logo.png)
### Pierre-Yves Barriat
##### November 09, 2023
##### November 13, 2024
###### CISM/CÉCI Training Sessions
......
Aucun aperçu pour ce type de fichier
! Display "Hello world!"
!
program hello_world
implicit none
write(*,*) "Hello world!"
end
\ No newline at end of file
!
! 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
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
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
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
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)
Fichier déplacé
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter