Login
Sign Up
Home
Program
Project
My Zone
Support
Show Code
Browse Program
Search Program
Cubic equation solver
Input Variables
Output Variables
Program Code
Description
Variable Name *
(dynamic type)
*
!
UP
Down
Delete
Default Value *
*
Description *
*
Multi Lines
Variable Name *
(dynamic type)
*
!
UP
Down
Delete
Default Value *
*
Description *
*
Multi Lines
Variable Name *
(dynamic type)
*
!
UP
Down
Delete
Default Value *
*
Description *
*
Multi Lines
Variable Name *
(dynamic type)
*
!
UP
Down
Delete
Default Value *
*
Description *
*
Multi Lines
Variable Name *
(dynamic type)
*
UP
Down
Delete
Description *
*
Variable Name *
(dynamic type)
*
UP
Down
Delete
Description *
*
Variable Name *
(dynamic type)
*
UP
Down
Delete
Description *
*
Input Variables List
Output Variables List
Source Code
double m = 27 * Math.Pow(a * d ,2) - 18 * a * b * c * d + 4 * a * Math.Pow(c,3) + 4 * Math.Pow(b,3) * d - Math.Pow(b * c,2); if (a == 0) { x1 = "a can not be zero"; x2 = "a can not be zero"; x3 = "a can not be zero"; } else { double z = 13.5 * a * a * d - 4.5 * a * b * c + Math.Pow(b,3); if (Math.Abs(m) < 1e-9) { if (Math.Abs(z) < 1e-12) x1 = x2 = x3 = -b / 3.0 / a; else { z = z < 0 ? -Math.Pow(-z,1.0/3):Math.Pow(z,1.0/3); x1 = (-b - 2 * z) / 3 / a; x2 = x3 = (-b + z) / 3 / a; } } else if (m < 0) { double th = Math.Acos(-z / (Math.Sqrt(-27 * Math.Pow(a * c,3) + 27 * Math.Pow(a * b * c,2) - 9 * a * Math.Pow(b,4) * c + Math.Pow(b,6)))) / 3; x1 = (-b + 2 * Math.Sqrt(b * b - 3 * a * c) * Math.Cos(th)) / 3 / a; x2 = (-b + 2 * Math.Sqrt(b * b - 3 * a * c) * Math.Cos(th + 2 * Math.PI / 3)) / 3 / a; x3 = (-b + 2 * Math.Sqrt(b * b - 3 * a * c) * Math.Cos(th + 4 * Math.PI / 3)) / 3 / a; } else { string i; double r1 = z + Math.Sqrt(z * z - Math.Pow(b * b - 3 * a * c,3)); double r2 = z - Math.Sqrt(z * z - Math.Pow(b * b - 3 * a * c,3)); r1 = r1 < 0 ? -Math.Pow(-r1,1.0/3):Math.Pow(r1,1.0/3); r2 = r2 < 0 ? -Math.Pow(-r2,1.0/3):Math.Pow(r2,1.0/3); x1 = (-b - r1 - r2) / 3 / a; double r = (-b + (r1 + r2) / 2) / 3 / a; if (Math.Abs(Math.Abs((r1 - r2) / Math.Sqrt(3) / 2 / a) - 1) < 1e-12) i = ""; else i = Convert.ToString(Math.Abs((r1 - r2) / Math.Sqrt(3) / 2 / a)); if (Math.Abs(r) < 1e-12) { x2 = i + "i"; x3 = "-" + i + "i"; } else { x2 = Convert.ToString(r) + " + " + i+ "i"; x3 = Convert.ToString(r) + " - " + i + "i"; } } }
Subject
Description
This program calculates the three roots of the cubic equation:
ax
3
+ bx
2
+ cx + d = 0
Category
Mathematics
Created By
zizo0003
Created Date
5/1/2011
Last Modify Date
5/1/2011
Calculation Count
20
Version
1.00
Positive
0
Neutral
0
Negative
0
Related Program ID
147 , 146
Copyright ©2009-2010 CUI WEI. All Rights Reserved.