> php教程 > php手册 > C#二元一次方程参数求解

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

WBOY
풀어 주다: 2016-07-06 13:30:23
원래의
2443명이 탐색했습니다.

本文记录了关于求直线斜率及纵截距值的简单方法,只是简单的记录下求解思路,最终还需根据具体项目进行优化。 设直线方程式为: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


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿