Home  >  Article  >  php教程  >  C#二元一次方程参数求解

C#二元一次方程参数求解

WBOY
WBOYOriginal
2016-07-06 13:30:232424browse

本文记录了关于求直线斜率及纵截距值的简单方法,只是简单的记录下求解思路,最终还需根据具体项目进行优化。 设直线方程式为:y=kx+b 编程思想: 1、代入y1与x1的值,得到:y1=kx1+b 2、代入y2与x2的值,得到:y2=kx2+b 3、首先算出一个系数m=kx1 / kx2 或

本文记录了关于求直线斜率及纵截距值的简单方法,只是简单的记录下求解思路,最终还需根据具体项目进行优化。

设直线方程式为:y=kx+b

编程思想:

1、代入y1与x1的值,得到:y1=kx1+b

2、代入y2与x2的值,得到:y2=kx2+b       

3、首先算出一个系数m=kx1 / kx2 或 m=kx2 / kx1

4、根据第三步,将 y1=kx1+b 或 y2=kx2+b 乘以系数m,使 kx1==kx2 ,注意 kx1与kx2不能为0       

4、将2个函数相减,例如:my2-my1=mb-b 即 m(y2-y1)=(m-1)b      

5、算出纵截距b=(m(y2-y1))/(m-1) 注意:m不能为1 同时注意浮点数四舍五入问题       

6、将b值y1=kx1+b中,求出斜率k值

 

示例代码:

<span style="color: #0000ff;">using</span><span style="color: #000000;"> System;

</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> Demo
{   
    </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> PRogram
    {
        </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">int</span> _decimaldigits = <span style="color: #800080;">2</span>;<span style="color: #008000;">//</span><span style="color: #008000;">小数位数保留2位</span>

        <span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> 计算斜率k及纵截距b值
        </span><span style="color: #808080;">///</span> <span style="color: #808080;"></span>
        <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="x1"></span><span style="color: #008000;">坐标点x1</span><span style="color: #808080;"></span>
        <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="x2"></span><span style="color: #008000;">坐标点x2</span><span style="color: #808080;"></span>
        <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="y1"></span><span style="color: #008000;">坐标点y1</span><span style="color: #808080;"></span>
        <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="y2"></span><span style="color: #008000;">坐标点y2</span><span style="color: #808080;"></span>
        <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="kvalue"></span><span style="color: #008000;">斜率k值</span><span style="color: #808080;"></span>
        <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="bvalue"></span><span style="color: #008000;">纵截距b值</span><span style="color: #808080;"></span>
        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Calculate(<span style="color: #0000ff;">float</span> x1, <span style="color: #0000ff;">float</span> x2, <span style="color: #0000ff;">float</span> y1, <span style="color: #0000ff;">float</span> y2, <span style="color: #0000ff;">ref</span> <span style="color: #0000ff;">float</span> kvalue, <span style="color: #0000ff;">ref</span> <span style="color: #0000ff;">float</span> bvalue)<span style="color: #008000;">//</span><span style="color: #008000;">求方程y=kx+b 系数 k ,b</span>
<span style="color: #000000;">        {
            </span><span style="color: #0000ff;">float</span> coefficient = <span style="color: #800080;">1</span>;<span style="color: #008000;">//</span><span style="color: #008000;">系数值</span>
            <span style="color: #0000ff;">try</span><span style="color: #000000;">
            {
                </span><span style="color: #0000ff;">if</span> ((x1 == <span style="color: #800080;">0</span>) || (x2 == <span style="color: #800080;">0</span>) || (x1 == x2)) <span style="color: #0000ff;">return</span>; <span style="color: #008000;">//</span><span style="color: #008000;">排除为零的情况以及x1,x2相等时无法运算的情况
                </span><span style="color: #008000;">//</span><span style="color: #008000;">if (y1 == y2) return; </span><span style="color: #008000;">//</span><span style="color: #008000;">根据具体情况而定,如何这两个值相等,得到的就是一条直线</span>
                <span style="color: #0000ff;">float</span> temp = <span style="color: #800080;">0</span><span style="color: #000000;">;
                </span><span style="color: #0000ff;">if</span> (x1 >=<span style="color: #000000;"> x2)
                {
                    coefficient </span>= (<span style="color: #0000ff;">float</span>)Math.Round((x1 /<span style="color: #000000;"> x2), _decimaldigits);
                    temp </span>= y2 * coefficient; <span style="color: #008000;">//</span><span style="color: #008000;">将对应的函数乘以系数</span>
                    bvalue = (<span style="color: #0000ff;">float</span>)Math.Round(((temp - y1) / (coefficient - <span style="color: #800080;">1</span><span style="color: #000000;">)), _decimaldigits);
                    kvalue </span>= (<span style="color: #0000ff;">float</span>)Math.Round(((y1 - bvalue) / x1), _decimaldigits); <span style="color: #008000;">//</span><span style="color: #008000;">求出k值</span>
<span style="color: #000000;">                }
                </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
                {
                    coefficient </span>= x2 /<span style="color: #000000;"> x1;
                    temp </span>= y1 *<span style="color: #000000;"> coefficient;
                    bvalue </span>= (<span style="color: #0000ff;">float</span>)Math.Round(((temp - y2) / (coefficient - <span style="color: #800080;">1</span>)), _decimaldigits);<span style="color: #008000;">//</span><span style="color: #008000;">求出b值</span>
                    kvalue = (<span style="color: #0000ff;">float</span>)Math.Round(((y2 - bvalue) / x2), _decimaldigits); <span style="color: #008000;">//</span><span style="color: #008000;">求出k值</span>
<span style="color: #000000;">                }
            }
            </span><span style="color: #0000ff;">catch</span><span style="color: #000000;">
            {
                Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">x系数不能为0或相等</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            }
        }
        </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> Main(<span style="color: #0000ff;">string</span><span style="color: #000000;">[] args)
        {
            </span><span style="color: #0000ff;">float</span> x1 = <span style="color: #800080;">0</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">float</span> y1 = <span style="color: #800080;">0</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">float</span> x2 = <span style="color: #800080;">0</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">float</span> y2 = <span style="color: #800080;">0</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">float</span> kvalue = <span style="color: #800080;">0</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">float</span> bvalue = <span style="color: #800080;">0</span><span style="color: #000000;">;
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">计算直线斜率k及纵截距b</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">请输入x1值</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            x1 </span>=<span style="color: #000000;"> Convert.ToSingle(Console.ReadLine());
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">请输入y1值</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            y1 </span>=<span style="color: #000000;"> Convert.ToSingle(Console.ReadLine());
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">请输入x2值</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            x2 </span>=<span style="color: #000000;"> Convert.ToSingle(Console.ReadLine());
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">请输入y2值</span><span style="color: #800000;">"</span><span style="color: #000000;">);
            y2 </span>=<span style="color: #000000;"> Convert.ToSingle(Console.ReadLine());
            Calculate(x1, x2, y1, y2, </span><span style="color: #0000ff;">ref</span> kvalue, <span style="color: #0000ff;">ref</span><span style="color: #000000;"> bvalue);
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">直线方程为:y={0}x+{1}</span><span style="color: #800000;">"</span><span style="color: #000000;">, kvalue, bvalue);
            Console.ReadKey();  
        }
    }
}</span>

运行结果:

截图20160202102625275


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn