matlab - How to get algebraic solution and symbolic Jacobian of this simple equation?
问题描述
(Crossposted from Matlab Answers) I am trying to simplify this set of algebraic equations. Then, I would like to have Matlab calculate the Jacobian for me. But it does not seem to work as I expect.
Consider this simple MWE:
% State Variables
syms x_0 x_1 x_2 x_3
% Input Variables
syms u_1 u_2 u_3
% Constants
syms k_1 V_liq dvs
% 3 Algebraic Equations
stateEquations = [...
x_1 == (x_0*(V_liq - u_1/dvs*1e3)*1e-3 + u_1)*1e3/V_liq*exp(-k_1), ...
x_2 == (x_1*(V_liq - u_2/dvs*1e3)*1e-3 + u_2)*1e3/V_liq*exp(-k_1), ...
x_3 == (x_2*(V_liq - u_3/dvs*1e3)*1e-3 + u_3)*1e3/V_liq*exp(-k_1)];
dstate_x3 = solve(stateEquations, x_3)
dstate_du = jacobian(dstate_x3, [u_1 u_2 u_3])
Since dstate_x3
is empty, the Jacobian is also empty. But I simply want Matlab to replace x_2
in eq. 3 by its right-hand side, and x_1
by its right-hand side...
Could you please give me a hint on how to achieve this with Symbolic Math Toolbox? (Deriving it manually would be very time-consuming, especially with x_i, i > 3
)
解决方案
Since your system has 3 equations, you must solve
it for 3 variables, not just for the variable x_3
. Because solve
doesn't know which variables you want so solve your system for, then it returns an empty solution.
You want to solve for x_1
, x_2
and x_3
, so replace the penultimate line of your code by
dstate = solve(stateEquations, [x_1 x_2 x_3])
Now dstate
is an 1x1 struct
with 3 <code>sym fields: x_1
, <code>x_2 and <code>x_3. Hence, replace the last line of your code by
dstate_du = jacobian(dstate.x_3, [u_1 u_2 u_3])
Eventually, you might want to <code>simplify(dstate_du).
推荐阅读
- java - 使用 if 语句时无法退出程序
- javascript - 尝试从外部 php 文件标记数据创建谷歌地图 api 集群
- matlab - 计算分段函数中的向量
- python - 尝试读取 .py 文件并创建 .mp4 输出时出现 CMD/Python 语法错误
- curl - 使用 curl 将不记名授权标头传递给 AWS 函数
- reactjs - 尽管 TypeScript 编译器错误,为什么我的 React Native 应用程序构建成功?
- sql - 在 where 子句中使用这个子查询的语法问题是什么
- c - 如何将格式化数据添加到 printf() 调用
- java - 'Dcoder' 练习失败的测试用例
- python - 如何在谷歌应用脚本中更新 mailgun 路由