How to quickly "plug in" numerical values into symbolic equations to get a numerical result

172 views (last 30 days)
Setup:
syms s
fs = (-2*(s-1))/((s+1)*(s+2));
fs1 = (1/s)*fs;
yt1 = ilaplace(fs1)
At this point Matlab returns the following symbolic equation:
yt1 = 3*exp(-2*t) - 4*exp(-t) + 1
To find yt1 when t = 0 and t = inf I can do the following:
yt1_0 = 3*exp(-2*0) - 4*exp(-0) + 1
yt1_inf = 3*exp(-2*inf) - 4*exp(-inf) + 1
But I was hoping that there is a smarter way to manipulate symbolic equations like:
yt1(t ==0)
yt1(t ==inf)
Or something like that so I do not have to write out the entire equation and update "t" as I have above.
Thoughts?
Thank you,
C

Accepted Answer

Voss
Voss on 23 Jan 2022
You can use subs() to do that. It will evaluate yt1 and give you a symbolic value, on which you can use double() to convert to a numeric value.
syms s
fs = (-2*(s-1))/((s+1)*(s+2));
fs1 = (1/s)*fs;
yt1 = ilaplace(fs1)
yt1 = 
t = 0;
yt1_0 = subs(yt1);
t = Inf;
yt1_inf = subs(yt1);
display(yt1_0); display(yt1_inf);
yt1_0 = 
0
yt1_inf = 
1
yt1_0 = double(yt1_0);
yt1_inf = double(yt1_inf);
display(yt1_0); display(yt1_inf);
yt1_0 = 0
yt1_inf = 1
  1 Comment
Voss
Voss on 23 Jan 2022
Better yet, do both values of t at once:
syms s
fs = (-2*(s-1))/((s+1)*(s+2));
fs1 = (1/s)*fs;
yt1 = ilaplace(fs1)
yt1 = 
t = [0 Inf];
yt1_0_inf = subs(yt1);
display(yt1_0_inf);
yt1_0_inf = 
yt1_0_inf = double(yt1_0_inf);
display(yt1_0_inf);
yt1_0_inf = 1×2
0 1

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox 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!