vector output of a symbolic vector

2 ビュー (過去 30 日間)
Lóránt Farkas
Lóránt Farkas 2024 年 10 月 3 日
回答済み: Lóránt Farkas 2024 年 10 月 8 日
syms s X Y Z
l1 = X*s - 1 == 3*X + Y + Z + 2/s
l2 =Y*s == - X - Y - 1/s^2
l3 =Z*s - 2 == X - Z - (s - 1)/s^2
[Xsol,Ysol,Zsol]=solve([l1,l2,l3],[X,Y,Z])
[Xsol,Ysol,Zsol]=partfrac([Xsol,Ysol,Zsol])
Gives error: "Error using sym/partfrac
Too many output arguments."
Why?

回答 (3 件)

John D'Errico
John D'Errico 2024 年 10 月 3 日
編集済み: John D'Errico 2024 年 10 月 3 日
syms s X Y Z
l1 = X*s - 1 == 3*X + Y + Z + 2/s
l2 =Y*s == - X - Y - 1/s^2
l3 =Z*s - 2 == X - Z - (s - 1)/s^2
[Xsol,Ysol,Zsol]=solve([l1,l2,l3],[X,Y,Z])
Up to this point, you have done nothing wrong. Xsol, Ysol, and Zsol are all validly defined expressions of x, y, and z.
Then you tried to use partfrac, as applied to a vector, composed of [xsol,ysol,zsol].
help partfrac
--- help for sym/partfrac --- sym/partfrac - Partial fraction decomposition This MATLAB function finds the partial fraction decomposition of expr with respect to var. Syntax partfrac(expr,var) partfrac(expr,var,Name,Value) Input Arguments expr - Rational expression symbolic expression | symbolic function var - Variable of interest symbolic variable Name-Value Arguments FactorMode - Factorization mode 'rational' (default) | 'real' | 'complex' | 'full' See also children, coeffs, collect, combine, compose, divisors, expand, factor, horner, numden, rewrite, simplify, simplifyFraction Introduced in Symbolic Math Toolbox in R2015a Documentation for sym/partfrac doc sym/partfrac
You could have applied partfrac to any one of them.
Xsolpf = partfrac(Xsol)
However, partfrac is not vectorized. And when you tried that as you did, it gave you an error. The point is, you could just have called partfrac three times. But not everything you write in MATLAB will be vectorized to work as you might hope it should work. Code is only code. As much as we wish it would do so, it cannot do more than it was written to do.
Could you have done like this?
XYZsol = partfrac([Xsol,Ysol,Zsol])
And then separated each of the three components into a separate result? Well, yes.
XYZsol(1)
XYZsol(2)
XYZsol(3)
  1 件のコメント
John D'Errico
John D'Errico 2024 年 10 月 3 日
Sorry. The Answers bug is again present. When I wrote this post, if displays nicely. But when saved, it fails to do so.

サインインしてコメントする。


Vinay
Vinay 2024 年 10 月 3 日
The issue stems from the “partfrac” function, which is configured to accept only a single output argument. This function specifically performs partial fraction decomposition on:
  • A single symbolic expression, or
Xsol_partfrac = partfrac(Xsol);
Ysol_partfrac = partfrac(Ysol);
Zsol_partfrac = partfrac(Zsol);
  • An array of symbolic expressions
result=partfrac([Xsol,Ysol,Zsol])
To resolve this issue, decompose each expression individually using the “partfrac” function or to pass the array of symbolic expression and store result in a single argument. This approach ensures proper handling of each term and prevents the multiple output argument error.
Kindly refer to the below documentation of “partfrac” for more details:
I hope this helps!

Lóránt Farkas
Lóránt Farkas 2024 年 10 月 8 日
I can do workaround, however it contradicts the main concepts of Matlab! The output of partfrac is 1x3 vector of symbolic variables the left side of the assigment is also a 1x3 vector of symbolic variables. Why should I do the serialization? Why works with solve (whose output is also a 1x3 symbolic vector) but not with partfrac? The non-symbolic part of matlab do this without any problem (and that is why it is so popular).
Why can I not do an assigment with symbolic vectors on the left side?

カテゴリ

Help Center および File ExchangeSymbolic Variables, Expressions, Functions, and Preferences についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by