fork download
  1. #include <stdio.h>
  2. #include <math.h> // 数学関数を使用するために必要
  3.  
  4. // 定数の設定
  5. #define E 5.0 // 電圧 (V)
  6. #define R 30.0 // 抵抗 (Ω)
  7. #define L 0.003 // インダクタンス (H)
  8. #define C 0.0000004 // キャパシタンス (F)
  9. #define DT 0.00002 // 時間増分 (s)
  10. #define T_MAX 0.002 // シミュレーション時間 (s)
  11.  
  12. // 電流I(t)を計算する関数
  13. double calculate_current(double t) {
  14. double alpha = R / (2.0 * L); // 減衰係数
  15. double omega_0 = 1.0 / sqrt(L * C); // 共振角周波数
  16. double omega_d = sqrt(pow(omega_0, 2) - pow(alpha, 2)); // 減衰振動の角周波数
  17.  
  18. // I(t) = (E / L) * exp(-alpha * t) * sin(omega_d * t) / omega_d
  19. return (E / L) * exp(-alpha * t) * sin(omega_d * t) / omega_d;
  20. }
  21.  
  22. int main() {
  23. double t = 0.0; // 時間を初期化
  24.  
  25. // データのヘッダーを出力
  26. printf("time(s),current(A)\n");
  27.  
  28. // 時間ごとに電流を計算して出力
  29. while (t <= T_MAX) {
  30. double current = calculate_current(t); // 電流の計算
  31. printf("%.6f,%.6f\n", t, current); // 時間と電流を出力
  32. t += DT; // 時間を増加
  33. }
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
time(s),current(A)
0.000000,0.000000
0.000020,0.028562
0.000040,0.043554
0.000060,0.043032
0.000080,0.029960
0.000100,0.010455
0.000120,-0.008586
0.000140,-0.021653
0.000160,-0.025989
0.000180,-0.021903
0.000200,-0.012122
0.000220,-0.000553
0.000240,0.009082
0.000260,0.014302
0.000280,0.014373
0.000300,0.010209
0.000320,0.003799
0.000340,-0.002565
0.000360,-0.007021
0.000380,-0.008607
0.000400,-0.007377
0.000420,-0.004202
0.000440,-0.000368
0.000460,0.002879
0.000480,0.004692
0.000500,0.004797
0.000520,0.003474
0.000540,0.001370
0.000560,-0.000755
0.000580,-0.002273
0.000600,-0.002848
0.000620,-0.002482
0.000640,-0.001453
0.000660,-0.000183
0.000680,0.000910
0.000700,0.001538
0.000720,0.001600
0.000740,0.001181
0.000760,0.000491
0.000780,-0.000218
0.000800,-0.000735
0.000820,-0.000942
0.000840,-0.000834
0.000860,-0.000501
0.000880,-0.000081
0.000900,0.000286
0.000920,0.000503
0.000940,0.000533
0.000960,0.000401
0.000980,0.000175
0.001000,-0.000062
0.001020,-0.000237
0.001040,-0.000311
0.001060,-0.000280
0.001080,-0.000173
0.001100,-0.000034
0.001120,0.000090
0.001140,0.000165
0.001160,0.000177
0.001180,0.000136
0.001200,0.000062
0.001220,-0.000017
0.001240,-0.000076
0.001260,-0.000103
0.001280,-0.000094
0.001300,-0.000059
0.001320,-0.000013
0.001340,0.000028
0.001360,0.000054
0.001380,0.000059
0.001400,0.000046
0.001420,0.000022
0.001440,-0.000004
0.001460,-0.000025
0.001480,-0.000034
0.001500,-0.000032
0.001520,-0.000020
0.001540,-0.000005
0.001560,0.000009
0.001580,0.000018
0.001600,0.000020
0.001620,0.000016
0.001640,0.000008
0.001660,-0.000001
0.001680,-0.000008
0.001700,-0.000011
0.001720,-0.000011
0.001740,-0.000007
0.001760,-0.000002
0.001780,0.000003
0.001800,0.000006
0.001820,0.000007
0.001840,0.000005
0.001860,0.000003
0.001880,-0.000000
0.001900,-0.000003
0.001920,-0.000004
0.001940,-0.000004
0.001960,-0.000002
0.001980,-0.000001