-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add rust implementation of forward euler #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ Pen Pal | |
Chinmaya Mahesh | ||
Unlambder | ||
Kjetil Johannessen | ||
CDsigma | ||
CDsigma | ||
hsjoihs |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
fn main() { | ||
let mut result = [0.0;100]; | ||
let threshold = 0.01; | ||
let timestep = 0.01; | ||
|
||
solve_euler(timestep, &mut result, 100); | ||
println!("{}", check_result(&result, 100, threshold, timestep)); | ||
} | ||
|
||
|
||
fn solve_euler(timestep: f64, result: &mut [f64], n: usize) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to pass There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops! Thanks |
||
if n != 0 { | ||
result[0] = 1.0; | ||
for i in 1..n { | ||
result[i] = result[i-1] - 3.0 * result[i-1] * timestep; | ||
} | ||
} | ||
} | ||
|
||
|
||
fn check_result(result: &[f64], n: usize, threshold: f64, timestep: f64) -> bool { | ||
let mut is_approx: bool = true; | ||
for i in 0..n { | ||
let solution = (-3.0 * i as f64 * timestep).exp(); | ||
if (result[i] - solution).abs() > threshold { | ||
println!("{} {}", result[i], solution); | ||
is_approx = false; | ||
} | ||
} | ||
|
||
return is_approx; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should consider formatting your code with
rustfmt
so it conforms to the standard styleguide.