Question about Taylor Series While loop.

1 Ansicht (letzte 30 Tage)
Derryn
Derryn am 6 Mär. 2013
I'm completely stuck on this While Loop using Taylor Series.
x = input('Input the angle in radians: ');
Cos_Estimate = 0;
k = 0;
Errrr = 1
while Errrr > .000001
if mod(k,2)
Sign = 1;
else
Sign = -1;
end
k = k + 2;
Cos_Estimate = Cos_Estimate + (x^k/(factorial(k)*Sign));
Errrr = abs(Cos_Estimate - cos(x));
end
err = abs(Cos_Estimate - cos(x));
fprintf('The estimated cosine value based on the Taylor Series is: %0.6f \n',Cos_Estimate)
fprintf('The actual cosine value is : %0.6f \n',cos(x))
fprintf('The estimation error is: %0.6f \n',err)
fprintf('The number of terms required was: \n',term)
Now I am getting NaN for my variable. I'm stuck.
  2 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 6 Mär. 2013
Now your variable is Nan
Derryn
Derryn am 6 Mär. 2013
How do I fix this?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 6 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 6 Mär. 2013
Try this
x = input('Input the angle in radians: ');
Cos_Estimate = 1;
Errrr=1
Sign = 1;
k = 0;
while Errrr > .000001 & k<60
Sign =-Sign;
k = k + 2;
Cos_Estimate = Cos_Estimate + (x^k/(factorial(k)*Sign))
Errrr = abs(Cos_Estimate - cos(x))
end
display(Cos_Estimate)

Weitere Antworten (2)

Babak
Babak am 6 Mär. 2013
add this line to the beginning of your code:
Errrr =1;
  1 Kommentar
Derryn
Derryn am 6 Mär. 2013
Added it, but now I get this as my output:
EDU>> Lab7_Prob3Script
Input the angle in radians: pi
The estimated cosine value based on the Taylor Series is: NaN
The actual cosine value is : -1.000000
The estimation error is: NaN
The number of terms required was:

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
Azzi Abdelmalek am 6 Mär. 2013
Bearbeitet: Azzi Abdelmalek am 6 Mär. 2013
Your code never enter in the loop because Errrr is not defined
  5 Kommentare
Matt Kindig
Matt Kindig am 6 Mär. 2013
Bearbeitet: Matt Kindig am 6 Mär. 2013
Also, what exactly is the point of the "estimation error" calculation (calculation of 'err')? By definition, won't this be equal to Errrr, because you have defined it that way? Also, won't 'err' always be less than 0.000001, because of the way you have defined your loop?
Derryn
Derryn am 6 Mär. 2013
Ah ok thanks. Does this look better?
x = input('Input the angle in radians: ');
Cos_Estimate = 0; k = 0;
count = 0;
Errrr = 1;
while Errrr > .000001
count = count + 1;
if mod(k,2)
Sign = 1;
else
Sign = -1;
end
k = k + 2;
Cos_Estimate = Cos_Estimate + (x^k/(factorial(k)*Sign));
Errrr = abs(Cos_Estimate - cos(x));
if k > 170
break
end
end
err = abs(Cos_Estimate - cos(x));
fprintf('The estimated cosine value based on the Taylor Series is: %0.6f \n',Cos_Estimate)
fprintf('The actual cosine value is : %0.6f \n',cos(x))
fprintf('The estimation error is: %0.6f \n',err)
fprintf('The number of terms required was: %0.0f \n',count)
I'm still getting the wrong value for Cos_Estimate

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by