C# 程式找出 Sin(x) 的值

PHPz
發布: 2023-08-26 11:25:02
轉載
1528 人瀏覽過

C# 程序找出 Sin(x) 的值

簡介

在本文中,我們將了解 C# 程式來找出 Sin(x) 的值。正弦是 Sin(x) 的另一個名稱。這是一個三角角公式。角的正弦是直角梯形中斜邊長度與垂線長度的比例。強大的電腦語言 C# 可用於解決具有挑戰性的數學問題。求 sin(x) 的值(其中 x 是任意以弧度表示的角度)就是這些問題之一。在本文中,我們將了解如何使用 Math 函式庫建立計算 sin(x) 值的 C# 程式。也將涵蓋 sin 函數的數學基礎,以及它在現實世界中的一些應用。無論您是新手還是經驗豐富的程式設計師,本文都會為您提供有關如何使用 C# 進行計算的有用提示。那麼讓我們開始學習如何在 C# 中計算 sin(x)

方法

透過使用內建的 sin() 函數,我們可以確定角度的正弦值。此方法在 Math 類別下指定,並且是系統命名空間的一部分。因為它涵蓋了常數和一些靜態三角、對數和其他方法,所以數學教學非常有幫助。

除了在我們的程式碼中直接使用的這個方法之外,考慮到輸出控制台,還有一個很重要的方法,那就是 -

透過使用麥克勞林展開式,我們可以確定角度的正弦值。因此,sin(x) 的麥克勞林級數擴展為

演算法

要計算 sin(x) 的值,請依照下列說明操作 -

第 1 步  將要計算的角度(以度為單位)設定為變數angleInDegree。

第 2 步  建立一個名為 terms 的新變量,用於儲存我們可以使用多少項來估計 sin.(x) 的值。

第 3 步 宣告 findSinx 全域函數。

第 4 步  建立波動流。方向以弧度保存。

第 5 步  使用 current 初始化變數回應。它將保存我們的完整回應。

第 6 步  使用 current 初始化另一個變數的溫度。

第 7 步  從第 1 項重複到第 i 項。將每個階段的溫度更新為 ((-temp) * current * current) / ((2 * i) * (2 * i 1)),並將答案更新為 ((answer temp))。

第 8 步  最後,給出 findSinX 方法的結果。

步驟 9  列印解決方案。

範例

// C# program to illustrate how we can
// calculate the value of sin(x)
// using Maclaurin's method

using System;
class SINE{
   static double findSinX(int angleInDegree, int terms) {

      // Converting angle in degree into radian
      double current = Math.PI * angleInDegree / 180f;

      // Declaring variable to calculate final answer
      double answer = current;
      double temp = current;

      // Loop till number of steps provided by the user
      for(int i = 1; i <= terms; i++) {

         // Updating temp and answer accordingly
         temp = ((-temp) * current * current) / ((2 * i) * (2 * i + 1)); 
         answer = answer + temp;
      }
      
      // Return the final answer
      return answer;
   }
   
   // Driver code
   static public void Main() {

      // Angle in degree
      int angleInDegree1 = 45;
      
      // Number of steps
      int terms1 = 10;
      
      // Calling function to calculate sine of angle
      double answer1 = findSinX(angleInDegree1, terms1);
      
      // Print the final answer
      Console.WriteLine("The value of sin({0}) = {1}", angleInDegree1, answer1);

      // Angle in degree
      int angleInDegree2 = 90;

      // Number of steps
      int terms2 = 20;
      
      // here we are calling function to calculate sine of angle
      double result2 = findSinX(angleInDegree2, terms2);
      
      // Print the final answer
      Console.WriteLine("The value of sin({0}) = {1}",
      angleInDegree2, result2);

      // Angle in degree
      int angleInDegree3 = 135;
      
      // Number of steps
      int terms3 = 30; 
      
      // Calling function to calculate sine of angle
      double result3 = findSinX(angleInDegree3, terms3);
      
      // Print the final answer
      Console.WriteLine("The value of sin({0}) = {1}",  angleInDegree3, result3);

      // Angle in degree
      int angleInDegree4 = 180;
      
      // Number of steps
      int terms4 = 40;

      // Calling function to calculate sine of angle
      double result4 = findSinX(angleInDegree4, terms4);
      
      // Print the final answer
      Console.WriteLine("The value of sin({0}) = {1}",  angleInDegree4, result4);
   }
}
登入後複製

輸出

The value of sin(45) = 0.707106781186547
The value of sin(90) = 1
The value of sin(135) = 0.707106781186548
The value of sin(180) = 2.34898825287367E-16
登入後複製

時間複雜度

在這個求 Sin(x) 值的特定程式中,我們得到了時間複雜度:O(n)。 //n 是作為輸入傳遞的術語數。

空間複雜度為O(1)。

結論

總之,建立 C# 程式來計算 sin(x) 是一個相當簡單的過程,可以使用 Math 函式庫來執行。程式設計師可以透過理解 sin 函數背後的數學思想,利用這些知識來建立更複雜的數學演算法和應用程式。

工程、物理學和電腦圖形學只是了解如何計算 sin 值的一些現實用途。例如,正弦函數經常用於模擬波動、提供視覺效果和管理機器人系統。

總之,學習如何使用 sin 函數和 C# 程式語言可以為程式設計師提供一組寶貴的能力,這些能力可用於解決各個領域的各種複雜的數學問題。

以上是C# 程式找出 Sin(x) 的值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!