substitute value for variable

5 views (last 30 days)
Carolyn
Carolyn on 21 Feb 2018
Commented: Karan Gill on 21 Feb 2018
I have created a code to solve a differential in terms of one variable (all C variables and B are constants and defined previously). Now I want to plug a value in for that variable into the differential equation, but it won't let me. I tried using the 'subs' function, but that doesn't work. Help?
syms I1 I2
F1 = C1*(exp(C2*(I1-3)-1))-((C1*C2)/2)*(I2-3);
W1 = 0;
F2 = piecewise(x<1,W1,1<x<lambda,W2,x>lambda,W3);
Wo = F1 + F2;
Wo1 = diff(Wo,I1);
Wo2 = diff(Wo,I2);
clear I1 I2
I1 = trace(B);
I2 = .5*((trace(B))^2+trace(B^2));
subs(Wo1,I1,I2);
  2 Comments
Birdman
Birdman on 21 Feb 2018
Share all variables, the entire code.
Stephen23
Stephen23 on 21 Feb 2018
Carolyn's "Answer" moved here:
Here's the whole code. It all works until the last step.
% Define coefficients for all the fiber families
syms x p Wx I1 I2
pres = [p 0 0
0 p 0
0 0 p];
theta = [2 122 241];
C1 = .00124;
C2 = 1.07;
C3 = [1.03*10^-7 1.67*10^-7 1.59*10^-7];
C4 = [26.6 35 38];
lambda = [2.78 5.6 7.74];
C5 = C3.*C4.*lambda.*(exp(C4.*(lambda-1)));
% Define displacement matrix and B matrix
F = [x 0 0
0 1/sqrt(x) 0
0 0 1/sqrt(x)];
B = F*transpose(F);
% Define F1 as given
F1 = C1*(exp(C2*(I1-3)-1))-((C1*C2)/2)*(I2-3);
W1 = 0;
% Run pipline for each fiber family to get individual strain energy
% functions
for n = 1:3
a = [cos(theta(n))
-sin(theta(n))
0];
W2 = C3(n)*exp(C4(n)*(x-1))-1;
W3 = C5(n);
F2 = piecewise(x<1,W1,1<x<lambda(n),W2,x>lambda(n),W3);
Wo(n) = F1 + F2;
Wo1(n) = diff(Wo(n),I1);
Wo2(n) = diff(Wo(n),I2);
ao(n) = transpose(a(n))*a(n);
end
% Plug in values for a matrix and invariantes
clear I1 I2
I1 = trace(B);
I2 = .5*((trace(B))^2+trace(B^2));
%Substitute in the invariant values
for n=1:3
subs(Wo1(n),I1);
subs(Wo2(n),I2);
end

Sign in to comment.

Accepted Answer

Karan Gill
Karan Gill on 21 Feb 2018
You shouldn't overwrite I1. Overwriting I1 will not automatically substitute for it in Wo1. Instead, remove I1 = trace(B); and use
>> I1_val = trace(B)
I1_val =
2/x + x^2
>> Wo1 = subs(Wo1,I1,I1_val)
Wo1 =
[ piecewise(x < 1 | 139/50 < x | x in Dom::Interval(1, 139/50), (3317*exp((107*I1)/100 - 421/100))/2500000), piecewise(x < 1 | 28/5 < x | x in Dom::Interval(1, 28/5), (3317*exp((107*I1)/100 - 421/100))/2500000), piecewise(x < 1 | 387/50 < x | x in Dom::Interval(1, 387/50), (3317*exp((107*I1)/100 - 421/100))/2500000)]
This way, you separate the symbolic variable from the value you want to substitute, which keeps the meaning of variables in your code clean.
  2 Comments
Carolyn
Carolyn on 21 Feb 2018
Thank you! This worked perfectly.
Karan Gill
Karan Gill on 21 Feb 2018
Great! :) (And I understand overwriting I1 was the intuitive thing to do.)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!