2019年3月27日 星期三

習題1-7 (b) H'n(x)的計算 


Hn(x)=




到google " online differential equation of first order" 搜尋
進入
https://www.symbolab.com/solver/linear-first-order-differential-equation-calculator



 
輸入資料

\left(x-1\right)+\left(x-1\right)^2\cdot \left(-0.307\right)+\left(x-2\right)\left(x-1\right)^2\cdot \left(0.114\right)\:+\left(-0.0323\right)\left(x-2\right)^2\left(x-1\right)^2+\left(x-1\right)^2\left(x-2\right)^2\left(x-3\right)\left(0.0091\right)+\left(x-1\right)^2\left(x-2\right)^2\left(x-3\right)^2\left(-0.0091\right)+\left(x-1\right)^2\left(x-2\right)^2\left(x-3\right)^2\left(x-4\right)\left(0.0004\right)




計算結果為
0.0004x^7-0.0155x^6+0.1607x^5-0.7924x^4+2.2079x^3-3.7649x^2+4.3624x-2.1586

=0.0004x^7-0.0155x^6+0.1607x^5-0.7924x^4+2.2079x^3-3.7649x^2+4.3624x-2.1586







2019年3月26日 星期二

C語言 例題 EX2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0

C語言 例題 EX2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0

/* ex2-9.c is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
 */
#include <stdio.h>
#include <math.h>
#define MAX  50
#define TOL 0.0001
#define  g(x)   (1/pow(5,x))
void main()
{
   int i=1;
   double x0,x;
   x0=0.45;
   while(i<=MAX)
   {
      x=g(x0);
      printf("%-2d  %10.7lf\n",i-1,x0);
      if(fabs(x-x0) < TOL)
      {
         printf("The Root=%10.7lf  x-x0=%10.7lf\n",x,fabs(x-x0));
         exit(0);
      }
      i++;
      x0=x;
   }
   printf("Fixed-point failed after %d iteration.\n",i);
   return;
}

輸入畫面  : 無
   
輸出畫面
0    0.4500000
1    0.4846894
2    0.4583705
3    0.4782035
4    0.4631803
5    0.4745160
6    0.4659374
7    0.4724151
8    0.4675155
9    0.4712167
10   0.4684181
11   0.4705327
12   0.4689340
13   0.4701421
14   0.4692289
15   0.4699191
16   0.4693974
17   0.4697917
18   0.4694936
19   0.4697189
20   0.4695486
21   0.4696773
The Root= 0.4695801  x-x0= 0.0000973

2-10 定點迴路法 C語言求根

2-10 定點迴路法 C語言求根
Fixed-Point Iteration. Root finding.

Algorithm:

INPUT initial approximation p0; tolerance TOL; maximum number of iterations N0.
OUTPUT approximate solution p or message of failure.

Step 1 Set i=1.
Step 2 While i <= N0 do Steps 3-6.
Step 3 Set p=g(p0). (Compute pi.)
Step 4 If |p-p0|<tol mode="hold"> OUTPUT (p); (The procedure was successful.)
STOP.
Step 5 Set i=i+1.
Step 6 Set p0=p. (Update p0.)
Step 7 OUTPUT ('The method failed after N0 iterations, N0=', N0);
(The procedure was unsuccessful.)
STOP. 
程式
#include <stdio.h>
#include <math.h>

double f(double x)
{
return x*x*x*x-3*x*x-3;  //change equation for each problem
}

double g(double x)
{
return pow(3*x*x+3,.25);
}

int main()
{
    double p, p0, Tol;
    int i=1;
    int No;

    //printf("Enter approximate p: ");
    //scanf ("%lf", &p0);

    //printf("Desired Tolerance: ");
    //scanf ("%lf", &Tol);

    //printf("Maximum Iterations: ");
    //scanf ("%d", &No);
    scanf ("%lf %lf %d",&p0,&Tol, &No);

    while (i<=No)
    {
        p = g(p0);

        if((fabs(p-p0))<Tol)
        {
            //printf("%lf", &p);
            break;
        }
        printf("Iteration %d: Current value = %lf\n", i, p);

        i++;  //i=i+1
        p0=p;

        if (i>No)
        {
        printf("Method Failed after %d", No);
        printf(" iterations");
        }

    }

}

輸入資料
10.0 0.001 50
輸出結果
Iteration 1: Current value = 4.172157
Iteration 2: Current value = 2.725997
Iteration 3: Current value = 2.242595
Iteration 4: Current value = 2.062271
Iteration 5: Current value = 1.992422
Iteration 6: Current value = 1.965006
Iteration 7: Current value = 1.954192
Iteration 8: Current value = 1.949919
Iteration 9: Current value = 1.948229

C語言 例題2-7已知方程式 e^x + x^-2 + 2 cosx -6 正割法(secant method)

C語言 例題2-7已知方程式 e^x + x^-2 + 2 cosx -6  正割法(secant method)

利用正割法(secant method) 採用誤差0.00001 找出f(x)=0的根 

程式
// C Program to find root of an equations using secant method 
#include <stdio.h>
#include <math.h>


// function takes value of x and returns f(x) 
float f(float x) 
{ 
    // we are taking equation as  e^x + x^-2 + 2 cosx -6
    float f =  exp(x) + (1/pow(x,2)) + 2*cos(x) - 6.0;
    return f; 
} 
  
void secant(float x1, float x2, float E) 
{ 
    float n = 0, xm, x0, c; 
    if (f(x1) * f(x2) < 0)
    { 
        do { 
            // calculate the intermediate value 
            x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); 
  
            // check if x0 is root of equation or not 
            c = f(x1) * f(x0); 
  
            // update the value of interval 
            x1 = x2; 
            x2 = x0; 
  
            // update number of iteration 
            n++; 
  
            // if x0 is the root of equation then break the loop 
            if (c == 0) 
                break; 
            xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
            printf("xm=%0.5lf\n", xm); 
        
            
        } while (fabs(xm - x0) >= E); // repeat the loop 
                                // until the convergence 
  
        printf("Root of the given equation=%0.5lf", x0); 
        printf("\nNo. of iterations = %0.0lf", n); 
    
        
    } else
        
        printf("Can not find a root in the given inteval"); 
} 
  
// Driver code 
int main() 
{ 
    // initializing the values 
    float x1 = 1.8, x2 = 2.0, E = 0.0001; 
    secant(x1, x2, E); 
    return 0; 
} 

輸出畫面

xm=1.82444                                                                                                                                       
xm=1.82498                                                                                                                                       
xm=1.82498                                                                                                                                       
Root of the given equation=1.82498                                                                                                               
No. of iterations = 3                                                                                                                            
                                                                                                                                                 
...Program finished with exit code 0                                                                                                             
Press ENTER to exit console.      



C語言 正割法 Program to find root of an equations using secant method

2-7 正割法 Program to find root of an equations using secant method

源自於

Examples :
Input : equation = x3 + x - 1 
        x1 = 0, x2 = 1, E = 0.0001
Output : Root of the given equation = 0.682326
         No. of iteration=5
Algorithm
Initialize: x1, x2, E, n         // E = convergence indicator
calculate f(x1),f(x2)

if(f(x1) * f(x2) = E); //repeat the loop until the convergence
    print 'x0' //value of the root
    print 'n' //number of iteration
}
else
    print "can not found a root in the given interval"

程式:

// C Program to find root of an equations using secant method 
#include <stdio.h>

// function takes value of x and returns f(x) 
float f(float x) 
{ 
    // we are taking equation as x^3+x-1 
    float f = pow(x, 3) + x - 1; 
    return f; 
} 
  
void secant(float x1, float x2, float E) 
{ 
    float n = 0, xm, x0, c; 
    if (f(x1) * f(x2) < 0)
    { 
        do { 
            // calculate the intermediate value 
            x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); 
  
            // check if x0 is root of equation or not 
            c = f(x1) * f(x0); 
  
            // update the value of interval 
            x1 = x2; 
            x2 = x0; 
  
            // update number of iteration 
            n++; 
  
            // if x0 is the root of equation then break the loop 
            if (c == 0) 
                break; 
            xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
            printf("xm=%0.5lf\n", xm); 
        
            
        } while (fabs(xm - x0) >= E); // repeat the loop 
                                // until the convergence 
  
        printf("Root of the given equation=%0.5lf", x0); 
        printf("\nNo. of iterations = %0.0lf", n); 
    
        
    } else
        
        printf("Can not find a root in the given inteval"); 
} 
  
// Driver code 
int main() 
{ 
    // initializing the values 
    float x1 = 0, x2 = 1, E = 0.0001; 
    secant(x1, x2, E); 
    return 0; 
} 

輸出結果

xm=0.69005                                                                                                                                     
xm=0.68202                                                                                                                                     
xm=0.68233                                                                                                                                     
xm=0.68233                                                                                                                                     
Root of the given equation=0.68233                                                                                                             
No. of iterations = 5   

C語言 例題2-7 已知方程式 e^x + x^-2 + 2 cosx -6 利用正割法 找出f(x)=0的根

C語言 例題2-6 已知方程式 e^x + x^-2 + 2 cosx -6 利用正割法 找出f(x)=0的根

C語言 例題2-7 已知方程式 e^x + x^-2 + 2 cosx -6 利用正割法 找出f(x)=0的根


/* ex2-7.c Secant Method is similar to Newton-Raphson
 *  Method used for find solutions to f(x)=0 given
 *  initial approximations x0 and x1.
 */
#include <stdio.h>
#include <math.h>
#define  MAX  50
#define  PI  3.14159
#define  TOL  0.00001
#define  f(x)   (exp(x)+1/pow(2,x)+2*cos(x)-6)
void main()
{
   int i=2;
   double x0,x1,x,q0,q1;
   x0=1.8;
   x1=2.0;
   q0=f(x0);
   q1=f(x1);
   printf("i       xi           f(x)\n");
   printf("%-2d   %10.6lf   %10.6lf\n",0,x0,q0);
   printf("%-2d   %10.6lf   %10.6lf\n",1,x1,q1);
   while(i<=MAX)
   {
      x=x1-q1*(x1-x0)/(q1-q0);
      printf("%-2d   %10.6lf   %10.6lf\n",i,x,f(x));
      if(fabs(x-x1) < TOL)
      {
    printf("The Root=%10.6lf f(%10.6lf)=%10.6lf\n",x,x,f(x));
    exit(0);
      }
      else
      {
    i++;
    x0=x1;
    q0=q1;
    x1=x;
    q1=f(x);
      }
   }
   if(i>MAX)
     printf("Secant Method faileds!!!\n");
   return;
}


輸出畫面

i       xi           f(x)                                                                                                                      
0      1.800000    -0.117582                                                                                                                   
1      2.000000     0.806762                                                                                                                   
2      1.825441    -0.016116                                                                                                                   
3      1.828860    -0.002147                                                                                                                   
4      1.829385     0.000007                                                                                                                   
5      1.829384    -0.000000                                                                                                                   

The Root=  1.829384 f(  1.829384)= -0.000000  

2019年3月22日 星期五

Bisection Method – Algorithm, Flowchart and Code in C

Bisection Method – Algorithm, Flowchart and Code in C

源自於
Bisection Method Illustration
Bisection Method Illustration
The bisection methodis one of the simplest and most reliable of iterative methods for the solution of nonlinear equations. This method, also known as binary chopping or half-interval method, relies on the fact that if f(x) is real and continuous in the interval a < x < b, and f(a) and f(b) are of opposite signs, that is,
f(a)*f(b) < 0
then there is at least one real root in the interval between a and b.
Bisection method is a closed bracket method and requires two initial guesses. It is the simplest method with slow but steady rate of convergence.
Features of Bisection Method:
  • Type – closed bracket
  • No. of initial guesses – 2
  • Convergence – linear
  • Rate of convergence – slow but steady
  • Accuracy – good
  • Programming effort – easy
  • Approach – middle point

Bisection Method Algorithm and Flowchart:

Algorithm:
Start
  1. Decide initial values for x1 and x2 and stopping criterion, E.
  2. Compute f1 = f(x1) and f2 = f(x2).
  3. If f1 * f2>0, x1 and x2 do not bracket any root and go to step 7;
    Otherwise continue.
  4. Compute x0 = (x1+x2)/2 and compute f0 = f(x0)
  5. If f1*f0 < 0 then
    set x2 = x0
    else
    set x1 = x0
    set f1 = f0
  6. If absolute value of (x2 – x1)/x2 is less than error E, then
    root = (x1 + x2)/2
    write the value of root
    go to step 7
    else
    go to step 4
  7. Stop.
Flow Chart
Bisection Method 1- Flowchart
Bisection Method 1- Flowchart
Above given Algorithm and Flowchart of Bisection Methods Root computation is a simple and easier way of understanding how the bracketing system works, algorithm and flowchart may not follow same procedure, yet they give the same outputs.

Tabular Example of Bisection Method Numerical Computation

Consider f(x) = x3 + 3x – 5, where [x1 = 1, x2 = 2] and E = 0.001.
ix1x0x2f(x1)f(x0)f(x2)
111.52–1 2.875 9
211.251.5–1 0.703125 2.875
311.1251.25–1–0.201171875 0.703125
41.1251.18751.25–0.201171875 0.237060546875 0.703125
51.1251.156251.1875–0.201171875 0.014556884765625 0.237060546875
61.1251.1406251.15625–0.201171875–0.0941429138183594  0.014556884765625
71.1406251.14843751.15625–0.0941429138183594–0.0400032997131348 0.014556884765625
81.14843751.152343751.15625–0.0400032997131348–0.0127759575843811 0.014556884765625
91.152343751.1542968751.15625–0.0127759575843811 0.000877253711223602 0.014556884765625
Note: Bisection method guarantees the convergence of a function f(x) if it is continuous on the interval [a,b] (denoted by x1 and x2 in the above algorithm. For this, f(a) and f(b) should be of opposite nature i.e. opposite signs. The slow convergence in bisection method is due to the fact that the absolute error is halved at each step. Due to this the method undergoes linear convergence, which is comparatively slower than the Newton-Raphson, secant or false-position method.

Bisection Method – Code in C Programming

METHOD 1:
This program in C is used to demonstrate bisection method. Bisection method is one of the many root finding methods.
In this method we are given a function f(x) and we approximate 2 roots a and b for the function such that f(a).f(b)<0
Then we find another point
c=(a+b)/2
if f(c)==0
then root=c;
else
if f(a).f(c)<0
b=c;
if f(b).f(c)<0
a=c;
and we repeat these steps for the given number of iterations.
METHOD 2:
Source Code for Bisection Method
for equation f(x) = x^3 – 4*x – 9

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...