フィルターのクリア

Array indices must be positive integers or logical values Error

2 ビュー (過去 30 日間)
Abigail Mullen
Abigail Mullen 2018 年 9 月 4 日
回答済み: Ashutosh Prasad 2018 年 9 月 7 日
The question I am trying to answer is: Write a program to test the two finite difference approximations in section 1.6 (the forward and centered difference formulas). Test your program on the function arctan(x) at x=sqrt(2).Start with h = 1/2, and generate a sequence of approximations by reducing h by a factor of 2 each time, until h = 2−40. Observe how the error changes with h.
So far I have;
f(x)=atan(x);
x=sqrt(2) ;
h=1:.5:2^(-40);
%Forward Euler
F(x)=((f(x+h)-f(x))/h);
I keep getting the array indices must be positive integers or logical values error.
  2 件のコメント
Adam
Adam 2018 年 9 月 4 日
f(x)=atan(x);
What are you looking to do with this line? f will be an array here and x the index into it. You haven't defined x before this line though immediately afterwards you define it to sqrt(2) which clearly is not an integer to use as an index.
Are you expecting the above line to define a function? If so you would need it to be more like
f = @(x) atan( x );
Abigail Mullen
Abigail Mullen 2018 年 9 月 4 日
I suppose I am just not sure how to do this problem, thank you for your help - I am new to matlab.

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

回答 (1 件)

Ashutosh Prasad
Ashutosh Prasad 2018 年 9 月 7 日
The error that you are getting is because of the way you are defining the h vector. Your definition creates an empty vector with no elements. But I understand you want h to be halved on each iteration, that's when loops come handy.
Try execution the following script
f = @(x) atan(x);
x = sqrt(2) ;
h = 0.5;
%Forward Euler
while(h >= 2^(-40))
F = ((f(x+h)-f(x))/h);
h = h/2;
end
Let me know if this helps

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by