# substitute variables into values

(Edit: I have recently started with Sagemath for an engineering course I am attending. I don't have access to Maple or Mathematica, so I wanted to give Sagemath a try. I am trying to combine info and examples from several sources but sadly I don't have any experience with Python)

I am trying to use Sagemath for a mechanics problem, for which I am at first solving a DE symbolically and later on assign physical properties to the parameters and finally plot the result. (It's a 2nd order bending problem) There are quite some parameters that I have to use, some parameters have to be expressed in terms of other parameters.

A1, A2, A3, A4, K, kt, qz, qzc, l, E, I, A, al, dT, y = var('A1, A2, A3, A4, K, kt, qz, qzc, l, E, I, A, al, dT, y')
eq1 = -A3-A4*K*l+A3*cos(K*l)+A4*sin(K*l)+((qzc*l^2)/(2*K^2)) == 0
eq2 = -A3*K^2*cos(K*l)-A4*K^2*sin(K*l)+(qzc/(K^2)) == 0
solutions = solve([eq1,eq2], A3, A4)
sol = solutions 
sol_A3 = sol.rhs()
sol_A4 = sol.rhs()
sol_A3

1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))

sol_A4

1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))

w2 = function('w2')(x)
w2 = -sol_A3 - sol_A4*K*x + sol_A3*cos(K*x) + sol_A4*sin(K*x) + (qzc*x^2)/(2*K^2)
w2

-1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*K*qzc*x/(K^5*l*cos(K*l) - K^4*sin(K*l)) + 1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc*cos(K*x)/(K^5*l*cos(K*l) - K^4*sin(K*l)) + 1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc*sin(K*x)/(K^5*l*cos(K*l) - K^4*sin(K*l)) - 1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l)) + 1/2*qzc*x^2/K^2


From this point on I want to assign values to variables, and express variables by means of other variables.

E = 21*10^4
I = 349*10^4
A = 2120
l = 3000
al = 11*10^(-6)
qz = 15
dT = 100
kt = (A*E)/l
qzc = qz/(E*I)
K = sqrt((al*dT*A*kt*l)/((E*A+kt*l)*I))
w2_intermediate = -sol_A3 - sol_A4*K*x + sol_A3*cos(K*x) + sol_A4*sin(K*x) + (qzc*x^2)/(2*K^2)
w2_intermediate

-1/2000*sqrt(583/1745)*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc*x/(K^5*l*cos(K*l) - K^4*sin(K*l)) + 1/32648*x^2 + 1/2*(2*K*l -      (K^2*l^2 + 2)*sin(K*l))*qzc*cos(1/1000*sqrt(583/1745)*x)/(K^5*l*cos(K*l) - K^4*sin(K*l)) + 1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc*sin(1/1000*sqrt(583/1745)*x)/(K^5*l*cos(K*l) - K^4*sin(K*l)) - 1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))


I don't know how to substitute the kt, qzc and the K's in this function.

edit retag close merge delete

Sort by » oldest newest most voted

The key is to proceed in steps.

We tell Sage exactly what we want to do at each step.

Set up the variables and equations:

sage: A1, A2, A3, A4, K, qz, qzc, l, E, I, A, al, dT, y \
....: = var('A1, A2, A3, A4, K, qz, qzc, l, E, I, A, al, dT, y')
sage: eq1 = -A3-A4*K*l+A3*cos(K*l)+A4*sin(K*l)+((qzc*l^2)/(2*K^2)) == 0
sage: eq2 = -A3*K^2*cos(K*l)-A4*K^2*sin(K*l)+(qzc/(K^2)) == 0


Solve:

sage: solutions = solve([eq1,eq2], A3, A4)
sage: sol_A3, sol_A4 = (eq.rhs() for eq in solutions)


Check the solutions:

sage: sol_A3
1/2*(2*K*l - (K^2*l^2 + 2)*sin(K*l))*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))
sage: sol_A4
1/2*((K^2*l^2 + 2)*cos(K*l) - 2)*qzc/(K^5*l*cos(K*l) - K^4*sin(K*l))


Define a new function:

sage: w2(x) = sol_A3*(cos(K*x) - 1) + sol_A4*(sin(K*x) - K*x) + (qzc*x^2)/(2*K^2)


Define some auxiliary variables and replacement values:

sage: kt = (A*E)/l
sage: literal_values = {
....:     qzc: qz/(E*I),
....:     K: sqrt((al*dT*A*kt*l)/((E*A+kt*l)*I)),
....: }
sage: numerical_values = {
....:     E: 21e4,
....:     I: 349e4,
....:     A: 2120.,
....:     l: 3000.,
....:     al: 11e-6,
....:     qz: 15.,
....:     dT: 100.,
....: }


Substitute:

sage: w2_intermediate = w2(x).subs(literal_values).subs(numerical_values)
sage: w2_intermediate
0.0000306297476108797*x^2 + (2.04666393778142e-11)*(9.80918229551222*sqrt(1/2) - 10.0137535816619*sin(2.45229557387805*sqrt(1/2)))*(cos(0.000817431857959351*sqrt(1/2)*x) - 1)/((1.09491158898182e-12)*sqrt(1/2)*cos(2.45229557387805*sqrt(1/2)) - (4.46484347419151e-13)*sin(2.45229557387805*sqrt(1/2))) - (2.04666393778142e-11)*(0.000817431857959351*sqrt(1/2)*x - sin(0.000817431857959351*sqrt(1/2)*x))*(10.0137535816619*cos(2.45229557387805*sqrt(1/2)) - 4)/((1.09491158898182e-12)*sqrt(1/2)*cos(2.45229557387805*sqrt(1/2)) - (4.46484347419151e-13)*sin(2.45229557387805*sqrt(1/2)))


Deal with the remaining exact sqrt(1/2):

sage: w2_intermediate = w2_intermediate.subs({1/2: 0.5})
sage: w2_intermediate
0.0000306297476108797*x^2 - 0.117540775230596*x + 106.403583186866*cos(0.000578011609920976*x) + 203.353657976985*sin(0.000578011609920976*x) - 106.403583186866

more

It works great! Thank you! The code gets more compact this way.