关于使用符号数学求解方程组的问题 [英] question about solving system of equations using symbolic math

查看:148
本文介绍了关于使用符号数学求解方程组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用符号符号来求解线性方程组.所以我准备下面的代码.

I want to use symbolic symbol to solve a system of linear equation. So I prepare the following code.

A=[1,2;3,4];

% syms x
x=sym('x_%d',[2 1]);

eqn=A*x==[1;2];

result=solve(eqn,x)

有趣的是,它可以工作,但是当我读取变量结果时,它给出了一个带有x_1的1X1结构,而x_2是1X1符号.但是我期望得到的应该是2个真实值,为什么呢?有人可以解释吗?备注:不想使用A ^ -1 * [1; 2]来获得答案.

Interestingly, it works, but when I read the variable result, it gives a 1X1 struct with x_1 and x_2 are 1X1 sym. But what I expect get should be 2 real values, why? Could someone explain it? Remark: do not want to use A^-1*[1;2] to obtain the answer.

推荐答案

  • 如果将输出设置为 单个变量 solve返回一个结构 包含所有解决方案的数据类型,以获取每种解决方案的使用 点.赋值,例如result.x_1result.x_2
    • If you set the output to single variable solve returns a structure data type that contains all the solutions, to get each solution use the dot. assignment, like result.x_1 or result.x_2
    • 代码如下

      A=[1,2;3,4];
      
      % syms x
      x=sym('x_%d',[2 1]);
      
       eqn=A*x==[1;2];
      result = solve(eqn,x);
      result.x_1
      % 0
      result.x_2
      % 1/2
      


      • 如果要使用result作为数组,请使用 多种输出格式 ,例如 result(1)表示第一个变量,result(2)表示第二个变量

        • If you want to have result as an array, use multiple output format, like result(1) for the first variable, result(2) for the second variable
        • 代码如下

          A=[1,2;3,4];
          
          % syms x
          x=sym('x_%d',[2 1]);
          
           eqn=A*x==[1;2];
          
          [result(1), result(2)] = solve(eqn,x);
          result
          % result = [0 , 1/2]
          

          这篇关于关于使用符号数学求解方程组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆