Incorrect Substitution with subs() from Workspace Variables?

3 views (last 30 days)
syms alpha s H t
system = 1/(s + 1/alpha);
y = ilaplace(1/s * (1-exp(-s*H)) * system)
y = 
Define specific values for alpha and H
% values
alpha = 15;
H = 32;
Substitute those values into y
foo = subs(y)
foo = 
Now substitute a specific value of t.
subs(foo,t,50)
ans = 
That looks like the correct result.
Define t as a workspace variable, and then try to subs in alpha, H, t all together, yields an incorrect result?
t = 50;
subs(y)
ans = 
0
Bug? Or am I misunderstanding something about @doc:subs?

Answers (1)

Piyush Kumar
Piyush Kumar on 14 Mar 2025
When you substitute all variables at once in MATLAB using subs(y, [alpha, H, t], [15, 32, 50]), MATLAB might not handle the substitutions in the expected order, leading to an incorrect result.
However, when you substitute variables one by one, MATLAB updates the expression incrementally, ensuring that each substitution is correctly applied in sequence. This avoids any conflicts or misinterpretations that might occur when substituting multiple variables at once.
Ensuring Correct Substitution
To ensure correct substitution, you can continue using the step-by-step approach, which helps MATLAB correctly map each variable:
% Define symbolic variables
syms alpha s H t
% Define the system
system = 1/(s + 1/alpha);
y = ilaplace(1/s * (1-exp(-s*H)) * system);
% Substitute specific values for alpha and H
alpha_value = 15;
H_value = 32;
t_value = 50;
% Substitute values one by one
foo = subs(y, alpha, alpha_value);
foo = subs(foo, H, H_value);
foo = subs(foo, t, t_value);
% Display the result
disp('Result after step-by-step substitution:');
Result after step-by-step substitution:
disp(foo);
Let's consider an example where the order of substitution can lead to different results.
Consider the expression:
Now, let's substitute x = y + z, y = z + 1, and z = 2.
Substitution Order 1:
Substitute z = 2 =>
Substitute y = z + 1 =>
Substitute x = y + z =>
In this case, the expression becomes undefined due to division by zero.
Substitution Order 2:
Substitute x = y + z =>
Substitute y = z + 1 =>
Substitute z = 2 =>
In this case, the expression evaluates to 5.
The order of substitution can significantly affect the final result, especially in complex expressions where variables are interdependent. In the first order, the expression becomes undefined due to division by zero, while in the second order, the expression evaluates to a finite value.
  3 Comments
Paul
Paul on 21 Mar 2025
MathWorks Tech Support said that they will consider fixing ilaplace in a future release.
Walter Roberson
Walter Roberson on 21 Mar 2025
When you substitute all variables at once in MATLAB using subs(y, [alpha, H, t], [15, 32, 50]), MATLAB might not handle the substitutions in the expected order, leading to an incorrect result.
When you subs() using a vector of variable names and a vector of values, subs() reads off the variable names and the corresponding replacement values, and does the substitutions effectively simultaneously. For example
syms a b
f = 10*a + b
f = 
subs(f, [a b], [b a])
ans = 
This contrasts with iterative order, which is not used
subs(subs(f, a, b), b, a)
ans = 
subs(subs(f, b, a), a, b)
ans = 
Simultaneous order is also necessary to get matrix subsitution right
subs([a, b], {a, b}, {[1;2], [3;4]})
ans = 
subs(subs([a, b], {a}, {[1;2]}), {b}, {[3;4]})
ans = 
subs(subs([a, b], {b}, {[3;4]}), {a}, {[1;2]})
ans = 
Therefore, doing simutaneous substitution does produce the expected results -- unless, that is, what is expected is wrong.
subs(10*a + b, [a b], [b 3])
ans = 
If the expectation was that the 10*a would first be processed through a->b, giving 10*b+b --> 11*b, and that then the b->3 would be processed, giving 33 as the final result, then Yes, the actual results might be "unexpected"
But when something like
subs(y, [alpha, H, t], [15, 32, 50])
is processed, the order of simultaneous substitutions is irrelevant.
The fact that the substitutions is not iterative can in theory matter for situations such as diff(f,alpha) as iterative substitution could end up turning the diff(f,alpha) into diff(f,15) which would be a request for the 15th derivative of the default variable.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!