Skip to content

Commit

Permalink
adjusted for 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
keesvp committed Oct 21, 2013
1 parent 7cf64e1 commit 9ca925a
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 56 deletions.
9 changes: 8 additions & 1 deletion advanced/7-apollo/constants.jl
@@ -1,3 +1,8 @@
module constants

export ME, RE, G, MM, RM, MCM, DISTANCE_TO_MOON, MOON_PERIOD, MOON_INITIAL_ANGLE, ORIGIN
export TOTAL_DURATION, MARKER_TIME, TOLERANCE, INITIAL_POSITION, INITIAL_VELOCITY

const ME = 5.97e24 # mass of earth in kg
const RE = 6.378e6 # radius of earth in m (at equator)
const G = 6.67e-11 # gravitational constant in m3 / kg s2
Expand All @@ -14,4 +19,6 @@ const MARKER_TIME = 0.5 * 3600. # (used in error correction) s
const TOLERANCE = 100000. # (used in error correction) m

const INITIAL_POSITION = [-6.701e6, 0.] # Initial vector for the spacecraft in m
const INITIAL_VELOCITY = [0., -10.818e3] # Initial velocity for spacecraft in m/s
const INITIAL_VELOCITY = [0., -10.818e3] # Initial velocity for spacecraft in m/s

end
18 changes: 9 additions & 9 deletions advanced/7-apollo/main.jl
@@ -1,17 +1,17 @@
# Problem: Save the Apollo 13 Astronauts

require("types.jl")
require("constants.jl")
require("physics.jl")
require("moon.jl")
require("command-module.jl")
require("system.jl")
using types
using constants
include("physics.jl")
include("moon.jl")
include("command-module.jl")
include("system.jl")

# initialization of our bodies
earth = Body(ME, [0.0, 0.0], RE, ORIGIN)
moon = Moon(MM, [0., 0.], RM, moon_position(0.0))
command_module = Command_Module(MCM, INITIAL_VELOCITY, 5.0, INITIAL_POSITION, INITIAL_POSITION, INITIAL_POSITION, INITIAL_VELOCITY, INITIAL_VELOCITY)
world = System(0.0, earth, moon, command_module)
world = EarthMoonSystem(0.0, earth, moon, command_module)

function simulate()
boost = 15. # m/s Change this to the correct value from the list above after everything else is done.
Expand Down Expand Up @@ -50,12 +50,12 @@ function simulate()
current_time += h
h = h_new

push(position_list, copy(world.command_module.position))
push!(position_list, copy(world.command_module.position))
end

return position_list
end
println("starting")
@time pos = simulate()
println(typeof(pos))
csvwrite("output.csv", pos)
writecsv("output.csv", pos)
4 changes: 2 additions & 2 deletions advanced/7-apollo/system.jl
@@ -1,8 +1,8 @@
function update(me::System, time::Float64, h::Float64)
function update(me::EarthMoonSystem, time::Float64, h::Float64)
me.time = time

update(me.moon, time)
update(me.command_module, time, h)

return me
end
end
10 changes: 8 additions & 2 deletions advanced/7-apollo/types.jl
@@ -1,3 +1,7 @@
module types

export Body, Moon, Command_Module, EarthMoonSystem

type Body{T}
mass::T
velocity::Vector{T}
Expand All @@ -18,9 +22,11 @@ type Command_Module{T}
velocityH::Vector{T}
end

type System
type EarthMoonSystem
time::Float64
earth::Body
moon::Moon
command_module::Command_Module
end
end

end
14 changes: 7 additions & 7 deletions advanced/8-queuing/main.jl
@@ -1,18 +1,18 @@
require("system.jl")
using queuingSystem

## USER DECISIONS

warm_up_time = 4320.0
warm_up_time = 43.0
run_time = 20160.0

# Standard Deviation = Mean * Coefficient of Variance
coeff_of_variance = 1

# Average Arrival Time
mean_iat = 3
mean_iat = 3.0

# Average length of service
mean_los = 9
mean_los = 9.0

# Number of servers
num_servers = 4
Expand Down Expand Up @@ -43,12 +43,12 @@ for i=1:max_arrivals

# Equivalent to push(arrival_times, prev_arrival)
# Push our random values into an array
arrival_times = push(arrival_times, prev_arrival)
service_times = push(service_times, prev_los)
arrival_times = push!(arrival_times, prev_arrival)
service_times = push!(service_times, prev_los)
end

# Create our new queuing system
qs = Queuing_System(arrival_times, service_times, warm_up_time, run_time, num_servers)

# Run the simulation
run_to_end(qs)
run_to_end(qs)
@@ -1,3 +1,7 @@
module queuingSystem

export random_gaussian, Queuing_System, run_to_end

function random_gaussian(mean::Float64, std_dev::Float64)
mean + (rand() - 0.5) * std_dev
end
Expand Down Expand Up @@ -99,14 +103,11 @@ function next_event(qs::Queuing_System)
if qs.in_system <= qs.servers
# When will the available server finish processing its next customer?
qs.next_completion[qs.open_server] = qs.sim_time + next_service(qs)
speak(qs, strcat("Customer arrived at server ",
qs.open_server,
" will be done at ",
qs.next_completion[qs.open_server]))
speak(qs, "Customer arrived at server $(qs.open_server) will be done at $(qs.next_completion[qs.open_server])")
else
# In the case where we have more customers in the system than servers
# Customers will have to wait in queue
speak(qs,"Customer arrived at an is waiting in line")
speak(qs,"Customer arrived and is waiting in line")
end

else
Expand All @@ -121,13 +122,13 @@ function next_event(qs::Queuing_System)
# Set this to a dummy value for now
qs.next_completion[qs.next_to_complete] = typemax(Int)

speak(qs, strcat("Person exited from server ", qs.next_to_complete))
speak(qs, "Person exited from server $(qs.next_to_complete)")

# If we have more customers in the system than servers
if qs.in_system >= qs.servers
# When will the next available server finish processing its current customer?
qs.next_completion[qs.next_to_complete] = qs.sim_time + next_service(qs)
speak(qs,strcat("Customer exited line to see server ", qs.next_to_complete, " will be done at ", qs.next_completion[qs.next_to_complete]))
speak(qs,"Customer exited line to see server $(qs.next_to_complete) will be done at $(qs.next_completion[qs.next_to_complete])")
end
end

Expand Down Expand Up @@ -169,7 +170,9 @@ end

# Outputs time and a message
function speak(qs::Queuing_System, words::String)
if qs.warmed_up
println(strcat(qs.sim_time, ": ", words))
if qs.warmed_up
println("$(qs.sim_time) : $words")
end
end
end

end
2 changes: 1 addition & 1 deletion beginner/1-helloworld/main.jl
@@ -1 +1 @@
require("hello.jl")
include("hello.jl")
4 changes: 2 additions & 2 deletions beginner/3-quadratic/main.jl
@@ -1,3 +1,3 @@
require("quadratic.jl")
include("quadratic.jl")

println(quadratic((x) -> 2x^2 + 30x + 9))
println(quadratic((x) -> 2x^2 + 30x + 9))
4 changes: 2 additions & 2 deletions beginner/3-quadratic/quadratic.jl
@@ -1,4 +1,4 @@
require("derivative.jl")
include("derivative.jl")

function quadratic(f)
# Compute the first derivative of f
Expand All @@ -17,4 +17,4 @@ function quadratic(f)
# Multiple values should be separated by a comma and
# are returned as a tuple
return (-b + sqrt(b^2 -4a*c + 0im)) / 2a, (-b - sqrt(b^2 -4a*c + 0im)) / 2a
end
end
6 changes: 3 additions & 3 deletions intermediate/4-statistics/main.jl
@@ -1,6 +1,6 @@
require("ols.jl")
using ols

data = csvread("gasoline.csv")
data = readcsv("gasoline.csv")

# Get the columns that correspond with our X values
# and create a new matrix to hold them
Expand All @@ -11,5 +11,5 @@ x = [data[:,2] data[:,3] data[:,4]]
y = data[:,6]

# Create a new OLS object
reg = ols(y, x, "Octane Rating", ["Error", "Component 1", "Component 2", "Component 3"])
reg = tols(y, x, "Octane Rating", ["Error", "Component 1", "Component 2", "Component 3"])
summary(reg)
40 changes: 23 additions & 17 deletions intermediate/4-statistics/ols.jl
@@ -1,3 +1,7 @@
module ols

export tols, summary

# Author: Adam Savitzky
# Email: asavitzky@forio.com
# Github: github.com/adambom
Expand All @@ -24,28 +28,28 @@
### F-Statistic: reg.F
### Summary: summary(reg)

type ols
y::Array{Float}
x::Array{Float}
type tols
y::Array{Float64}
x::Array{Float64}
y_varnm::String
x_varnm::Array{String, 1}
inv_xx::Array{Float}
b::Array{Float, 1}
inv_xx::Array{Float64}
b::Array{Float64, 1}
nobs::Int
ncoef::Int
df_e::Int
df_r::Int
er::Array
sse::Float
se::Array{Float, 1}
t::Array{Float}
sse::Float64
se::Array{Float64, 1}
t::Array{Float64}
#p::Array
R2::Float
R2adj::Float
F::Float
#Fpv::Float
R2::Float64
R2adj::Float64
F::Float64
#Fpv::Float64

function ols(y, x, y_varnm, x_varnm)
function tols(y, x, y_varnm, x_varnm)
x = hcat(ones(size(x, 1)), x)
xT = transpose(x)

Expand Down Expand Up @@ -74,14 +78,14 @@ type ols
end
end

function dw(self::ols)
function dw(self::tols)
# Calculates the Durbin-Waston statistic
de = self.er - 1.
result = dot(de, de) / dot(self.er, self.er)
return result
end

function ll(self::ols)
function ll(self::tols)
# Calculate model log-likelihood and two information criteria

# Model log-likelihood, AIC, and BIC criterion values
Expand All @@ -92,7 +96,7 @@ function ll(self::ols)
return loglike, aic, bic
end

function summary(self::ols)
function summary(self::tols)
# print model output to screen

t = time()
Expand Down Expand Up @@ -128,4 +132,6 @@ end

function linreg{T<:Number}(X::StridedVecOrMat{T}, y::Vector{T})
hcat(ones(T, size(X,1)), X)\y
end
end

end

0 comments on commit 9ca925a

Please sign in to comment.