Why I keep getting error on this Numerical Integration problem ?
11 ビュー (過去 30 日間)
古いコメントを表示
syms x
f=((cos(log(x)./x))./x);
for i=1:1000
a=a+0.001*i;
b=b+0.001*(i+1);
s1=int(@f,x,a,b);
i=i+s1;
end
New to MATLAB. Can anyone please help me why can't I execute the code ?
Error: "f" was previously used as a variable, conflicting with its use here as the name of a function or command.
0 件のコメント
回答 (2 件)
Star Strider
2019 年 12 月 29 日
Do not use the function handle representation here.
Just use:
s1=int(f,x,a,b);
It would be preferable to define ‘f’ as a symbolic function:
f(x)=((cos(log(x)./x))./x);
Also, define ‘a’ and ‘b’ with some initial values before the loop. Not doing that will throw an error, since they are calculated recursively.
0 件のコメント
John D'Errico
2019 年 12 月 29 日
編集済み: John D'Errico
2019 年 12 月 29 日
Your problem is you are mixing up numerical integratino and symbolic integration. What are you trying to do?
A numerical solution might look like:
f = @(x) ((cos(log(x)./x))./x);
x = linspace(0,1,1001);
trapz(x,f(x))
ans =
NaN
It fails to generate anything useful, as I would expect based on what I write below.
A symbolic solution might look like:
syms x
K = ((cos(log(x)./x))./x);
int(K,0,1)
ans =
int(cos(log(x)/x)/x, x, 0, 1)
int returns no solution, which suggest only that it was unable to do the symbolic integral. We might try vpaintegral...
vpaintegral(K,0,1)
Error using sym/vpaintegral (line 202)
Failed precision goal. Try using 'MaxFunctionCalls'.
But it also fails. Why? To understand that, we need to look at your kernel.
f = @(x) ((cos(log(x)/x))/x);
fplot(f,[0,1])
UGH. A highly oscillatory integrand, that oscillates between -inf and inf near zero. You don't seriously want to integrate this, do you? It does look like Wolfram Alpha gves an answer, though I would want to verify that before I trust it very far.
integral_0^1 cos(log(x)/x)/x dx≈0.3233674...
Hmm. Suppose we try a transformation?
u = log(x)
then we have
du = dx/x
the integral becomes
int(cos(u*exp(-u)),-inf,0)
It looks a little simpler, but it is still highly oscillatory on that now infinite interval.
Again however, int and vapintegral will fail to yield a result that I can trust, and Wolfram Alpha gives a completely different answer. That suggests the answer is not as easy to find as you might think.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Calculus についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!