MATLAB Answers

How can I get the solve function to display values instead of solutions containing other variables?

3 views (last 30 days)
Caleb Hedin
Caleb Hedin on 22 Nov 2020
Edited: Walter Roberson on 5 Dec 2020
%% 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

Accepted Answer

Steven Lord
Steven Lord on 22 Nov 2020
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 = 
  2 Comments
Steven Lord
Steven Lord on 23 Nov 2020
By using symbolic calculations, you told MATLAB you didn't want to approximate any of the values in double precision. If you want to see the result approximated in double precision when you're done, use either the double function (if your symbolic expression doesn't contain symbolic variables) or vpa (if it does.) [When you combined D_od's value of 1.9 with symbolic variables, MATLAB converted it into the expression 19/10.]
two = sym(2);
s = sqrt(two)
s = 
double(s)
ans = 1.4142
vpa(s)
ans = 
1.4142135623730950488016887242097

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!