如何在PHP中求解方程式

编程入门 行业动态 更新时间:2024-10-26 23:38:15
本文介绍了如何在PHP中求解方程式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个方程式在解决它时遇到了麻烦.我的方程式:

I have an equation getting trouble while solving it. My equation:

S1=O*P*M1/Y1 D1=N-S1 S2=(O-D1)*P*M2/Y2 D2=N-S2 S3=(O-D1-D2)*P*M3/Y3 D3=N-S3 ...

我所做的:

$S = array(); $M = array(30,31,30); $Y = array(360,360,360); $O = 30000; $P = 0.3; $N = 10509.74; $D = array(); for($i=1; $i<=count($M); $i++){ if($i==1){ $S[1] = $O*$P*$M[1]/$Y[1]; $D[1] = $N - $S[1]; } else{ for($k=2; $k<=count($M); $k++){ $S[$i] = ($O-$D[$k-1])*$P*$M[$k]/$Y[$k]; $D[$i] = $N - $S[$i]; } } } print_r($S);

不知道我在哪里做错了,或者我做错了方向.

No idea where i did wrong or I'm doing on wrong way.

推荐答案

我很确定这是正确的.如果不是,请更新您的问题,以包括示例输入的预期输出. (无论如何,这样做可能不是一个坏主意.)

I'm pretty sure this is right. If not, please update your question to include the expected output for your sample input. (Doing this anyway probably isn't a bad idea.)

// Set up things that don't change as constants. define('P', 0.3); define('N', 10509.74); // O doesn't change, but it's only used once on // its own. We'll reuse it to store the ongoing // value of (O-D1), (O-D1-D2), etc. $O = 30000; $M = array(30, 31, 30); $Y = array(360, 360, 360); $S = array(); $D = array(); for ($i = 0; $i < count($M); $i++) { $S[$i] = $O * P * $M[$i] / $Y[$i]; $D[$i] = N - $S[$i]; $O -= $D[$i]; } print_r($S);

输出:

Array ( [0] => 750 [1] => 522.87338333333 [2] => 256.33483458333 )

如果在此末尾不需要完整的$D数组,则可以将其完全剪切掉,而只需使用$O -= N - $S[$i];即可.这个问题目前还不清楚,所以我把它留了下来.

If you don't need the complete $D array at the end of this you can cut it out entirely and just use $O -= N - $S[$i];. It's not exactly clear from the question so I left it in.

精度可能是一个问题,尽管所需的精度程度被忽略了,所以我根本不用理会它.

Precision may be an issue, though the degree of precision needed was left out so I didn't bother tackling it at all.

更多推荐

如何在PHP中求解方程式

本文发布于:2023-11-29 18:35:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方程式   如何在   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!