how to remove error"Conversion to logical from sym is not possible."?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Muhammad
le 1 Oct 2021
Réponse apportée : Steven Lord
le 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
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 commentaires
Réponse acceptée
Steven Lord
le 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 =
1.0000
symbolicAnswer =
1
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Calculus dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!