简体   繁体   English

线性方程组的求解系统R-验证

[英]Solving System of Linear Equations R - Verification

So I managed to solve this system of linear equations in R: 因此,我设法解决了R中的线性方程组:

x–y+z=1, x+y–z=1 and x+y+z=3 x–y + z = 1,x + y–z = 1和x + y + z = 3

My code below is: 我的代码如下:

A <- matrix(data=c(1, -1, 1, 1, 1, -1, 1, 1, 1), nrow=3, ncol=3, byrow=TRUE) 

b <- matrix(data=c(1, 1, 3), nrow=3, ncol=1, byrow=FALSE)

round(solve(A, b), 3)

However, I need to demonstrate my solution is correct by substituting values for x, y and z. 但是,我需要通过用x,y和z替换值来证明我的解决方案是正确的。 Apparently this is easily done with one line of code using matrix operations. 显然,使用矩阵运算只需一行代码即可轻松完成。 Can anyone help me out? 谁能帮我吗? Thanks in advance. 提前致谢。

You gave the equations with a minus sign that was actually a dash, but after correcting that error we can transmute those equations and assess the solution. 您给方程式加了一个负号,实际上是一个破折号,但是在纠正了这个误差之后,我们可以对这些方程式进行变换并评估解。 In this case the result is exact, but in most other instances you would probably want to cast the tests using all.equal . 在这种情况下,结果是准确的,但是在大多数其他情况下,您可能希望使用all.equal进行测试。

eval( substitute( x-y+z==1 && x+y-z==1 && x+y+z==3, list(x=1,y=1,z=1) ) )
[1] TRUE

Before the recognition that "-" is not the same as "–" I was getting: 在认识到“-”与“-”不同之前,我得到了:

Error: unexpected input in "eval( substitute( x‚" 错误:“ eval(替代(x,)”中意外输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM