how to find values are within tol of on another or not?

1 回表示 (過去 30 日間)
Muhammad
Muhammad 2021 年 10 月 1 日
コメント済み: Voss 2021 年 12 月 29 日
i have the following code
that perform left sum and definate integral i have done it now i want to generate the output 1 if thes two valuse(leftsum and integral) are within tol of one another otherwise output 0
in myleftsum fuction f is function handel ,a and b are the real numbers a<b and n is an integer, tol is the positive number
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);
y=t
o=int(f(x),a,b)
if (o<=tol) & (tol<=t)
output=1
else
if (t<=tol) & (tol<=o)
output=1
else
output=0
end
end
code is runnnig but output is not coming correct
for
myleftcheck(@(x) sin(x),0,3.14,10,0.001)
output=1
for
myleftcheck(@(x) sin(x),0,3.14,10,0.001)
output=0
  1 件のコメント
Voss
Voss 2021 年 12 月 29 日
You are calling myleftcheck two times with the same inputs and getting two different results? That's not what I get:
myleftcheck(@(x) sin(x),0,3.14,10,0.001)
y = 1.9833
o = 
output = 0
ans = 0
myleftcheck(@(x) sin(x),0,3.14,10,0.001)
y = 1.9833
o = 
output = 0
ans = 0
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);
y=t
o=int(f(x),a,b)
if (o<=tol) & (tol<=t)
output=1
else
if (t<=tol) & (tol<=o)
output=1
else
output=0
end
end
end
Can you check that you have specified the two calls to myleftcheck correctly?

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeFormula Manipulation and Simplification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by