Last active
September 13, 2021 15:12
-
-
Save sonsongithub/e7694ab05cdebb0f1795301ac2cdb182 to your computer and use it in GitHub Desktop.
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
| // 接線の傾き | |
| fn get_a(x:&f32) -> f32 { | |
| return 3.0 * x * x + 2.0 * x + 2.0; | |
| } | |
| // 接線の切片 | |
| fn get_b(x:&f32, n: &f32) -> f32 { | |
| return x * x * x + x * x + 2.0 * x + (3.0 - n) - x * get_a(x); | |
| } | |
| // f(x) = x^3 + x^2 + 2x + (3-n) | |
| // f(x)は,単調増加関数で,かつ,x->-∞のときにf(x)<0かつx->∞のときにf(x)>0かつf(x)は連続関数なので,f(x)=0は,ただ一つ解をもつ. | |
| // ゆえに,どこから解の探索を始めてもかまわない. | |
| fn main() { | |
| let mut x: f32 = 0.0; | |
| let n: f32 = 1000000.0; | |
| loop { | |
| // 接線のX軸との交点の座標を求める | |
| let new_x = - get_b(&x, &n) / get_a(&x); | |
| println!("x = {}", x); | |
| if (new_x - x).abs() < 0.01 { | |
| x = new_x; | |
| break; | |
| } | |
| x = new_x; | |
| } | |
| println!("Ans. x = {}", x); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment