how to remove error"Conversion to logical from sym is not possible."?

1 view (last 30 days)
Muhammad on 1 Oct 2021
Answered: Steven Lord on 1 Oct 2021
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
% initialize r to f(a) (the left-end point of the first sub-interval
% [a,a+dx])
% need only consider the n-1 remaining sub-intervals
for k=0:n-1
if p < tol >t
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 =
Conversion to logical from sym is not possible.
Error in myleftcheck (line 15)
if p<= tol >=t || t<=tol>=p

Accepted Answer

Steven Lord
Steven Lord on 1 Oct 2021
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 =
symbolicAnswer =

More Answers (0)

Community Treasure Hunt

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

Start Hunting!