Using a while loop to solve a Taylor Series with approximate values.

f = atan(1);
n = 0;
i = -1;
err_approx = 100;
while (err_approx<=.5)
i = i + 2;
y_prev = y_approx
y_approx = (-1)^(i)(1/i+2)*1^(i+2)
err_approx = abs((y_approx-y_prev)/y_prev)*100
err_true = abs((y_approx-f)/f)*100
n = n + 1
[y_approx, err_approx, err_true, n]
end
This is my current code. I am using it to solve the taylor series expansion with approximate values to solve for arctan(1). I am having 2 errors. It is stating y_approx is undefined, and there is no output.
the expansion is in the form -x+x^(3)/3-x^(5)/5+x^(7)/7
Thanks in advance

回答 (1 件)

VBBV
VBBV 2022 年 10 月 14 日
編集済み: VBBV 2022 年 10 月 14 日
f = atan(1);
n = 0;
i = -1;
err_approx = 100;
while (err_approx >= .5) % check the condition here
i = i + 2;
y_approx = ((-1)^(i))*(1/(i+2))*1^(i+2)
y_prev = y_approx;
err_approx = abs((y_approx-y_prev)/y_prev)*100
err_true = abs((y_approx-f)/f)*100;
n = n + 1
[y_approx, err_approx, err_true, n];
end
y_approx = -0.3333
err_approx = 0
n = 1
y_approx
y_approx = -0.3333
Use a inequality opertor which satisfies the condition for while loop.
In your code, while loop is not satisfied, hence you get an error

8 件のコメント

f = atan(1);
n = 0;
i = -1;
err_approx = 100;
y_approx = 0; % give an initial value for y_approx
while (err_approx >= .5) % check the condition here
i = i + 2;
y_prev = y_approx;
y_approx = ((-1)^(i))*(1/(i+2))*1^(i+2);
err_approx = abs((y_approx-y_prev)/y_prev)*100;
err_true = abs((y_approx-f)/f)*100;
n = n + 1;
[y_approx, err_approx, err_true, n];
end
n
n = 200
y_approx
y_approx = -0.0025
VBBV
VBBV 2022 年 10 月 14 日
if you give intial value to y_approx and run the loop, you can see the resulting y_aapprox is found at the end of 200 iteration after while loop execution.
stephen
stephen 2022 年 10 月 14 日
how would i put an inquality into a while loop would that be done using min and max values?
VBBV
VBBV 2022 年 10 月 14 日
You have applied the inequality operator for the condition in while loop. But if you notice it is less than ( < ) which makes the condition not satisfied at the beginning of loop itself since 100 > 0.5, So, you need to use a greater than (>) operator to satisfy the condition and execute the statements inside the while loop.
stephen
stephen 2022 年 10 月 14 日
I am trying to get an approximate error between the current y value and the previous y value until that value is below .5%
stephen
stephen 2022 年 10 月 14 日
I see what you are saying now I am still havign trouble getting the code to output the answer in the right format though
stephen
stephen 2022 年 10 月 14 日
I am trying to get it to output a matrix of all values that were used
f = atan(1);
n = 0;
i = -1;
err_approx = 100;
y_approx = 0; % give an initial value for y_approx
while (err_approx >= .5) % check the condition here
i = i + 2;
y_prev = y_approx;
y_approx = ((-1)^(i))*(1/(i+2))*1^(i+2);
err_approx = abs((y_approx-y_prev)/y_prev)*100;
err_true = abs((y_approx-f)/f)*100;
n = n + 1;
Y_p(n) = y_approx;
E_p(n) = err_approx;
E_t(n) = err_true; % error
N(n) = n; % iter
end
[Y_p;E_p;E_t;N].'
ans = 200×4
-0.3333 Inf 142.4413 1.0000 -0.2000 40.0000 125.4648 2.0000 -0.1429 28.5714 118.1891 3.0000 -0.1111 22.2222 114.1471 4.0000 -0.0909 18.1818 111.5749 5.0000 -0.0769 15.3846 109.7942 6.0000 -0.0667 13.3333 108.4883 7.0000 -0.0588 11.7647 107.4896 8.0000 -0.0526 10.5263 106.7013 9.0000 -0.0476 9.5238 106.0630 10.0000

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

質問済み:

2022 年 10 月 14 日

コメント済み:

2022 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by