วิธีซีแคนต์ยังใช้เพื่อแก้สมการไม่เชิงเส้นอีกด้วย วิธีนี้คล้ายกับวิธีของนิวตัน-ราฟสัน แต่เราไม่จำเป็นต้องค้นหาความแตกต่างของฟังก์ชัน f(x) ใช้เฉพาะ f(x) เท่านั้น เราสามารถหา f'(x) เป็นตัวเลขได้โดยใช้สูตรผลต่างของการหารของนิวตัน จากสูตรนิวตัน-ราฟสัน
เรารู้ดีว่า

ทีนี้ โดยใช้สูตรผลหารหาร เราจะได้

เมื่อแทนที่ f’(x) ของสูตรนิวตัน-ราฟสันด้วย f’(x) ใหม่ เราจะสามารถหาสูตรซีแคนต์เพื่อแก้สมการไม่เชิงเส้นได้

หมายเหตุ: สำหรับวิธีนี้ เราต้องใช้การเดาเบื้องต้นสองครั้งเพื่อเริ่มหารากของสมการไม่เชิงเส้น
อินพุตและเอาต์พุต
Input: The function f(x) = (x*x) - (4*x) - 10 Output: The root is: -1.74166
อัลกอริทึม
secant(x1, x2)
ป้อนข้อมูล: การเดาเบื้องต้นสองครั้งสำหรับการรูท
ผลลัพธ์: ค่ารากโดยประมาณของสมการไม่เชิงเส้น f(x)
Begin f1 := f(x1) f2 := f(x2) x3 := ((f2*x1) – (f1*x2)) / (f2 – f1) while relative error of x3 and x2 are > precision, do x1 := x2 f1 := f2 x2 := x3 f2 := f(x2) x3 := ((f2*x1) – (f1*x2)) / (f2 – f1) done root := x3 return root End
ตัวอย่าง
#include<iostream>
#include<cmath>
using namespace std;
double absolute(double value) { //to find magnitude of value
if(value < 0)
return (-value);
return value;
}
double f(double x) { //the given function x^2-4x-10
return ((x*x)-(4*x)-10);
}
double secant(double x1, double x2) {
double x3, root;
double f1, f2;
f1 = f(x1);
f2 = f(x2);
x3 = (f2*x1-f1*x2)/(f2-f1);
while(absolute((x3-x2)/x3) > 0.00001) { //test accuracy of x3
x1 = x2; //shift x values
f1 = f2;
x2 = x3;
f2 = f(x2); //find new x2
x3 = (f2*x1-f1*x2)/(f2-f1); //calculate x3
}
root = x3;
return root; //root of the equation
}
main() {
double a, b, res;
a = 0.5;
b = 0.75;
res = secant(a, b);
cout << "The root is: " << res;
} ผลลัพธ์
The root is: -1.74166