C # solving the parameters of the binary one-time equation,

Source: Internet
Author: User

C # solving the parameters of the binary one-time equation,

This article records a simple method for finding the slope of a straight line and the longitudinal intercept value, but simply records the solution ideas, and finally needs to be optimized according to the specific project.

Set the linear equation to y = kx + B.

Programming ideology:

1. input values of y1 and x1 to get y1 = kx1 + B.

2. input the values of y2 and x2 to get: y2 = kx2 + B.

3. First, calculate a coefficient m = kx1/kx2 or m = kx2/kx1

4. in step 3, multiply y1 = kx1 + B or y2 = kx2 + B by the coefficient m to make kx1 = kx2. Note that kx1 and kx2 cannot be 0.

4. Subtract two functions, for example, my2-my1 = mb-B, that is, m (y2-y1) = (S-1) B

5. Calculate the longitudinal intercept B = (m (y2-y1)/(m-1) Note: m cannot be 1 at the same time pay attention to floating point Rounding Problem

6. In B value y1 = kx1 + B, obtain the slope k value.

 

Sample Code:

Using System; namespace Demo {class Program {private static readonly int _ decimaldigits = 2; // retain 2 decimal places /// <summary> // calculates the slope k and the vertical intercept B value /// </summary> /// <param name = "x1 "> coordinate point x1 </param> // <param name =" x2 "> coordinate point x2 </param> // <param name =" y1 "> coordinate point y1 </param> /// <param name = "y2"> coordinate point y2 </param> /// <param name = "kvalue"> slope k value </param> /// <param name = "bvalue"> vertical intercept B value </param> private static void Calculate (float x1, float x2, float y1, float y2, ref float kvalue, ref float bvalue) // obtain the equation y = kx + B coefficient k, B {float coefficient = 1; // system value try {if (x1 = 0) | (x2 = 0) | (x1 = x2) return; // eliminate the case of zero and the case where calculation cannot be performed when x1 and x2 are equal // if (y1 = y2) return; // depending on the actual situation, the two values are equal. The result is a straight line float temp = 0; if (x1> = x2) {coefficient = (float) Math. round (x1/x2), _ decimaldigits); temp = y2 * coefficient; // multiply the corresponding function by the coefficient bvalue = (float) Math. round (temp-y1)/(coefficient-1), _ decimaldigits); kvalue = (float) Math. round (y1-bvalue)/x1), _ decimaldigits); // obtain the K value} else {coefficient = x2/x1; temp = y1 * coefficient; bvalue = (float) math. round (temp-y2)/(coefficient-1), _ decimaldigits); // obtain the value of B kvalue = (float) Math. round (y2-bvalue)/x2), _ decimaldigits); // obtain the K value} catch {Console. writeLine ("x coefficient cannot be 0 or equal");} static void Main (string [] args) {float x1 = 0; float y1 = 0; float x2 = 0; float y2 = 0; float kvalue = 0; float bvalue = 0; Console. writeLine ("calculating the slope of a straight line k and longitudinal intercept B"); Console. writeLine ("Enter the x1 value"); x1 = Convert. toSingle (Console. readLine (); Console. writeLine ("Enter y1 value"); y1 = Convert. toSingle (Console. readLine (); Console. writeLine ("Enter the x2 value"); x2 = Convert. toSingle (Console. readLine (); Console. writeLine ("Enter the y2 value"); y2 = Convert. toSingle (Console. readLine (); Calculate (x1, x2, y1, y2, ref kvalue, ref bvalue); Console. writeLine ("linear equation: y = {0} x + {1}", kvalue, bvalue); Console. readKey ();}}}

Running result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.