Last active
September 26, 2020 10:19
-
-
Save mathisve/7998c03d29bf1a616bb6d22f8922e920 to your computer and use it in GitHub Desktop.
calculate displacement at time t using functions as variables in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func main() { | |
var acceleration, velocity, displacement, time float64 | |
fmt.Print("Enter the acceleration: ") | |
fmt.Scan(&acceleration) | |
fmt.Print("Enter the velocity: ") | |
fmt.Scan(&velocity) | |
fmt.Print("Enter the displacement: ") | |
fmt.Scan(&displacement) | |
fmt.Print("Enter the time: ") | |
fmt.Scan(&timeS) | |
fn := GenDisplaceFn(acceleration, velocity, displacement) | |
fmt.Printf("The displacement at time t=%s is: %v\n", time, fn(time)) | |
} | |
func GenDisplaceFn(acceleration, velocity, displacement float64) func (time float64) (float64) { | |
return func (time float64) (float64){ | |
return float64(1/2) * acceleration * math.Exp(time) + velocity * time + displacement | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment