এই টিউটোরিয়ালে, আমরা সিক্যান্ট মেথড (Secant Method) ব্যবহার করে একটি সমীকরণের মূল (root) বের করার প্রোগ্রাম নিয়ে আলোচনা করব।
এখানে একটি সমীকরণ দেওয়া থাকবে। আমাদের কাজ হল পুনরাবৃত্তিমূলক (iterative) সিক্যান্ট মেথড ব্যবহার করে সেই সমীকরণের মূলগুলো বের করা।
উদাহরণ
#include <bits/stdc++.h>
using namespace std;
float f(float x) {
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 {
// মধ্যবর্তী মান হিসাব
x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n++;
if (c == 0) break;
xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
} while (fabs(xm - x0) >= E);
cout << "Root of the given equation = " << x0 << endl;
cout << "No. of iterations = " << n << endl;
} else
cout << "Can not find a root in the given interval";
}
int main() {
// মান ইনিশিয়ালাইজ
float x1 = 0, x2 = 1, E = 0.0001;
secant(x1, x2, E);
return 0;
}
আউটপুট
Root of the given equation = 0.682326 No. of iterations = 5