フィルターのクリア

Help with while loops

16 ビュー (過去 30 日間)
Giovanni Principato
Giovanni Principato 2021 年 3 月 25 日
コメント済み: Walter Roberson 2021 年 3 月 25 日
The Taylor series expansion for cos(x) is:
Where x is in radians. Write a MATLAB program that determines cos(x) using the Taylor series expansion. Use the value of x = 5. Then the program should use a while loop for adding the terms of the Taylor series. If the nth term in the series is , then the sum of the n terms is .
In each pass calculate the estimated error E given by . Stop adding terms when E ≤ 0.000001.
The program should display the statement "The value of cos(x) is X.XXXXXX." Please do not assign this fprintf to a variable. Just use the fprintf as you do in Matlab.
Start by initializing (defining an initial or starting value for) your variables before the while loop. The following variables should be initialized before the while loop: x, n, E, An, and Sn.
Set up your while loop to continue while E is greater than 0.000001 (aka the while loop will stop when E is less than or equal to 0.000001.
Inside your while loop, define An according to the equation given for cos(x) above.
Create a vector for Sn, starting at the n+1 element. You defined the 1st value value before the loop when you initialized the variable.
Next, define the error according to the equation presented above.
Then increment your n variable by adding 1 to the previous value to go to the next term of the series.
Finally, outside the while loop, create an fprintf to show the cos(x) approximation to 6 decimal places.
I have this as my code:
%Create a while loop that runs the until the value of E<-0.000001
x = 5;
n = 1;
Sn = 1;
E = 1;
An = 0;
while E > 0.000001
An = ((-1^n)/factorial(2*n))*x^(2*n)
Sn(n+1) = Sn + An
E = abs((Sn - Sn(n+1))/(Sn(n+1)))
n = n + 1
end
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 25 日
you assign to Sn(n+1) so after one iteration, Sn is a vector of length 2. You have
Sn(n+1) = Sn + An
But Sn is now a vector so the right hand side is a vector while the left side can only hold a scalar.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 3 月 25 日
This fixes your indexing, but not anything else.
format long g
%Create a while loop that runs the until the value of E<-0.000001
x = 5;
n = 1;
Sn = 1;
E = 1;
An = 0;
while E > 0.000001
An = ((-10^n)/factorial(2*n))*x^(2*n);
Sn(n+1) = Sn(n) + An;
n = n + 1;
E = abs((Sn(n) - Sn(n-1))/(Sn(n-1)));
end
E
E =
3.02961052110883e-07
Sn
Sn = 1×21
1 -124 -2728.16666666667 -24429.5555555556 -121310.755952381 -390425.201499118 -900111.651398242 -1600230.40125968 -2329520.76569867 -2925346.22684164 -3317336.66180412 -3529452.69803922 -3625519.74343556 -3662468.60704953 -3674687.14660442 -3678198.22118916 -3679083.06861475 -3679280.22713203 -3679319.34588546 -3679326.30156708 -3679327.41625965
Sn(end)
ans =
-3679327.41625965

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by