fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. // ফাংশন এবং তার ডেরিভেটিভের মান নির্ণয়ের জন্য
  9. double evaluatePolynomial(const vector<double>& coeffs, double x) {
  10. double result = 0;
  11. int degree = coeffs.size() - 1;
  12. for (int i = 0; i <= degree; ++i) {
  13. result += coeffs[i] * pow(x, degree - i);
  14. }
  15. return result;
  16. }
  17.  
  18. double evaluateDerivative(const vector<double>& coeffs, double x) {
  19. double result = 0;
  20. int degree = coeffs.size() - 1;
  21. for (int i = 0; i < degree; ++i) {
  22. result += coeffs[i] * (degree - i) * pow(x, degree - i - 1);
  23. }
  24. return result;
  25. }
  26.  
  27. // সাইনটিক ডিভিশন পদ্ধতি
  28. vector<double> syntheticDivision(const vector<double>& coeffs, double root) {
  29. int n = coeffs.size();
  30. vector<double> newCoeffs(n - 1);
  31. newCoeffs[0] = coeffs[0];
  32. for (int i = 1; i < n - 1; ++i) {
  33. newCoeffs[i] = coeffs[i] + newCoeffs[i - 1] * root;
  34. }
  35. return newCoeffs;
  36. }
  37.  
  38. int main() {
  39. int degree;
  40. cout << "পলিনোমিয়ালের ডিগ্রি প্রদান করুন: ";
  41. cin >> degree;
  42.  
  43. vector<double> coeffs(degree + 1);
  44. cout << "পলিনোমিয়ালের সহগ প্রদান করুন (উচ্চতর থেকে নিম্নতর):\n";
  45. for (int i = 0; i <= degree; ++i) {
  46. cin >> coeffs[i];
  47. }
  48.  
  49. double x0, E;
  50. cout << "প্রাথমিক অনুমান (x0) প্রদান করুন: ";
  51. cin >> x0;
  52. cout << "ত্রুটি সীমা (E) প্রদান করুন: ";
  53. cin >> E;
  54.  
  55. vector<double> currentCoeffs = coeffs;
  56. for (int n = degree; n > 1; --n) {
  57. double f_x0 = evaluatePolynomial(currentCoeffs, x0);
  58. double f_prime_x0 = evaluateDerivative(currentCoeffs, x0);
  59. double x1 = x0 - f_x0 / f_prime_x0;
  60.  
  61. cout << fixed << setprecision(6);
  62. cout << "পুনরাবৃত্তি " << degree - n + 1 << ":\n";
  63. cout << "x0 = " << x0 << ", x1 = " << x1 << endl;
  64.  
  65. if (fabs(x1 - x0) < E) {
  66. cout << "মূল: " << x1 << endl;
  67. break;
  68. }
  69.  
  70. currentCoeffs = syntheticDivision(currentCoeffs, x1);
  71. x0 = x1;
  72. }
  73.  
  74. if (currentCoeffs.size() == 2) {
  75. double root = -currentCoeffs[0] / currentCoeffs[1];
  76. cout << "শেষ মূল: " << root << endl;
  77. }
  78.  
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0s 5316KB
stdin
Enter the degree of the equation: 2
Enter the coefficients of the equation: -10 -4 1
stdout
পলিনোমিয়ালের ডিগ্রি প্রদান করুন: পলিনোমিয়ালের সহগ প্রদান করুন (উচ্চতর থেকে নিম্নতর):
প্রাথমিক অনুমান (x0) প্রদান করুন: ত্রুটি সীমা (E) প্রদান করুন: