A program for simulating a basketball throw is presented. The presentation is in Swedish but all source code is in English Fortran (code comments are in Swedish though).
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Basketball Throw Simulation (Basketbollkast)
1. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
Projekt: Basketbollkast F¨rs¨ttsblad
o a
Inneh˚
all
Sida 1 av 22
av Grupp 3: Mikael B, J¨rgen H, Sofie J, Johannes L,
o Tillbaka
Anton N, Jenny P och Rasmus W
f¨r kursen Ber¨kningsprogrammering, NUM131
o a Full Screen
St¨ng
a
Avsluta
2. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
7 maxIndex.f90 DG.f90
F(s)
1
2
Hemsida
3
6
4
F¨rs¨ttsblad
o a
5
Inneh˚
all
height (m)
4
3
Sida 2 av 22
2
Tillbaka
1 Full Screen
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
distance (m)
St¨ng
a
z0 = 0.2 s, α0 = 77◦, z ∗ = 88.264 ms, α∗ = 42.868◦. Avsluta
3. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
12 maxIndex.f90 DG.f90
F(s)
Hemsida
10
F¨rs¨ttsblad
o a
8
Inneh˚
all
height (m)
6
4
Sida 3 av 22
2
Korgen
1 Tillbaka
2
3
4
0 Full Screen
0 1 2 3 4 5 6 7
distance (m)
St¨ng
a
z0 = 3.5 s, α0 = 70◦, z ∗ = 3.554 s, α∗ = 86.826◦. Avsluta
4. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
p3.f90 pivot.f90
maxIndex.f90
baksub.f90 eulexpl.f90
DG.f90
F(s)
Hemsida
rootz31.f90
F¨rs¨ttsblad
o a
Inneh˚
all
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
Sida 4 av 22
maxIndex.f90 DG.f90 Tillbaka
F(s)
Full Screen
St¨ng
a
Subrutiner och funktioner i programmet p3 och deras relationer.
Avsluta
5. Interfaces:
p3.f90
subroutine rootz31(f, x0, tol, maxIter, xst, ierr) gauss.f90 jacobi.f90
rootz31.f90
MyG.f90 pyts.f90
implicit none pivot.f90
maxIndex.f90
baksub.f90 eulexpl.f90
DG.f90
F(s)
real(kind=8), intent(in) :: x0(2), tol
integer, intent(in) :: maxIter Hemsida
real(kind=8), intent(out) :: xst(2)
F¨rs¨ttsblad
o a
integer, intent(out) :: ierr
Inneh˚
all
external f
end subroutine rootz31
real(kind = 8) function pyts(x)
implicit none Sida 5 av 22
real(kind = 8), intent(in) :: x(2)
end function pyts Tillbaka
subroutine jacobi(f, fx, x, jret) Full Screen
implicit none
St¨ng
a
real(kind = 8), intent(in) :: fx(2), x(2)
real(kind = 8), intent(out) :: jret(2,2) Avsluta
external f
end subroutine jacobi
6. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Interfaces: (forts)
Hemsida
subroutine MyG(z0alpha0, xy, printing)
implicit none
F¨rs¨ttsblad
o a
real(kind=8), intent(in) :: z0alpha0(2)
real(kind=8), intent(out) :: xy(2) Inneh˚
all
logical, intent(in) :: printing
end subroutine MyG
subroutine eulexpl(yprim, ts, y0, divs, t, Y)
implicit none Sida 6 av 22
real(kind=8), intent(in) :: ts(2), y0(5)
integer, intent(in) :: divs Tillbaka
real(kind=8), intent(out) :: t(divs+1), Y(divs+1, 5)
Full Screen
external yprim
end subroutine eulexpl St¨ng
a
Avsluta
7. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
Interfaces: (forts) F¨rs¨ttsblad
o a
subroutine DG(t, G, Gd)
implicit none Inneh˚
all
real(kind=8), intent(in) :: t, G(5)
real(kind=8), intent(out) :: Gd(5)
end subroutine DG
real(kind=8) function F(s) Sida 7 av 22
implicit none
real(kind=8), intent(in) :: s Tillbaka
end function F
Full Screen
St¨ng
a
Avsluta
8. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
Interfaces: (forts) pivot.f90
maxIndex.f90
baksub.f90 eulexpl.f90
DG.f90
subroutine gauss(A, b, x, r) F(s)
implicit none Hemsida
real(kind=8), intent(in) :: A(2,2), b(2)
real(kind=8), intent(out) :: x(2) F¨rs¨ttsblad
o a
integer, intent(out) :: r
Inneh˚
all
end subroutine gauss
subroutine pivot(A, m, Aret)
implicit none
real(kind=8), intent(in) :: A(2, 3)
Sida 8 av 22
integer, intent(in) :: m
real(kind=8), intent(out):: Aret(2, 3) Tillbaka
end subroutine pivot
Full Screen
integer function maxIndex(y)
St¨ng
a
real(kind=8), intent(in) :: y(:)
end function maxIndex
Avsluta
9. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
F¨rs¨ttsblad
o a
Interfaces: (sista) Inneh˚
all
subroutine baksub(U, b, x, ierr)
implicit none
real(kind=8), intent(in) :: U(2,2), b(2)
real(kind=8), intent(out) :: x(2)
integer, intent(out) :: ierr Sida 9 av 22
end subroutine baksub
Tillbaka
Full Screen
St¨ng
a
Avsluta
10. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
p3: pivot.f90
maxIndex.f90
baksub.f90 eulexpl.f90
DG.f90
write (*, *) ’Ange startv¨rdet z0’
a F(s)
read (*, *) z0 Hemsida
write (*, *) ’Ange begynnelsevinkeln i grader’
read (*, *) alphadeg F¨rs¨ttsblad
o a
alpha0 = alphadeg*pi/180 ! R¨knar om vinkeln fr˚n grader
a a
Inneh˚
all
! till radianer
x0(1) = z0
x0(2) = alpha0 ! S¨tter in startv¨rdena i en vektor
a a
open(unit = 15, file=’matG.m’, status=’unknown’, &
Sida 10 av 22
& action=’write’, iostat=oerr)
write (15, *) ’G=[’ Tillbaka
call rootz31(MyG, x0, tol, maxIter, xst, ierr)
write (15, *) ’];’ Full Screen
close(15)
write (*, *) ’V¨rdena f¨r x- och y-koordinater ¨r&
a o a St¨ng
a
& skrivna i filen matG.m.’
Avsluta
11. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
p3: (forts) F¨rs¨ttsblad
o a
z = xst(1)
Inneh˚
all
alphast = xst(2) ! F˚r ut de slutliga v¨rdena i form av
a a
! en vektor
alphastdeg = alphast*180/pi ! R¨knar om vinkeln fr˚n
a a
! radianer till grader
write (*, *) ’Bollen m˚ste skickas iv¨g med vinkeln’, &
a a
Sida 11 av 22
& alphastdeg, ’grader f¨r att tr¨ffa korgen’
o a
write (*, *) ’Tiden det tar f¨r bollen att n˚ korgen &
o a Tillbaka
&¨r’, z, ’sekunder.’
a
Full Screen
St¨ng
a
Avsluta
12. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
subroutine rootz31(f, x0, tol, maxiter, xst, ierr): maxIndex.f90 DG.f90
F(s)
tol2 = tol**2
Hemsida
x = x0 F¨rs¨ttsblad
o a
do i = 1, maxIter
call f(x, fx, printing) Inneh˚
all
call jacobi(f, fx, x, jx)
call gauss(jx, fx, deltax, r)
x = x - deltax
if (pyts(deltax) < tol2) then
ierr = 0 Sida 12 av 22
xst = x
return Tillbaka
end if
Full Screen
end do
St¨ng
a
ierr = 1
Avsluta
13. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
F¨rs¨ttsblad
o a
Inneh˚
all
real(kind = 8) function pyts(x):
real(kind = 8), intent(in):: x(2)
pyts = x(1)*x(1) + x(2)*x(2)
Sida 13 av 22
Tillbaka
Full Screen
St¨ng
a
Avsluta
14. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
subroutine jacobi(f, fx, x, J): F¨rs¨ttsblad
o a
E(1,1) = 1
E(1,2) = 0 Inneh˚
all
E(2,1) = 0
E(2,2) = 1
do i = 1, n
xplushe = x + h*E(:, i) Sida 14 av 22
call f(xplushe, fxplushe, printing)
J(:, i) = (fxplushe - fx)/h Tillbaka
end do
Full Screen
St¨ng
a
Avsluta
15. p3.f90
subroutine MyG(z0alpha0, xy, printing):
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
z0 = z0alpha0(1)
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
alpha0 = z0alpha0(2)
Hemsida
G0(1) = x0
G0(2) = y0 F¨rs¨ttsblad
o a
G0(3) = z0
G0(4) = s0*cos(alpha0); ! vx0, alpha0 from ground plane Inneh˚
all
G0(5) = s0*sin(alpha0); ! vy0
ts = (/ 0, 1 /)
call eulexpl(DG, ts, G0, divs, t, G)
if (printing) then
do i = 1, size(G, 1) Sida 15 av 22
write (unit=15, fmt=’(f20.14)’, advance=’no’) &
& G(i, 1) Tillbaka
if (mod(i, 3) == 0) then
Full Screen
write (15, *) ’ ...’
end if St¨ng
a
end do
write (15, *) ’;’ Avsluta
16. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
subroutine MyG(z0alpha0, xy, printing): (forts)
do i = 1, size(G, 1) F¨rs¨ttsblad
o a
write (unit=15, fmt=’(f20.14)’, advance=’no’) &
Inneh˚
all
& G(i, 2)
if (mod(i, 3) == 0) then
write (15, *) ’ ...’
end if
end do
Sida 16 av 22
write (15, *) ’;’
end if
Tillbaka
xy(1) = G(size(G, 1), 1) - x_B
xy(2) = G(size(G, 1), 2) - y_B Full Screen
St¨ng
a
Avsluta
17. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
subroutine eulexpl(yprim, ts, y0, divs, tret, Yret):
h = (ts(2) - ts(1))/divs ! h ¨r stegl¨ngden
a a F¨rs¨ttsblad
o a
! skapar en vektor t med tidpunkter
Inneh˚
all
do i = 1, divs+1
tret(i) = (i-1)*h+ts(1)
end do
Yret(1, :) = y0 !tilldelning av initialv¨rde a
do i = 1, divs
Sida 17 av 22
Ya = Yret(i,:) !(a = aktuell)
call yprim(tret(i), Ya, Yd) Tillbaka
Yret(i+1, :) = Yret(i, :) + Yd*h
end do Full Screen
St¨ng
a
Avsluta
18. subroutine DG(t, G, Gd):
p3.f90
rootz31.f90
x = G(1)
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
y = G(2) F(s)
z = G(3) Hemsida
vx = G(4)
vy = G(5) F¨rs¨ttsblad
o a
Inneh˚
all
xd = vx * z
yd = vy * z
s = sqrt(xd**2 + yd**2)
alpha = acos(xd / s)
Sida 18 av 22
! Gd = 0
Gd(1) = xd Tillbaka
Gd(2) = yd
Gd(3) = 0 Full Screen
Gd(4) = -F(s) * z * cos(alpha)
St¨ng
a
Gd(5) = -F(s) * z * sin(alpha) - gravity
Avsluta
contains
19. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
F¨rs¨ttsblad
o a
Inneh˚
all
real(kind=8) function F(s):
F = (1.0/2.0) * rho * c_w * (pi/4) * d**2 * s**2
Sida 19 av 22
Tillbaka
Full Screen
St¨ng
a
Avsluta
20. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Hemsida
F¨rs¨ttsblad
o a
subroutine gauss(A, b, x, r):
det = A(1,1)*A(2,2) - A(1,2)*A(2,1)
Inneh˚
all
if(abs(det) < tol) then
stop ’Fel i funktionen gauss. Singul¨r matris’
a
end if
x(1) = (b(1)*A(2,2) - b(2)*A(1,2))/det
Sida 20 av 22
x(2) = (b(2)*A(1,1) - b(1)*A(2,1))/det
Tillbaka
r = 2
Full Screen
St¨ng
a
Avsluta
21. p3.f90
rootz31.f90
gauss.f90 jacobi.f90 MyG.f90 pyts.f90
pivot.f90 baksub.f90 eulexpl.f90
maxIndex.f90 DG.f90
F(s)
Genomf¨randeplan
o Hemsida
• Fungerande MATLAB-prototyp F¨rs¨ttsblad
o a
¨
• Overs¨ttning till Fortran 95
a Inneh˚
all
- Konstruktion av interfaces
• Skriva Fortran-program-, subrutin- och funktionsinterfaces
• Konstruktion av skelett av intefaces Sida 21 av 22
• Test av varje enskilt program & subrutin
Tillbaka
• Hops¨ttning av fungerande skelett + test
a
Full Screen
• MATLAB for att visa resultat
¨
St¨ng
a
Avsluta