Sei sulla pagina 1di 4

Wei Cao

901866879
AME 20214
13 November 2014
Homework 9
For Problem 1, the Euler approximation program from Homework 3 was programmed in languages
alternative to Fortran 90: Fortran 77, C, Microsoft Excel, and VBA. The numerical solution to a linear
mass-spring system can be written as
r r !
m k
y(t) = ẏ0 sin t . (1)
k m

The parameters used to solve the mass-spring system were y0 = 0 m, ẏ0 = 0 m/s, m = 1 kg, and k = 4 N/m.
An Euler approximation be represented as a set of two first order differential equations:
dy
ẏ0 = = v, y(0) = 0, (2)
dt
dv k
= − y, v(0) = ẏ0 . (3)
dt m
The following is the code for the Fortran 77 program.
[wcao@darrow1-p ~/20214-hw11]$ cat stinkyfoot.f
program euler ! beginning of the program
! approximate
! Vo*sqrt(m/k)*sin(sqrt(k/m)*t)
! with Euler’s method
! exact solution is
! y=yo*exp(lambda*t)
implicit none ! force all variables to be declared
integer*4 n
real*4 dt,tstop,yo,Vo,m,k
parameter (dt=2.E-2, tstop=10., yo=0., Vo = 2., m=1., k=4.)
real*4 y,ynew,V,Vnew,t
t = 0. ! set initial time
V = Vo ! set initial velocity
y = yo ! set initial value of y
n=0 ! set initial value of n
!print*,t,y,Vo*sqrt(m/k)*sin(sqrt(k/m)*t) ! print initial values
do while (t<tstop) ! main loop for calculation
ynew = y+V*dt ! calculate the new y
Vnew = V-k/m*y*dt ! calculate the new V
n = n+1 ! update the counter
t = dt*n ! update time
y = ynew ! update y
V = Vnew ! update V
! print*,t,y,Vo*sqrt(m/k)*sin(sqrt(k/m)*t)! print results to screen
enddo ! end the loop
print*,’error for ’,dt,’ ’,y-Vo*sqrt(m/k)*sin(sqrt(k/m)*t)! print the final error
end program euler ! end of program

The following is the C code for the Euler approximation.


[wcao@darrow1-p ~/20214-hw11]$ cat armpithairbraids.c
/*Name: armpithairbraids.c

1
*Description: Approximates
* a mass spring dampening system
* with an initial velocity and
* position.
*/

//Include I/O, math libraries


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

//Define problem parameters


#define dt 0.02f
#define tstop 10.0f
#define yo 0.0f
#define Vo 2.0f
#define m 1.0f
#define k 4.0f

//Main program
int main ( void ){
float y=yo,V=Vo,ynew,Vnew,t=0.;
int n = 0;
// printf("%.8f\t%.8f\t%.8f\n", t, y, Vo*sqrt(m/k)*sin(sqrt(k/m)*t));
do{
ynew = y+V*dt; //New solution
Vnew = V-k/m*y*dt;
n = n+1;
t = dt*n;
y = ynew;
V = Vnew;
// printf("%.8f\t%.8f\t%.8f\n", t, y, Vo*sqrt(m/k)*sin(sqrt(k/m)*t));
} while(t < tstop);
printf("error %.8e\n", y-Vo*sqrt(m/k)*sin(sqrt(k/m)*t));
return 0;
}
[wcao@darrow1-p ~/20214-hw11]$
The following is the code for the VBA script.
Sub Butt_click()

Dim dt As Double
Dim tstop As Double
Dim yo As Double
Dim Vo As Double
Dim m As Double
Dim k As Double

dt = Cells(2, 1)
tstop = Cells(2, 2)
yo = Cells(2, 3)
Vo = Cells(2, 4)
m = Cells(2, 5)
k = Cells(2, 6)

2
Figure 1: A screenshot of the code used for the Euler approximation in Excel.

Dim ynew As Double


Dim Vnew As Double
Dim y As Double
Dim V As Double
Dim t As Double
Dim n As Integer
Dim i As Integer
Dim exact As Double

y = yo
V = Vo
t = 0
n = tstop / dt

Cells(1, 8) = t
Cells(1, 9) = y
Cells(1, 10) = yo

For i = 1 To n

3
t = i * dt
ynew = y + V * dt
Vnew = V - k / m * y * dt
y = ynew
V = Vnew
exact = Vo * Sqr(m / k) * Sin(Sqr(k / m) * t)
Cells((i + 1), 8) = t
Cells((i + 1), 9) = y
Cells((i + 1), 10) = exact
Next i
End Sub
There error for the problems are shown in Table 1.

Language Error
Fortran 77 7.2598457E-04
C 7.26004384e-04
MS Excel 0.000726148
VBA 0.000726148

Table 1: The errors and their respective languages.

Problem 2. A program was written to approximate the Taylor series approximation about x = 0 with
seven non-zero terms. This function was then processed with the f2py Fortran to Python software and
run within in Python. An approximation was then calculated for sinh(3). The equation of the seven-term
Taylor expansion is
x3 x5 x7 x9 x11 x13
sinh x = x+ + + + + + . (4)
3! 5! 7! 9! 11! 13!
The program is written as follows.
[wcao@remote102 ~/20214-hw11]$ cat toenailclippings.f90
real function sinha(x)
implicit none
real, intent(in) :: x
sinha = x + x**3/6. + x**5/120. + x**7/5040. + x**9/362880. + x**11/39916800. + x**13/6227020800.
end function
[wcao@remote102 ~/20214-hw11]$
The program is run as follows.
[wcao@remote102 ~/20214-hw11]$ python
Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import toenailclippings
>>> print toenailclippings.sinha(1)
1.17520105839
>>> print toenailclippings.sinha(2)
3.62686061859
>>> print toenailclippings.sinha(3)
10.0178623199
>>> exit()
[wcao@remote102 ~/20214-hw11]$
The exact solution of sinh(3) is 10.0178749. Compared to the approximation, the error is 1.25801e-5.

Potrebbero piacerti anche