Conversion to logical from sym is not possible

조회 수: 1 (최근 30일)
Nguyen Tien Dung
Nguyen Tien Dung 2020년 7월 3일
댓글: Nguyen Tien Dung 2020년 7월 3일
Hi everyone, can you help me fix this problem
1. This is my matlab code
clc
clear all
syms v u w
w= exp(v)/(v^2 + u)
diffw = diff(w,v)
simplify(diffw)
eq = v^2 - 2*v + u == 0
D1 = 4-4*u < 0
D2 = 4-4*u == 0
D3 = 4-4*u > 0
if D1 < 0
fprintf('No solution')
solve(D1,u)
elseif D == 0
fprintf('Dual solution')
solveD(D2,u)
else D > 0
fprintf('Two solution')
solve(D3,u)
end
prompt1='u=';
u= input(prompt1);
xSol=solve(eq,v)
subs(xSol,u)
Error : Conversion to logical from sym is not possible
2.I don't know matlab can solve > 0 or < 0 with no u(number) so i put another code and it have the same error
clc
clear all
syms v u w
w= exp(v)/(v^2 + u)
diffw = diff(w,v)
simplify(diffw)
eq = v^2 - 2*v + u == 0
prompt1='u=';
u= input(prompt1);
D1 = 4-4*u < 0
D2 = 4-4*u == 0
D3 = 4-4*u > 0
if D1 < 0
fprintf('No solution')
solve(D1,u)
elseif D == 0
fprintf('Dual solution')
solveD(D2,u)
else D > 0
fprintf('Two solution')
solve(D3,u)
end
xSol=solve(eq,v)
subs(xSol,u)
Error :When I put u=1
D1 =
logical
0
D2 =
logical
1
D3 =
logical
0
Unrecognized function or variable 'D'.
elseif D == 0
Thank you very much.

채택된 답변

madhan ravi
madhan ravi 2020년 7월 3일
편집: madhan ravi 2020년 7월 3일
4 * u
% ^
Give assumptions for u:
assume(u > 0)
D1 = isAlways(4-4*u < 0)
D2 = isAlways(4-4*u == 0)
D3 = isAlways(4-4*u > 0)
And in if block remove the comparator.
if D1
fprintf('No solution')
solve(D1,u)
elseif D2
fprintf('Dual solution')
solveD(D2,u)
else D3
fprintf('Two solution')
solve(D3,u)
end
  댓글 수: 2
Nguyen Tien Dung
Nguyen Tien Dung 2020년 7월 3일
Sorry I fixed it, but it seems like my error is in the if command
Nguyen Tien Dung
Nguyen Tien Dung 2020년 7월 3일
Soryy if i don't put u and just use command if
D1 = 4-4*u < 0
D2 = 4-4*u == 0
D3 = 4-4*u > 0
if D1
fprintf('No solution')
solve(D1,u)
elseif D2
fprintf('Dual solution')
solve(D2,u)
else D3
fprintf('Two solution')
solve(D3,u)
end
can matlab solve
u>1
u=1
u<1
Thanks for your help :))

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

추가 답변 (0개)

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by