PHP realizes the solution of binary linear equations

藏色散人
Release: 2023-04-05 18:54:02
Original
4241 people have browsed it

An integral equation that contains two unknowns and the degree of the terms containing the unknowns is 1 is called a linear equation of two variables. All linear equations of two variables can be transformed into the general formula of ax by c=0 (a, b≠0) and the standard formula of ax by=c (a, b≠0), otherwise they are not linear equations of two variables.

Below we will introduce to you how to use PHP to solve linear equations of two variables with specific examples.

The linear equation of two variables is as follows:

ax + by = c 
dx + ey = f
Copy after login

Here we need to first give the values ​​​​of a, b, c, d, e and f, and then print out the values ​​​​of x, y .

Input: a b c d e f separated by a space. (- 1000≤a,b,c,d,e,f≤1000)

PHP code is as follows:

<?php
  function to_f($e) {
    return (float)$e;
  } 
  while($line = fgets(STDIN)) {
    $a = explode(" ", $line);
    $a = array_map("to_f", $a);
    $x = ($a[2]*$a[4]-$a[1]*$a[5])/($a[0]*$a[4]-$a[3]*$a[1]);
    $y = ($a[2]*$a[3]-$a[0]*$a[5])/($a[1]*$a[3]-$a[0]*$a[4]);
    print("x和y的值分别是:\n");
    printf("%.3f %.3f\n", $x, $y);
  }
Copy after login

Output:

x和y的值分别是:
-1.684 2.737
Copy after login

Related recommendations: "PHP Tutorial"

This article is an introduction to the method of solving binary linear equations in PHP. I hope it will be helpful to friends in need!

The above is the detailed content of PHP realizes the solution of binary linear equations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!