MATLAB求解函数的输出顺序 [英] order of the outputs for the MATLAB solve function

查看:241
本文介绍了MATLAB求解函数的输出顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经修改了MATLAB解函数有一段时间了,但是似乎无法确定它如何确定输出符号变量的顺序.

I have been tinkering with the MATLAB solve function for a while, but cannot seem how it determines the order that it outputs the symbolic variables.

具体来说,我有一个要同时求解的方程组.

Specifically, I have a system of equations that I want to solve simultaneously.

a = f(a, b, c, d)
b = f(a, b, c, d)
c = f(a, b, c, d)
d = f(a, b, c, d)

,这些等式是符号性的,并且具有其他符号性变量(除了a,b,c和d之外). (因此,解决方案的输出不是数字的,而是符号的).

and these equations are symbolic and have other symbolic variables (aside from a, b, c, and d). (so the solution outputs aren't numeric, but are symbolic).

例如,当我求解弹簧倒立摆的运动方程时,我有两个方程都依赖于phiDDot和lenDDot.我通过以下调用使用Solve函数分别求解phiDDot和lenDDot:

For example, when I am solving the for the equations of motion for an inverted spring pendulum, I have two equations that are both dependent on phiDDot and lenDDot. I use the solve function to solve for phiDDot and lenDDot separately using this call:

[eom2, eom1] = solve(Lag(1)==0, Lag(2)==0, ddphi, ddlen);

ddphi的解对应于输出矩阵的第二项,而ddlen对应于矩阵的第一项.我想知道是否有某种方法告诉M​​ATLAB首先输出ddphi,然后输出ddlen,或者至少确定它们的输出顺序.当我求解四个以上的变量,并尝试使用ode45求解微分方程时,不知道变量的顺序成为一个大问题.

The solution for ddphi corresponds to the second term of the matrix outputted, while ddlen corresponds to the first term of the matrix. I was wondering whether there was some way to tell MATLAB to output ddphi first and ddlen second, or at least determine what order they are outputted. Not knowing the order of the variables becomes a big problem when I am solving for more than 4 variables, and trying to solve the differential equations using ode45.

任何建议都会有所帮助!

Any advice would be helpful!!

推荐答案

我认为,它是基于等式中变量名的ASCII值的字母顺序的.根据文档对于solve sym/symvar 来解析方程式输出变量. sym/symvar的帮助表示它以词典顺序返回变量,即字母顺序( symvar 做了同样的事情,尽管它不是这样说的,方法是调用 setdiff ).如果您查看solve.m的实际代码(在命令窗口中输入edit solve)并检查名为assignOutputs的子功能(R2012b中的第190行),您会看到它调用了 sort ,并且有关于字典顺序的评论.

I believe that it's alphabetical based on the ASCII values of the variable names in your equations. As per the documentation for solve, sym/symvar is used to parse the equations in the case where you don't supply the names of output variables. The help for sym/symvar indicates that it returns variables in lexicographical order, i.e. alphabetical (symvar does the same, even though it doesn't say so, by making calls to setdiff). If you look at the actual code for solve.m (type edit solve in your command window) and examine the sub-function called assignOutputs (line 190 in R2012b) you'll see that it makes a call to sort and that there's a comment about lexicographical order.

在R2012b(可能还有更早的版本)中,文档与R2013a的文档有所不同,似乎与您的问题有关.在R2013a中,添加了这句话:

In R2012b (and likely earlier) the documentation differs from that of R2013a in a way that seems relevant to your issue. In R2013a, this sentence is added:

如果您明确指定自变量vars,则求解器将使用相同的顺序 返回解决方案.

If you explicitly specify independent variables vars, then the solver uses the same order to return the solutions.

我仍在运行R2012b,因此无法确认这种不同的行为.

I'm still running R2012b, so I can't confirm this different behavior.

这篇关于MATLAB求解函数的输出顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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