How to simplify function handles?
8 views (last 30 days)
Show older comments
anuj maheshwari
on 19 Aug 2021
Commented: Walter Roberson
on 20 Aug 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 Comments
the cyclist
on 19 Aug 2021
It would helpful if you included a small example of what you see, and what you wish to see.
Accepted Answer
Walter Roberson
on 19 Aug 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 Comments
Walter Roberson
on 20 Aug 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)
iY = ilaplace(Ys)
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))
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)
Y5 = YY(X5idx)
syms t
subs(eiY, t, X5(:))
vpa(ans)
More Answers (0)
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!