How to simplify function handles?

12 vues (au cours des 30 derniers jours)
anuj maheshwari
anuj maheshwari le 19 Août 2021
Commenté : Walter Roberson le 20 Août 2021
Y =
function_handle with value:
@(s) (F(s)-Q(s))/P(s)
As seen, I have three functions F, Q and P and Y is function made by algebraic operations on these three. Is there a way, to get a simplified expression for Y in terms of the function variable (s) ? When I type in Y in command window, I just get the result as shown above.
As a simplified example:
F = @(s) s;
Q = @(s) s^2;
P = @(s) s^3-3*s^2-1;
Y = @(s) (F(s) - Q(s))/P(s);
When i type in Y in command window, what is see is:
Y =
function_handle with value:
@(s) (F(s)-Q(s))/P(s)
What I wish to see is:
Y = (s-s^2)/(s^3-3*s^2-1)
  2 commentaires
the cyclist
the cyclist le 19 Août 2021
It would helpful if you included a small example of what you see, and what you wish to see.
anuj maheshwari
anuj maheshwari le 19 Août 2021
Added

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 19 Août 2021
You used a tag of symbolic which implies that you might have the Symbolic Toolbox. If so then
syms s
Ys = Y(s)
You might want to simplify() the result.
  7 commentaires
anuj maheshwari
anuj maheshwari le 20 Août 2021
Oh my actual "simple case" is :
((5946427424506845*s)/2417851639229258349412352 + (100*exp(-s/400000) - 100)/(s*(exp(-s/100000) - 1)) + 707092764722665/144115188075855872)/(s^2/10000000000 + s/5000 + 1)
and things will get more complex from there. If matlab isnt the tool for calculating inverse laplace of these functions, can you recommend something?
Walter Roberson
Walter Roberson le 20 Août 2021
In the below, eiY is the output to pay attention to: it is the symbolic form of the ilaplace expaned out from sym of roots into closed formula.
The plotting after that is investigative, to try to figure out why the fplot() is showing up with discontinuities.
According to the X / YY values, the matlabFunction() version of eiY is producing values with imaginary component roughly 1e-17 there. Zooming in trying to look for visible infinities or whatever, did not help. But taking some of the locations that produce non-zero imaginary part and evaluating them symbolically to 50 digits shows a mix of pure-rule output and very small imaginary parts; the amplitude of the imaginary parts decreases as the number of digits increases.
So what is happening here is that the matlabFunction() version is suffering from round-off error (and even the symbolic version does too). The root() that is the solution of the ilaplace has a conjugate pair, so in theory the positive and negative imaginary components should cancel, but in practice because of floating point round off, they do not.
In this particular case, at least for positive t, you should be able to just take real() of the values.
format long g
digits(50)
F = @(s) s;
Q = @(s) s.^2;
P = @(s) s.^3-3*s.^2-1;
Y = @(s) (F(s) - Q(s))./P(s);
syms s real
Ys = Y(s)
Ys = 
iY = ilaplace(Ys)
iY = 
Nth = @(expr,N) expr(N);
resolve_a_root = @(X6) Nth(solve(children(X6,1),children(X6,2),'maxdegree',4),children(X6,3));
resolve_a_summand = @(X5) mapSymType(X5, 'root', resolve_a_root);
resolve_nth_summand = @(X2,N) resolve_a_summand(subs(children(X2,1),children(X2,2),N));
map_symsum = @(X2) sum(arrayfun(resolve_nth_summand, repmat(X2,1,double(children(X2,4)-children(X2,3)+1)), children(X2,3):children(X2,4)));
expand_symsum = @(X1) mapSymType(X1, 'symsum', map_symsum);
eiY = simplify(expand_symsum(iY))
eiY = 
y = matlabFunction(eiY);
fplot(y, [1e-4,10e-4]);
fplot(y, [4.257475e-4 4.25749e-4]); ylim([-1.001 -1]); title('zoom fplot numeric')
fplot(eiY, [4.257475e-4 4.25749e-4]); ylim([-1.001 -1]); title('zoom fplot symbolic')
X = linspace(4.257475e-4, 4.25749e-4,1000);
YY = y(X);
subplot(2,1,1); plot(X, real(YY)); title('fixed numeric evaluation real')
subplot(2,1,2); plot(X, imag(YY)); title('fixed numeric evaluation imag')
X5idx = find(imag(YY), 5);
X5 = X(X5idx)
X5 = 1×5
0.000425747501501501 0.000425747504504504 0.000425747513513513 0.000425747516516516 0.00042574751951952
Y5 = YY(X5idx)
Y5 =
-1.00085203903026 + 1.38777878078145e-17i -1.00085203903628 - 1.38777878078145e-17i -1.00085203905432 - 1.38777878078145e-17i -1.00085203906033 + 1.38777878078145e-17i -1.00085203906635 - 1.38777878078145e-17i
syms t
subs(eiY, t, X5(:))
ans = 
vpa(ans)
ans = 

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by