1: 2016-05-19 (木) 20:03:16 osinko |
2: 2016-05-19 (木) 23:07:33 osinko |
| TITLE:プログレス3 | | TITLE:プログレス3 |
| + | #jsmath |
| **二次方程式と確率 [#sb757614] | | **二次方程式と確率 [#sb757614] |
| | | |
| 虚数の情緒P480から7.3.2 虚根の確率より | | 虚数の情緒P480から7.3.2 虚根の確率より |
| + | |
| + | まず、基本的な確認。実根と虚根を持つ二次方程式の例を以下に挙げる |
| + | |
| + | <公式> |
| + | \(判別式 D={ b }^{ 2 }-4ac\quad ,\quad 根の公式 \frac { -b\pm \sqrt { { b }^{ 2 }-4ac } }{ 2a } \) |
| + | |
| + | <実根の例> |
| + | \({ x }^{ 2 }+2x-8=0\quad \Leftrightarrow \quad (x+4)(x-2)\\ x=-4,2\quad D={ 2 }^{ 2 }-4\times -8=36\) |
| + | |
| + | <虚根の例> |
| + | \({ x }^{ 2 }+3x+3=0\quad \Leftrightarrow \quad \frac { -3\pm \sqrt { { 3 }^{ 2 }-4\times 3 } }{ 2 } =\frac { -3\pm \sqrt { 3 } i }{ 2 } \\ x=\frac { -3-\sqrt { 3 } i }{ 2 } ,\frac { -3+\sqrt { 3 } i }{ 2 } \quad \quad D={ 3 }^{ 2 }-4\times 3=-3 \) |
| + | |
| + | <pocketCasでグラフを見て定量、定性を確認> |
| + | &ref(siki1.png); |
| + | |
| + | 本に書いてある仕組みのプログラムをunityで書く |
| + | |
| + | #code(csharp){{ |
| + | using UnityEngine; |
| + | using System.Collections; |
| + | |
| + | public class Niji_rand : MonoBehaviour |
| + | { |
| + | void Start() |
| + | { |
| + | int sampling = 100000; |
| + | for (int i = 0; i < 5; i++) |
| + | { |
| + | print(Test(sampling)); |
| + | } |
| + | } |
| + | |
| + | private float Test(int sampling) |
| + | { |
| + | int count = 0; |
| + | |
| + | for (int i = 0; i < sampling; i++) |
| + | { |
| + | int b = RandomNum(); |
| + | int c = RandomNum(); |
| + | |
| + | if (D(b, c) < 0) count++; //判別式がマイナスの場合虚根となるのでカウントアップ |
| + | } |
| + | return (float)count / (float)sampling; //確率を求める |
| + | } |
| + | |
| + | //判別式 |
| + | private int D(int b, int c) |
| + | { |
| + | return (b * b) - 4 * c; |
| + | } |
| + | |
| + | //-9~9の乱数を返す |
| + | private int RandomNum() |
| + | { |
| + | return Random.Range(0, 9 + 1) * PositiveNegative(); |
| + | } |
| + | |
| + | //指定した表の確率でコイントスして表が出たら正数、裏で負数を返す |
| + | private int PositiveNegative(float headPercent = 0.5f) |
| + | { |
| + | if (Random.value < headPercent) return 1; |
| + | return -1; |
| + | } |
| + | } |
| + | }} |
| + | 出力 |
| + | 0.20434 |
| + | 0.20621 |
| + | 0.20502 |
| + | 0.20486 |
| + | 0.2047 |
| + | |
| + | この後の順次実行のコードの場合 |
| + | |
| + | #code(csharp){{ |
| + | using UnityEngine; |
| + | using System.Collections; |
| + | using System.Collections.Generic; |
| + | |
| + | public class Prob : MonoBehaviour |
| + | { |
| + | |
| + | void Start() |
| + | { |
| + | //intの範囲的に9999で止めている。https://msdn.microsoft.com/ja-jp/library/exx3b86w.aspx?f=255&MSPPError=-2147217396 |
| + | //?? unityは値のオーバーフローをすると実行が無警告で止まる ??? 試しに99999を追加すると無警告ノーコンソールで画面が固まる |
| + | //この場合のクラッシュの特徴としてunity上のインターフェイスの▷ボタン(再生ボタン)を押して処理を中断できなくなる。ちょっと憶えておくとデバックの時困らないかも |
| + | int[] h = { 4, 9, 16, 25, 36, 49, 64, 81, 99, 999, 9999, }; |
| + | |
| + | foreach (int n in h) |
| + | { |
| + | int count = 0; |
| + | int D_count = 0; |
| + | |
| + | for (int b = -n; b < n + 1; b++) |
| + | { |
| + | for (int c = -n; c < n + 1; c++) |
| + | { |
| + | count++; |
| + | if ((b * b - 4 * c) < 0) D_count++; |
| + | } |
| + | } |
| + | print(string.Format("方程式の総数={0} 虚根の場合={1} 虚根の確率={2}", count, D_count, (float)D_count / (float)count)); |
| + | } |
| + | } |
| + | } |
| + | }} |