JS 一元四次方程计算器

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<body>

<div align="center">例如输入:a=3, b=6, c=-123, d=-126 e=1080</div>
<form name="form1" action="">
	ax<sup>4</sup> + bx<sup>3</sup> + cx<sup>2</sup> + dx + e = 0
	<table border="0" align="center" colspan="4" cellspacing="0" cellpadding="0">
		<tbody>
			<tr>
				<td><input type="text" name="aIn" size="5" placeholder="a">x<sup>4</sup> +</td>
				<td><input type="text" name="bIn" size="5" placeholder="b">x³ +</td>
				<td><input type="text" name="cIn" size="5" placeholder="c">x² +</td>
				<td><input type="text" name="dIn" size="5" placeholder="d">x +</td>
				<td><input type="text" name="eIn" size="5" placeholder="e">e</td>
			</tr>
		</tbody>
	</table>
	
	<center>
		<input type="button" name="button1" value="计算" onclick="quad4solve(this.form)">
	</center>
	
	<table border="0" class="innerc" cellspacing="0" cellpadding="3">
		<tbody>
		<tr align="center">
			<td class="inner" colspan="4"><b>结果:</b></td>
		</tr>
		<tr>
			<td>x<sub>1</sub>: 
				<input type="text" name="x1Re" size="25"> + &nbsp;
				<input type="text" name="x1Im" size="25"> i 
			</td>
		</tr>
		<tr>
			<td>x<sub>2</sub>: 
				<input type="text" name="x2Re" size="25"> + &nbsp;
				<input type="text" name="x2Im" size="25"> i 
			</td>
		</tr>
		<tr>
			<td>x<sub>3</sub>: 
				<input type="text" name="x3Re" size="25"> + &nbsp;
				<input type="text" name="x3Im" size="25"> i 
			</td>
		</tr>
		<tr>
			<td>x<sub>4</sub>: 
				<input type="text" name="x4Re" size="25"> + &nbsp;
				<input type="text" name="x4Im" size="25"> i 
			</td>
		</tr>
		</tbody>
	</table>
</form>

<script type="text/javascript">
function quad4solve(dataForm)
{
	var a = parseFloat(dataForm.aIn.value);
	var b = parseFloat(dataForm.bIn.value);
	var c = parseFloat(dataForm.cIn.value);
	var d = parseFloat(dataForm.dIn.value);
	var e = parseFloat(dataForm.eIn.value);
	if (a == 0)
	{
		alert("The coefficient of the power four of x is 0. Please use the utility for a third degree quadratic.");
		return;
	} 
	if (e == 0)
	{
		alert("One root is 0. Now divide through by x and use the utility for a third degree quadratic to solve the resulting equation for the other three roots.");
		return;
	} 
	if (a != 1) 
	{
		b /= a;
		c /= a;
		d /= a;
		e /= a;
	}

	//立方解算器系数
	var cb, cc, cd;  
	var discrim, q, r, RRe, RIm, DRe, DIm, dum1, ERe, EIm, s, t, term1, r13, sqR, y1, z1Re, z1Im, z2Re;
	cb = -c;
	cc = -4.0*e + d*b;
	cd = -(b*b*e + d*d) + 4.0*c*e;
	if (cd == 0)  
	{
		alert("cd = 0.");
	}
	q = (3.0*cc - (cb*cb))/9.0;
	r = -(27.0*cd) + cb*(9.0*cc - 2.0*(cb*cb));
	r /= 54.0;
	discrim = q*q*q + r*r;
	term1 = (cb/3.0);
	if (discrim > 0) 
	{ 
		// 1 实数, 2 复数
		s = r + Math.sqrt(discrim);
		s = ((s < 0) ? -Math.pow(-s, (1.0/3.0)) : Math.pow(s, (1.0/3.0)));
		t = r - Math.sqrt(discrim);
		t = ((t < 0) ? -Math.pow(-t, (1.0/3.0)) : Math.pow(t, (1.0/3.0)));
		y1 = -term1 + s + t;
		} 
		else 
		{
			if (discrim == 0) 
			{
				r13 = ((r < 0) ? -Math.pow(-r,(1.0/3.0)) : Math.pow(r,(1.0/3.0)));
				y1 = -term1 + 2.0*r13;
			} 
			else 
			{                             	
				q = -q;
				dum1 = q*q*q;
				dum1 = Math.acos(r/Math.sqrt(dum1));
				r13 = 2.0*Math.sqrt(q);
				y1 = -term1 + r13*Math.cos(dum1/3.0);
			}
		} 
		// 确定了y1 分解立方的实根
		term1 = b/4.0;
		sqR = -c + term1*b + y1;  
		RRe = RIm = DRe = DIm = ERe = EIm = z1Re = z1Im = z2Re = 0;
		if (sqR >= 0) 
		{
			if (sqR == 0) 
			{
				dum1 = -(4.0*e) + y1*y1;
				if (dum1 < 0){
					z1Im = 2.0*Math.sqrt(-dum1);
				} else {
	  				z1Re = 2.0*Math.sqrt(dum1);
					z2Re = -z1Re;
				}
			} 
			else 
			{                       
			RRe = Math.sqrt(sqR);
			z1Re = -(8.0*d + b*b*b)/4.0 + b*c;
			z1Re /= RRe;
			z2Re = -z1Re;
		 } 
	} 
	else 
	{                           
		RIm = Math.sqrt(-sqR);
		z1Im = -(8.0*d + b*b*b)/4.0 + b*c;
		z1Im /= RIm;
		z1Im = -z1Im;
	} 
	z1Re += -(2.0*c + sqR) + 3.0*b*term1;
	z2Re += -(2.0*c + sqR) + 3.0*b*term1;

	//At this point, z1 and z2 should be the terms under the square root for D and E
	if (z1Im == 0)
	{               // Both z1 and z2 real
		if (z1Re >= 0)
		{
			DRe = Math.sqrt(z1Re);
		}
		else
		{
			DIm = Math.sqrt(-z1Re);
		}
		if (z2Re >= 0)
		{
			ERe = Math.sqrt(z2Re);
		}
		else
		{
			EIm = Math.sqrt(-z2Re);
		}
	}
	else 
	{                      
		r = Math.sqrt(z1Re*z1Re + z1Im*z1Im); 
		r = Math.sqrt(r);
		dum1 = Math.atan2(z1Im, z1Re); 
		dum1 /= 2; //Divide this angle by 2
		ERe = DRe = r*Math.cos(dum1); 
		DIm = r*Math.sin(dum1);
		EIm = -DIm;
	} 
	dataForm.x1Re.value = -term1 + (RRe + DRe)/2;
	dataForm.x1Im.value = (RIm + DIm)/2;
	dataForm.x2Re.value = -(term1 + DRe/2) + RRe/2;
	dataForm.x2Im.value = (-DIm + RIm)/2;
	dataForm.x3Re.value = -(term1 + RRe/2) + ERe/2;
	dataForm.x3Im.value = (-RIm + EIm)/2;
	dataForm.x4Re.value = -(term1 + (RRe + ERe)/2);
	dataForm.x4Im.value = -(RIm + EIm)/2;
	return;
}
</script>
</body>
</html>

1,886 thoughts on “JS 一元四次方程计算器”

  1. Hi there! Do you use Twitter? I’d like to follow you if
    that would be ok. I’m undoubtedly enjoying your blog and look forward to new posts.

  2. 《539彩券:台灣的小確幸》

    哎呀,說到台灣的彩券遊戲,你怎麼可能不知道539彩券呢?每次”539開獎”,都有那麼多人緊張地盯著螢幕,心想:「這次會不會輪到我?」。

    ### 539彩券,那是什麼來頭?

    嘿,539彩券可不是昨天才有的新鮮事,它在台灣已經陪伴了我們好多年了。簡單的玩法,小小的投注,卻有著不小的期待,難怪它這麼受歡迎。

    ### 539開獎,是場視覺盛宴!

    每次”539開獎”,都像是一場小型的節目。專業的主持人、明亮的燈光,還有那台專業的抽獎機器,每次都帶給我們不小的刺激。

    ### 跟我一起玩539?

    想玩539?超簡單!走到街上,找個彩券行,選五個你喜歡的號碼,買下來就對了。當然,現在科技這麼發達,坐在家裡也能買,多方便!

    ### 539開獎,那刺激的感覺!

    每次”539開獎”,真的是讓人既期待又緊張。想像一下,如果這次中了,是不是可以去吃那家一直想去但又覺得太貴的餐廳?

    ### 最後說兩句

    539彩券,真的是個小確幸。但嘿,玩彩券也要有度,別太沉迷哦!希望每次”539開獎”,都能帶給你一點點的驚喜和快樂。

  3. Seriously a good deal of great knowledge!
    [url=https://essaypromaster.com/]essay writer website[/url] paper writers [url=https://paperwritingservicecheap.com/]how to write a scientific paper[/url] how to write a reflection paper

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Back To Top

鄂ICP备17008157号-1