Skip to content

Instantly share code, notes, and snippets.

@rthbound
Last active August 1, 2016 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rthbound/65e17675dc95d3a6d25fc254ce215265 to your computer and use it in GitHub Desktop.
Save rthbound/65e17675dc95d3a6d25fc254ce215265 to your computer and use it in GitHub Desktop.
attempting to use Newton#nlsolve ... got it, i think. it's fastest when the initial guess is close or the allowed error is huge :)
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'dentaku'
end
require 'bigdecimal/newton'
require 'dentaku'
class Channel
def initialize
@flow = 15
@z_left = 3
@z_right = 3
@bottom_width = 3
@mannings = 0.024
@conversion = 1.486
@bed_slope = 0.1
@zero = BigDecimal::new("0.0")
@one = BigDecimal::new("1.0")
@two = BigDecimal::new("2.0")
@ten = BigDecimal::new("10.0")
@eps = BigDecimal::new("1.0e-3")
end
def eps; @eps end
def one; @one end
def two; @two end
def ten; @ten end
def zero; @zero end
def values(d)
area = Dentaku("(0.5 * z_left * d^2) + (0.5 * z_right * d^2) + (bottom_width * d)", {
z_left: @z_left,
z_right: @z_right,
bottom_width: @bottom_width,
d: d[0]
})
wet_perimeter = Dentaku("((((z_left * d) ^ 2) + d^2)^0.5) + ((((z_right * d) ^ 2) + d^2)^0.5) + bottom_width", {
z_left: @z_left,
z_right: @z_right,
bottom_width: @bottom_width,
d: d[0]
})
value = Dentaku("(1/mannings) * conversion * (bed_slope ^ 0.5) * area * (( area / wet_perimiter ) ^ (2/3)) - flow", {
flow: @flow,
mannings: @mannings,
conversion: @conversion,
bed_slope: @bed_slope,
area: area,
wet_perimiter: wet_perimeter
})
[value]
end
end
include Newton
c = Channel.new
x = [0.3]
nlsolve(c, x)
p "Channel height is: #{x[0].to_f}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment