MATLAB: How to get the solve function to display values instead of solutions containing other variables

solve

%% A helical compressions spring is to be cycled between 150 lbf and
%300 lbf with a 1 in stroke. The number of cycles is low, so fatigue is not
%an issue. The coil must fit in a 2.1 in. diameter hole with a o.1 in
%clearance all the way around the spring. Use unpeened oil tempered wire
%with squared and ground ends.
%DETERMINE A SUITABLE WIRE DIAMETER, USING A SPRING INDEX OF C = 7.
clear all
syms d D N_a
D_od = 1.9; %outer diameter of coil
C = 7;
eqn1 = (D_od-d)/d == C;
solve(eqn1,d)
%DETERMINE A SUITABLE MEAN COIL DIAMETER
eqn2 = D/d == C;
solve(eqn2,D)
%DETERMINE NECESSARY SPRING CONSTANT
f_max = 300;
f_min = 150;
delta_f = f_max-f_min;
delta_x = 1;
k = delta_f/delta_x %spring constant
%dETERMINE SUITABLE NUMBER OF COILS
G = 11.2*10^6; %Constant from table...look at 10_5 example
eqn3 = (d^4*G)/(8*D^3*N_a) == k;
solve(eqn3,N_a)
N_t = N_a+2
%DETERMINE NECESSARY FREE LENGTH SO THAT IF THE SPRING WERE COMPRESSED TO
%ITS SOLID LENGTH, THERE WOULD BE NO YIELDING
L_s = (N_t+1)*d
A = 147 %from table 10-4

m = 0.187 %from table 10-4
S_ut = (A)/(d^m)
x = 0.5 %From table 10-6
S_sy = x*S_ut
k_B = (4*C+2)/(4*C-3) %
F_s = (pi*d^3*S_sy)/(8*k_B*D) %Force of the spring in pounds
L_o = F_s/k+L_s %Maximum length, can be less than this though

Best Answer

syms d D N_a
D_od = 1.9; %outer diameter of coil
C = 7;
eqn1 = (D_od-d)/d == C;
solve(eqn1,d)
ans = 
This second line of code solves the equation eqn1 for d and stores the solution in the variable ans. It does not automatically change the definition of d to contain the solution, as I suspect you may expect. So when you execute this next line:
eqn2 = D/d == C;
solve(eqn2,D)
ans = 
d does not have the value you obtained by solving eqn1, it remains the symbolic variable d. So this solve call solves for D in terms of that symbolic variable d.
If I store the solution in a variable and either use that variable directly or substitute its value into the equation, that equation will use the value the first solve call computed.
d_sol = solve(eqn1, d);
eqn2 = D/d_sol == C
eqn2 = 
solve(eqn2) % or
ans = 
eqn3 = subs(D/d == C, d, d_sol)
eqn3 = 
solve(eqn3)
ans = 
Related Question