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

조회 수: 9 (최근 30일)
Amanda Garcia-Menocal
Amanda Garcia-Menocal 2022년 11월 25일
댓글: Star Strider 2022년 11월 25일
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
Star Strider
Star Strider 2022년 11월 25일
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.
.

댓글을 달려면 로그인하십시오.

채택된 답변

Vilém Frynta
Vilém Frynta 2022년 11월 25일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by