C#二元一次方程參數(shù)求解
Jul 06, 2016 pm 01:30 PM本文記錄了關(guān)于求直線斜率及縱截距值的簡單方法,只是簡單的記錄下求解思路,最終還需根據(jù)具體項(xiàng)目進(jìn)行優(yōu)化。 設(shè)直線方程式為:y=kx+b 編程思想: 1、代入y1與x1的值,得到:y1=kx1+b 2、代入y2與x2的值,得到:y2=kx2+b 3、首先算出一個(gè)系數(shù)m=kx1 / kx2 或
本文記錄了關(guān)于求直線斜率及縱截距值的簡單方法,只是簡單的記錄下求解思路,最終還需根據(jù)具體項(xiàng)目進(jìn)行優(yōu)化。
設(shè)直線方程式為:y=kx+b
編程思想:
1、代入y1與x1的值,得到:y1=kx1+b
2、代入y2與x2的值,得到:y2=kx2+b???????
3、首先算出一個(gè)系數(shù)m=kx1 / kx2 或 m=kx2 / kx1
4、根據(jù)第三步,將 y1=kx1+b 或 y2=kx2+b 乘以系數(shù)m,使 kx1==kx2 ,注意 kx1與kx2不能為0???????
4、將2個(gè)函數(shù)相減,例如:my2-my1=mb-b 即 m(y2-y1)=(m-1)b??????
5、算出縱截距b=(m(y2-y1))/(m-1) 注意:m不能為1 同時(shí)注意浮點(diǎn)數(shù)四舍五入問題???????
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;">小數(shù)位數(shù)保留2位</span> <span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 計(jì)算斜率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;">坐標(biāo)點(diǎn)x1</span><span style="color: #808080;"></span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="x2"></span><span style="color: #008000;">坐標(biāo)點(diǎn)x2</span><span style="color: #808080;"></span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="y1"></span><span style="color: #008000;">坐標(biāo)點(diǎn)y1</span><span style="color: #808080;"></span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="y2"></span><span style="color: #008000;">坐標(biāo)點(diǎn)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 系數(shù) 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;">系數(shù)值</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相等時(shí)無法運(yùn)算的情況 </span><span style="color: #008000;">//</span><span style="color: #008000;">if (y1 == y2) return; </span><span style="color: #008000;">//</span><span style="color: #008000;">根據(jù)具體情況而定,如何這兩個(gè)值相等,得到的就是一條直線</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;">將對(duì)應(yīng)的函數(shù)乘以系數(shù)</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系數(shù)不能為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;">計(jì)算直線斜率k及縱截距b</span><span style="color: #800000;">"</span><span style="color: #000000;">); Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">請(qǐng)輸入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;">請(qǐng)輸入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;">請(qǐng)輸入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;">請(qǐng)輸入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>
運(yùn)行結(jié)果:

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Pinduoduo software provides a lot of good products, you can buy them anytime and anywhere, and the quality of each product is strictly controlled, every product is genuine, and there are many preferential shopping discounts, allowing everyone to shop online Simply can not stop. Enter your mobile phone number to log in online, add multiple delivery addresses and contact information online, and check the latest logistics trends at any time. Product sections of different categories are open, search and swipe up and down to purchase and place orders, and experience convenience without leaving home. With the online shopping service, you can also view all purchase records, including the goods you have purchased, and receive dozens of shopping red envelopes and coupons for free. Now the editor has provided Pinduoduo users with a detailed online way to view purchased product records. method. 1. Open your phone and click on the Pinduoduo icon.

Call recording in iPhone is often underestimated and is one of the most critical features of iPhone. With its simplicity, this feature is of vital importance and can provide important insights about the calls made or received on the device. Whether for work purposes or legal proceedings, the ability to access call records can prove invaluable. In simple terms, call history refers to the entries created on your iPhone whenever you make or receive a call. These logs contain key information, including the contact's name (or number if not saved as a contact), timestamp, duration, and call status (dialed, missed, or not answered). They are a concise record of your communication history. Call history includes call history strips stored on your iPhone

How to View Command History in Linux In Linux, we use the history command to view the list of all previously executed commands. It has a very simple syntax: history Some options for pairing with the history command include: Option description -c clears the command history for the current session -w writes the command history to a file -r reloads the command history from the history file -n Limit the number of output of recent commands Simply run the history command to see a list of all previously executed commands in a Linux terminal: In addition to viewing command history, you can also manage command history and perform modifications to previously executed commands , reverse search command history or even delete history completely

Product parameters refer to the meaning of product attributes. For example, clothing parameters include brand, material, model, size, style, fabric, applicable group, color, etc.; food parameters include brand, weight, material, health license number, applicable group, color, etc.; home appliance parameters include brand, size, color , place of origin, applicable voltage, signal, interface and power, etc.

New feature of PHP5.4 version: How to use callable type hint parameters to accept callable functions or methods Introduction: PHP5.4 version introduces a very convenient new feature - you can use callable type hint parameters to accept callable functions or methods . This new feature allows functions and methods to directly specify the corresponding callable parameters without additional checks and conversions. In this article, we will introduce the use of callable type hints and provide some code examples,

i9-12900H is a 14-core processor. The architecture and technology used are all new, and the threads are also very high. The overall work is excellent, and some parameters have been improved. It is particularly comprehensive and can bring users Excellent experience. i9-12900H parameter evaluation review: 1. i9-12900H is a 14-core processor, which adopts the q1 architecture and 24576kb process technology, and has been upgraded to 20 threads. 2. The maximum CPU frequency is 1.80! 5.00ghz, which mainly depends on the workload. 3. Compared with the price, it is very suitable. The price-performance ratio is very good, and it is very suitable for some partners who need normal use. i9-12900H parameter evaluation and performance running scores

The latest official news of vivox200ultra has exposed the parameters and price details of vivox200ultra. It is reported that vivox200ultra will be equipped with a 10x periscope super telephoto lens, and the price starts at about 6999 yuan. It can be seen that it occupies an absolute advantage in photography performance. The following are the parameters and prices of vivox200ultra Come and see the details. 1. Parameter configuration details of vivox200ultra 1. Vivox200ultra rendering From the vivo X200 Ultra rendering, the front of the phone adopts a borderless full-screen design, and the visual effect of the entire front of the phone can be said to be very invincible. 2. vivox200ultra has Blackhawk frame

How to write an algorithm to find the least common multiple in Python? The least common multiple is the smallest integer between two numbers that can divide the two numbers. In mathematics, solving the least common multiple is a basic mathematical task, and in computer programming, we can use Python to write an algorithm for solving the least common multiple. The following will introduce the basic least common multiple algorithm and give specific code examples. The mathematical definition of the least common multiple is: If a is divisible by n and b is divisible by n, then n is the least common multiple of a and b. To solve the minimum
