how to remove error"Conversion to logical from sym is not possible."?
조회 수: 2 (최근 30일)
이전 댓글 표시
i make a fuction that take anynomous fuction as input with value ,
the fuction perform leftsum and definate integral if ' tol' value that is varialble take value from input lies between the value of left sum and integral the it generate the output 1 otherwise 0
i have made that code
function output=myleftcheck(f,a,b,n,tol)
syms x
dx=(b-a)/n;
% initialize r to f(a) (the left-end point of the first sub-interval
% [a,a+dx])
r=0;
% need only consider the n-1 remaining sub-intervals
for k=0:n-1
c=a+k*dx;
r=r+f(c);
end
t=dx*r;
o=int(f(x),a,b)
p=vpa(o)
if p < tol >t
output=1
else
output=0
end
the following error is coming,how i should debugg the error
>> myleftcheck(@(x) sin(x),0,3.14,10,0.1)
o =
1 - cos(157/50)
p =
1.999998731727539545285114306345
Conversion to logical from sym is not possible.
Error in myleftcheck (line 15)
if p<= tol >=t || t<=tol>=p
댓글 수: 0
채택된 답변
Steven Lord
2021년 10월 1일
This doesn't do what you probably think it does or want it to do.
p<= tol >=t
This is the same as:
(p<= tol) >=t
which would be equivalent to one of the following two expressions if p, tol, and t were all numeric variables.
0 >= t
1 >= t
If you want to ask if tol is greater than or equal to both p and t:
(p <= tol) & (tol >= t) % or rewriting
(p <= tol) & (t <= tol)
If you want to ask if tol is between p and t:
(p <= tol) & (tol <= t) % And similarly if p could be greater than t
(t <= tol) & (tol <= p)
But the fact that symbolic variables are involved complicates matters. [For general symbolic variables x and y, is x >= y? The answer in this context is "It's impossible to say."] One solution is not to use symbolic variables. Use integral to integrate your anonymous function numerically rather than evaluating your anonymous function at the symbolic variable x and using int to integrate it symbolically.
f = @sin;
numericAnswer = integral(f, 0, pi/2)
syms x
fs = f(x);
symbolicAnswer = int(fs, 0, pi/2)
When I run those lines of code the answers I receive are:
numericAnswer =
1.0000
symbolicAnswer =
1
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!