Hi, I need to find out the minimum N so that Eg<10^(-7). I got that N=199...and it's to much... Can anyone help me what is the problem with my code?
Thanks,
<<
>>

2 件のコメント

James Tursa
James Tursa 2015 年 5 月 24 日
Please don't post your code as an image ... it forces the reader to type your code in by hand in order to run it, and nobody wants to do that. Please re-post your code as text and use the { } Code button to format it.
david david
david david 2015 年 5 月 25 日
編集済み: david david 2015 年 5 月 25 日
h=1/2000;
er= 10^(-7);
N=1;
eg=10;
while (eg>er)
sumup=0;
sumdown=0;
for m=0:5000,
nsum=0;
for n=0:N,
nsum=nsum+(((32/((pi).^3)).*(((-1)^n)/((2.*n+1).^3).*(cos(((2*n+1)/5)*(pi)*m*h)))));
end
sumup=sumup+(abs(nsum-(1-(2.*m.*h/5).^2)).^2);
sumdown=sumdown+((1-(2.*m.*h/5).^2).^2);
end
eg=sqrt((sumup)/(sumdown));
N=N+1;
end
N;

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

 採用された回答

Stephen23
Stephen23 2015 年 5 月 25 日
編集済み: Stephen23 2015 年 5 月 25 日

2 投票

Here is a solution using no loops, and only vectorized code:
hy = 1/2000;
mv = 0:5000;
den = sqrt(sum((1-(2/5*mv*hy).^2).^2));
pfx = @(nv) (-1).^nv ./ (2*nv + 1).^3;
trg = @(nv) bsxfun(@times, (2*nv+1)/5, pi*mv*hy);
foo = @(nv) bsxfun(@times, pfx(nv), cos(trg(nv)));
bar = @(nv) sqrt(sum((32/pi^3 * sum(foo(nv)) - (1-(2/5*mv*hy).^2)).^2));
fun = @(N) bar((0:N)') / den;
First lets take a look at the function:
>> X = 1:300; % range of N values
>> Y = arrayfun(fun, X);
>> semilogy(X,Y)
And now the first value <1e-7 occurs for an N of:
>> Z = Y<1e-7;
>> X(find(Z,1,'first'))
ans =
198

1 件のコメント

david david
david david 2015 年 5 月 26 日
Thanks

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Environment and Settings についてさらに検索

製品

タグ

タグが未入力です。

質問済み:

2015 年 5 月 24 日

コメント済み:

2015 年 5 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by