یافتن ریشه معادله — به زبان ساده
در این آموزش، با روش عددی یافتن ریشه معادله آشنا میشویم. میخواهیم معادلهای به فرم $$ f ( x ) = 0 $$ را حل کنیم. بدین منظور از روش سکانت یا وتری (Secant Method) استفاده میکنیم. در ادامه، روش سکانت به طور خلاصه توضیح داده شده است.
یافتن ریشه معادله
همانطور که گفتیم، از روش سکانت یا وتری استفاده میکنیم. با داشتن مقادیر اولیه $$ x _ 0 $$ و $$ x _ 1 $$، معادله خط گذرنده از نقاط $$ ( x _ 1 , f ( x _ 1 ))$$ و $$ ( x _ 2 , f ( x _ 2 )) $$ را به صورت زیر مینویسیم:
$$ \large y = \frac { f ( x _ 2 ) - f ( x _ 1 ) } { x _ 2 - x _ 1 } ( x - x _ 2 ) + f ( x _ 2 ) . $$
ریشه این تابع خطی، مقداری از $$ x $$ است که به ازای آن، $$ y = 0 $$ است:
$$ \large x = x _ 2 - f ( x _ 2 ) \frac { x _ 2 - x _ 1 } { f ( x _ 2 ) - f ( x _ 1 ) } . $$
در ادامه، از مقدار جدید $$ x $$ به عنوان $$ x _ 3 $$ استفاده کرده و روال را به جای $$ x _ 1 $$ و $$ x _2$$، با $$ x _ 2 $$ و $$ x _ 3 $$ تکرار میکنیم. این کار را تا جایی ادامه میدهیم که به دقت مناسبی برسیم و اختلاف بین $$ x _{n-1}$$ و $$ x _ n $$ به مقدار بسیار کوچکی میل کند:
$$ \large \begin {align}
x _ 3 & = x _ 2 - f ( x _ 2 ) \frac { x _ 2 - x _ 1 } { f ( x _ 2 ) - f ( x _ 1 ) } , \\[6pt]
x _ 4 & = x _ 3 - f ( x _ 3 ) \frac { x _ 3 - x _ 2 } { f ( x _ 3 ) - f ( x _ 2 ) } , \\[6pt]
& \, \, \, \vdots \\[6pt]
x _ n & = x _ { n - 1 } - f ( x _ { n - 1 } ) \frac { x _ { n - 1 } - x _ { n - 2 } } { f ( x _ { n - 1 } ) - f ( x _ { n - 2 } ) } .
\end {align} $$
به طور خلاصه میتوان گفت که روش سکانت با رابطه بازگشتی زیر تعریف میشود:
$$ \large \begin {align*} x _ n & = x _ { n - 1 } - f ( x _ { n - 1 } ) \frac { x _ { n - 1 } - x _ { n - 2 } } { f ( x _ { n - 1 } ) - f ( x _ { n - 2 } ) }
\\ & = \frac { x _ { n - 2 } f ( x _ { n - 1 } ) - x _ { n - 1 } f ( x _ { n - 2 } ) } { f ( x _ { n - 1 } ) - f ( x _ { n - 2 } ) } . \end {align*} $$
همانطور که از این رابطه بازگشتی میبینیم، روش سکانت به دو مقدار اولیه $$ x _ 1 $$ و $$ x _ 2 $$ نیاز دارد و این مقادیر باید به گونهای انتخاب شوند که نزدیک ریشه باشند.
پیادهسازی یافتن ریشه معادله در زبانهای برنامهنویسی مختلف
در ادامه، برنامه یافتن ریشه معادله در زبانهای برنامهنویسی مختلف ارائه شده است.
Input : equation = x3 + x - 1 x1 = 0, x2 = 1, E = 0.0001 Output : Root of the given equation = 0.682326 No. of iteration=5
برنامه یافتن ریشه معادله در ++C
1// C++ Program to find root of an
2// equations using secant method
3#include <bits/stdc++.h>
4using namespace std;
5// function takes value of x and returns f(x)
6float f(float x)
7{
8 // we are taking equation as x^3+x-1
9 float f = pow(x, 3) + x - 1;
10 return f;
11}
12
13void secant(float x1, float x2, float E)
14{
15 float n = 0, xm, x0, c;
16 if (f(x1) * f(x2) < 0) {
17 do {
18 // calculate the intermediate value
19 x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
20
21 // check if x0 is root of equation or not
22 c = f(x1) * f(x0);
23
24 // update the value of interval
25 x1 = x2;
26 x2 = x0;
27
28 // update number of iteration
29 n++;
30
31 // if x0 is the root of equation then break the loop
32 if (c == 0)
33 break;
34 xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
35 } while (fabs(xm - x0) >= E); // repeat the loop
36 // until the convergence
37
38 cout << "Root of the given equation=" << x0 << endl;
39 cout << "No. of iterations = " << n << endl;
40 } else
41 cout << "Can not find a root in the given inteval";
42}
43
44// Driver code
45int main()
46{
47 // initializing the values
48 float x1 = 0, x2 = 1, E = 0.0001;
49 secant(x1, x2, E);
50 return 0;
51}
52 <div class="faradars-courses faradars-courses-single">
53 <a href="https://faradars.org/courses/fvrnm101-numerical-root-finding-methods-using-matlab-implementation?utm_source=blog.faradars&utm_medium=referral-post&utm_campaign=related-courses-inline-5&utm_term=s.hamidi&utm_content=programming_mathematics_fundamental-science" target="_blank">
54 <div class="card card-course">
55 <div class="card-image">
56 <img class="pop-img" src="https://faradars.org/wp-content/uploads/2015/06/fvrnm101ab-svg.svg" alt="آموزش روش های عددی ریشه یابی و حل معادلات و پیاده سازی در متلب MATLAB">
57 </div>
58 <div class="card-body">
59 <div class="card-title">
60 فیلم آموزش روش های عددی ریشه یابی و حل معادلات و پیاده سازی در متلب MATLAB در فرادرس
61 </div>
62 </div>
63 <div class="card-action ml-3">
64 <div class="fdb-btn">کلیک کنید</div>
65 </div>
66 </div>
67 </a>
68 </div>
69
برنامه یافتن ریشه معادله در Java
1// Java Program to find root of an
2// equations using secant method
3class GFG {
4
5 // function takes value of x and
6 // returns f(x)
7 static float f(float x) {
8
9 // we are taking equation
10 // as x^3+x-1
11 float f = (float)Math.pow(x, 3)
12 + x - 1;
13
14 return f;
15 }
16
17 static void secant(float x1, float x2,
18 float E) {
19
20 float n = 0, xm, x0, c;
21 if (f(x1) * f(x2) < 0)
22 {
23 do {
24
25 // calculate the intermediate
26 // value
27 x0 = (x1 * f(x2) - x2 * f(x1))
28 / (f(x2) - f(x1));
29
30 // check if x0 is root of
31 // equation or not
32 c = f(x1) * f(x0);
33
34 // update the value of interval
35 x1 = x2;
36 x2 = x0;
37
38 // update number of iteration
39 n++;
40
41 // if x0 is the root of equation
42 // then break the loop
43 if (c == 0)
44 break;
45 xm = (x1 * f(x2) - x2 * f(x1))
46 / (f(x2) - f(x1));
47
48 // repeat the loop until the
49 // convergence
50 } while (Math.abs(xm - x0) >= E);
51
52 System.out.println("Root of the" +
53 " given equation=" + x0);
54
55 System.out.println("No. of "
56 + "iterations = " + n);
57 }
58
59 else
60 System.out.print("Can not find a"
61 + " root in the given inteval");
62 }
63
64 // Driver code
65 public static void main(String[] args) {
66
67 // initializing the values
68 float x1 = 0, x2 = 1, E = 0.0001f;
69 secant(x1, x2, E);
70 }
71}
72
73// This code is contributed by Anant Agarwal.
برنامه یافتن ریشه معادله در Python
1# Python3 program to find the values of
2# X and Y using the given equations
3
4# Function to find the values of X and Y
5def findValues(a, b):
6
7 # base condition
8 if ((a - b) % 2 == 1):
9 print("-1");
10 return;
11
12 # required answer
13 print((a - b) // 2, (a + b) // 2);
14
15# Driver Code
16a = 12; b = 8;
17
18findValues(a, b);
19
20# This code is contributed
21# by Akanksha Rai
برنامه یافتن ریشه معادله در #C
1// C# Program to find root of an
2// equations using secant method
3using System;
4
5class GFG {
6
7 // function takes value of
8 // x and returns f(x)
9 static float f(float x)
10 {
11
12 // we are taking equation
13 // as x^3+x-1
14 float f = (float)Math.Pow(x, 3)
15 + x - 1;
16 return f;
17 }
18
19 static void secant(float x1, float x2,
20 float E)
21
22 {
23
24 float n = 0, xm, x0, c;
25 if (f(x1) * f(x2) < 0)
26 {
27 do {
28
29 // calculate the intermediate
30 // value
31 x0 = (x1 * f(x2) - x2 * f(x1))
32 / (f(x2) - f(x1));
33
34 // check if x0 is root of
35 // equation or not
36 c = f(x1) * f(x0);
37
38 // update the value of interval
39 x1 = x2;
40 x2 = x0;
41
42 // update number of iteration
43 n++;
44
45 // if x0 is the root of equation
46 // then break the loop
47 if (c == 0)
48 break;
49 xm = (x1 * f(x2) - x2 * f(x1))
50 / (f(x2) - f(x1));
51
52 // repeat the loop until
53 // the convergence
54 } while (Math.Abs(xm - x0) >= E);
55
56 Console.WriteLine("Root of the" +
57 " given equation=" + x0);
58
59 Console.WriteLine("No. of " +
60 "iterations = " + n);
61 }
62
63 else
64 Console.WriteLine("Can not find a" +
65 " root in the given inteval");
66 }
67
68 // Driver code
69 public static void Main(String []args)
70 {
71
72 // initializing the values
73 float x1 = 0, x2 = 1, E = 0.0001f;
74 secant(x1, x2, E);
75 }
76}
77
78// This code is contributed by vt_m.
برنامه یافتن ریشه معادله در PHP
1<?php
2// PHP Program to find root of an
3// equations using secant method
4
5// function takes value of x
6// and returns f(x)
7function f( $x)
8{
9
10 // we are taking equation
11 // as x^3+x-1
12 $f = pow($x, 3) + $x - 1;
13 return $f;
14}
15
16function secant($x1, $x2, $E)
17{
18 $n = 0; $xm;
19 $x0; $c;
20 if (f($x1) * f($x2) < 0)
21 {
22 do {
23
24 // calculate the intermediate value
25 $x0 = ($x1 * f($x2) - $x2 *
26 f($x1)) / (f($x2) - f($x1));
27
28 // check if x0 is root
29 // of equation or not
30 $c = f($x1) * f($x0);
31
32 // update the value of interval
33 $x1 = $x2;
34 $x2 = $x0;
35
36 // update number of iteration
37 $n++;
38
39 // if x0 is the root of equation
40 // then break the loop
41 if ($c == 0)
42 break;
43 $xm = ($x1 * f($x2) - $x2 * f($x1)) /
44 (f($x2) - f($x1));
45
46 // repeat the loop
47 // until the convergence
48 } while (abs($xm - $x0) >= $E);
49
50 echo "Root of the given equation=". $x0."\n" ;
51 echo "No. of iterations = ". $n ;
52
53 } else
54 echo "Can not find a root in the given inteval";
55}
56
57// Driver code
58{
59
60 // initializing the values
61 $x1 = 0; $x2 = 1;
62 $E = 0.0001;
63 secant($x1, $x2, $E);
64 return 0;
65}
66
67// This code is contributed by nitin mittal.
68?>
اگر این مطلب برایتان مفید بوده است، آموزشهای زیر نیز به شما پیشنهاد میشوند:
- مجموعه آموزشهای محاسبات عددی
- آموزش محاسبات عددی با MATLAB
- مجموعه آموزشهای دروس ریاضیات
- آموزش ریاضی پایه دانشگاهی
- روش نیوتن — به زبان ساده
- روش دو بخشی — به زبان ساده
- جذر یا محاسبه ریشه دوم عدد — به زبان ساده
^^