vector output of a symbolic vector

2 views (last 30 days)
Lóránt Farkas
Lóránt Farkas on 3 Oct 2024
Answered: Lóránt Farkas on 8 Oct 2024
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?

Answers (3)

John D'Errico
John D'Errico on 3 Oct 2024
Edited: John D'Errico on 3 Oct 2024
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 Comment
John D'Errico
John D'Errico on 3 Oct 2024
Sorry. The Answers bug is again present. When I wrote this post, if displays nicely. But when saved, it fails to do so.

Sign in to comment.


Vinay
Vinay on 3 Oct 2024
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 on 8 Oct 2024
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?

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!