Advertisement
Guest User

1028

a guest
Oct 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. // ConsoleApplication9.cpp : 定義主控台應用程式的進入點。
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <math.h>
  7. using namespace std;
  8. class axn {
  9. public:
  10.     axn() {
  11.         //cout << "inside\t" << "建立了一個項" << endl;
  12.     }
  13.     void set(double ina, double inn) {
  14.         a = ina;
  15.         n = inn;
  16.         //cout << "inside\t" << "設定了一個項為" << a << "x^" << n << endl;
  17.     }
  18.     double df(double x) {
  19.         //cout << "inside\t" << "一次微分了" << a << "x^" << n << endl;
  20.         return pow(x, n - 1)*n*a;
  21.     }
  22.     double f(double x) {
  23.         //cout << "inside\t" << "一次微分了" << a << "x^" << n << endl;
  24.         return pow(x, n )*a;
  25.     }
  26.     double getdfa() {
  27.         return a*n;
  28.     }
  29.     double getdfn() {
  30.         return n-1;
  31.     }
  32.     double ddf(double x) {
  33.         return pow(x, n - 2)*n*(n - 1)*a;
  34.     }
  35.     double geta() {
  36.         return a;
  37.     }
  38.     double getn() {
  39.         return n;
  40.     }
  41. private:
  42.     double n;
  43.     double a;
  44. };
  45. class sumaxn {
  46. public:
  47.     int i;
  48.     axn *arr;
  49.     sumaxn(double ai[], double ni[]) {
  50.         int k = 0;
  51.         while (ni[k] != -50) {
  52.             k++;
  53.         }
  54.         i = k;
  55.         //cout << "inside\t\t" << "建立了一個" << i << "項的項之和" << endl;
  56.         arr = new axn[i];
  57.         for (int j = 0; j<i; j++) {
  58.             arr[j].set(ai[j], ni[j]);
  59.         }
  60.     }
  61.     sumaxn getdf() {
  62.         double *dadate = new double[i];
  63.         double *dndate = new double[i];
  64.         for (int j = 0; j < i; j++) {
  65.             dadate[j] = arr[j].getdfa();
  66.             dndate[j] = arr[j].getdfn();
  67.         }
  68.         //cout << "inside\t\t" << "微分多項式" << endl;
  69.         dadate[i] = -50;
  70.         dndate[i] = -50;
  71.         sumaxn df(dadate, dndate);
  72.         //cout << "inside\t\t" << "微分多項式";
  73.         //df.pp();
  74.         return df;
  75.     }
  76.     double getdf(double x) {
  77.         double tot=0;
  78.         for (int j = 0; j < i; j++) {
  79.             tot += arr[j].df(x);
  80.         }
  81.         return tot;
  82.     }
  83.     double getf(double x) {
  84.         double tot = 0;
  85.         for (int j = 0; j < i; j++) {
  86.             tot += arr[j].f(x);
  87.         }
  88.         return tot;
  89.     }
  90.     void pp() {
  91.         for (int j = 0; j<i; j++) {
  92.             cout <<arr[j].geta() << "x^" << arr[j].getn() <<(j==(i-1)?"":"+");
  93.         }
  94.         cout << endl;
  95.     }
  96. };
  97. int main(int argc, char** argv) {
  98.     double adate[] = { 2,1,3,4,4,4,-50};
  99.     double ndate[] = { 5,8,3,2,1,0,-50};
  100.     sumaxn f(adate, ndate);
  101.     cout << "原方程式為:";
  102.     f.pp();
  103.     cout << "一次微分後的方程式為:";
  104.     f.getdf().pp();
  105.     double a = 10;
  106.     for (int i = 0; i < 100; i++) {
  107.         a = a - f.getf(a) / f.getdf().getf(a);
  108.     }
  109.     cout << "牛頓法求y=0的解x為" << setprecision(20) << a;
  110.     system("pause");
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement