Skip to content

Instantly share code, notes, and snippets.

@K-REBO
Created May 18, 2026 02:06
Show Gist options
  • Select an option

  • Save K-REBO/3cc3ebe609fa22dbbfb9f19d707b3bf7 to your computer and use it in GitHub Desktop.

Select an option

Save K-REBO/3cc3ebe609fa22dbbfb9f19d707b3bf7 to your computer and use it in GitHub Desktop.
/*
B4ゼミ: ホモトピー法
次の非線形方程式の海を、ホモトピー法を用いて近似的に求めよ。
```typst
$f(x) = x^3 + x - 1 = 0$
但し、$g(x) = x, H(x,t) = (1-t) g(x) + t f(x)$
```
Hint: 連続法で、「tを0.2刻みで予測子を出し、修正するためにニュートン法を行う」
これを0から1まで5回行う。
*/
fn main() {
println!("");
let mut x:f64 = 0.0;//初期値
let expected_deviation:f64 = 0.01;//期待する精度
// steps
for i in 0..=5 {
let t = (i as f64/ 5.0) as f64;
let mut loop_counter = 0;
println!("====step {}===",i);
loop {
loop_counter += 1;
let x2 = x - (t * x * x * x + x - t)/(3.0 * t * x * x + 1.0);
println!("value calculating: {}",x2);
//期待する誤差以下になれば終了
if (x2 - x).abs() <= expected_deviation {
println!("value: {}",x);
break;
}else {
x = x2;
}
}
println!("t: {}, x: {} loop:{}\n",t,x,loop_counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment