I am trying to assign only positive integers to a variable.

16 Ansichten (letzte 30 Tage)
I have an equation which returns positive and negative real and imaginary numbers. I have already separated the real part and assigned it to a variable "w", which now returns positive and negative real numbers. I only need the positive numbers assigned to "w" so that when "h" is solved the result is only positive real numbers.
L=15.7;
l=11.6;
Wab=20;
Wbd=40;
Mab = Wab/32.2;
Mbd = Wbd/32.2;
Wbd2=(Wbd/(L-1));
d=2.58;
OB=L-l-d;
BD=d+OB;
Wbo=Wbd2*OB;
Wod=Wbd2*d;
delta=d*0.95;
syms Wc
eqn = (((11.6/2)+1.52)*20)+((((15.6-11.6)/2)-1.52)*Wbo)-((d/2)*Wod)-(delta*Wc)==0;
S = solve(eqn);
Wc = eval(S)
Wc = 56.8458
Mc = Wc/32.2;
D=490*(1/32.2);
syms w
eqn = Mc*D==(4*(w.^3));
V = solve(eqn);
w1 = eval(V);
w = real(w1)
w = 3×1
1.8867 -0.9434 -0.9434
h=2*w
h = 3×1
3.7734 -1.8867 -1.8867
  1 Kommentar
Star Strider
Star Strider am 25 Nov. 2022
Specify ‘w’ to be real, and then request 'ReturnConditions' on the evaluation to understand under what conditions ‘w’ will be real:
syms w real
eqn = Mc*D==(4*(w.^3));
V = solve(eqn, 'ReturnConditions',1)
That is likely the best you can do.
L=15.7;
l=11.6;
Wab=20;
Wbd=40;
Mab = Wab/32.2;
Mbd = Wbd/32.2;
Wbd2=(Wbd/(L-1));
d=2.58;
OB=L-l-d;
BD=d+OB;
Wbo=Wbd2*OB;
Wod=Wbd2*d;
delta=d*0.95;
syms Wc real
eqn = (((11.6/2)+1.52)*20)+((((15.6-11.6)/2)-1.52)*Wbo)-((d/2)*Wod)-(delta*Wc)==0;
S = solve(eqn)
S = 
% Wc = eval(S)
Mc = Wc/32.2;
D=490*(1/32.2);
syms w real
eqn = Mc*D==(4*(w.^3));
V = solve(eqn, 'ReturnConditions',1)
V = struct with fields:
w: [3×1 sym] parameters: [1×0 sym] conditions: [3×1 sym]
V.w
ans = 
V.conditions
ans = 
% w1 = eval(V)
% w = real(w1)
h=2*w
h = 
I leave you to explore those conditions at your leisure.
.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Vilém Frynta
Vilém Frynta am 25 Nov. 2022
Not sure if I understand correctly, but if you want to ignore the negative values, you can do:
w = [1.8 -0.9 -0.9]' % your values
w = 3×1
1.8000 -0.9000 -0.9000
w = w(w>0) % only positive values
w = 1.8000
Hope I helped, otherwise feel free to correct me or describe your problem.

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by