Finding unknown in MATLAB

I would like to find the unknown value d in this code. Unfortunately, something wrong between line 19 and line 24 because the answer printed as ans = 0. I don't know why?
d answer should be approximately 4.33832
format short
%from table A-2
lbf = 4.45; %Pound-force to Newton 1 pound-force = 4.45 Newton
inch = 25.4; %inch to mm, 1 inch = 25.4 millimeter
%-----------------------------------
%Assumptions
Zie = 0.15; %Minimized space usage
F.S = 1.2; %Factor of Safety
%--------------------------------------------------------------------------
D = 1.25*inch, fprintf('mm') %Mean Diameter of the Coil
Req_valve_lift = 0.9*inch, fprintf('mm')
Fpreload = 50*lbf, fprintf ('N')
Fmax = 150*lbf, fprintf ('N')
%--------------------------------------------------------------------------
%Table 10-4
syms d
Ssy = (1974)*(d^0.108);
Kb = (4*((1.25*inch)/d)+2)/(4*((1.25*inch)/d)-3);
(Ssy*0.56)/F.S == (Kb*((8*Fmax*D)/(pi*d^3)));
sol=solve((Ssy*0.56)/F.S == (Kb*((8*Fmax*D)/(pi*d^3))),d);

 Risposta accettata

Walter Roberson
Walter Roberson il 6 Mar 2021
Modificato: Walter Roberson il 6 Mar 2021
Q = @(v) sym(v);
format short
%from table A-2
lbf = Q(4.45); %Pound-force to Newton 1 pound-force = 4.45 Newton
inch = Q(25.4); %inch to mm, 1 inch = 25.4 millimeter
%-----------------------------------
%Assumptions
Zie = Q(0.15); %Minimized space usage
F.S = Q(1.2); %Factor of Safety
%--------------------------------------------------------------------------
D = Q(1.25)*inch, fprintf('mm') %Mean Diameter of the Coil
D = 
mm
Req_valve_lift = Q(0.9)*inch, fprintf('mm')
Req_valve_lift = 
mm
Fpreload = Q(50)*lbf, fprintf ('N')
Fpreload = 
N
Fmax = Q(150)*lbf, fprintf ('N')
Fmax = 
N
%--------------------------------------------------------------------------
%Table 10-4
syms d
Ssy = Q(1974)*(d^Q(0.108));
Kb = (Q(4)*((Q(1.25)*inch)/d)+Q(2))/(Q(4)*((Q(1.25)*inch)/d)-Q(3));
eqn = (Ssy*Q(0.56))/F.S == (Kb*((Q(8)*Fmax*D)/(Q(pi)*d^Q(3))));
eqn
eqn = 
sol = solve(eqn,d, 'returncondition', true);
condch = children(sol.conditions);
P = sym2poly(lhs(condch{1}));
z1 = roots(P);
mask = isAlways(subs(condch{2} & condch{3},sol.parameters,z1));
useful_z1 = z1(mask);
d_solutions = subs(sol.d, sol.parameters, useful_z1)
d_solutions = 
vpa(d_solutions)
ans = 

5 Commenti

Note: I improved the answer after you accepted it. (There are some glitches in the code-running facility that sometimes require me to save the answer to get further.)
Berke ERKAN
Berke ERKAN il 6 Mar 2021
I appreciated. I am really surprised that the answer and solution may be that long and complicated. It is too easy to find the "d" by hand.
I take some errors such as
Brace indexing is not supported for variables of this type.
Error in sym/subsref (line 898)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in deneme (line 29)
P = sym2poly(lhs(condch{1}));
But I really appreciated to your work.
Which version are you using? The output above is from a live run in R2020b.
You can proceed numerically using the exponent of d as a numeric exponent and taking a log approach, but you cannot get all of the solutions that way.
By the way, it is a category error to try to solve equations that have floating point coefficients using solve(). solve() is for finding indefinitely precise solutions, like a thousand-page formula to find the exact irrational number. But it does not make sense to ask for an infinitely precise solution for equations that contain coefficients that have relative errors exceeding 5%, such as 0.9.
Berke ERKAN
Berke ERKAN il 7 Mar 2021
I am using R2020a.
Firstly, my main goal was to code an emulator calculator that can be calculate spring diameter which is "d" for this question. I edited the question just after the submitted here. I added the expected "d" value which is 4.33832.
The change for R2020a turned out to be small. The results are the same as above, though.
You can test for yourself that d around 4.33832 is not a solution by substituting it into eqn
Q = @(v) sym(v);
format short
%from table A-2
lbf = Q(4.45); %Pound-force to Newton 1 pound-force = 4.45 Newton
inch = Q(25.4); %inch to mm, 1 inch = 25.4 millimeter
%-----------------------------------
%Assumptions
Zie = Q(0.15); %Minimized space usage
F.S = Q(1.2); %Factor of Safety
%--------------------------------------------------------------------------
D = Q(1.25)*inch, fprintf('mm') %Mean Diameter of the Coil
Req_valve_lift = Q(0.9)*inch, fprintf('mm')
Fpreload = Q(50)*lbf, fprintf ('N')
Fmax = Q(150)*lbf, fprintf ('N')
%--------------------------------------------------------------------------
%Table 10-4
syms d
Ssy = Q(1974)*(d^Q(0.108));
Kb = (Q(4)*((Q(1.25)*inch)/d)+Q(2))/(Q(4)*((Q(1.25)*inch)/d)-Q(3));
eqn = (Ssy*Q(0.56))/F.S == (Kb*((Q(8)*Fmax*D)/(Q(pi)*d^Q(3))));
eqn
sol = solve(eqn,d, 'returncondition', true);
condch = children(sol.conditions);
P = sym2poly(lhs(condch(1)));
z1 = roots(P);
mask = isAlways(subs(condch(2) & condch(3),sol.parameters,z1));
useful_z1 = z1(mask);
d_solutions = subs(sol.d, sol.parameters, useful_z1)
vpa(d_solutions)

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by