フィルターのクリア

I cant figure out why my function is giving me NaN and not an answer

2 ビュー (過去 30 日間)
Colleen Gibbons
Colleen Gibbons 2017 年 2 月 15 日
編集済み: Walter Roberson 2017 年 2 月 15 日
degree=input('Enter the value of x in degrees to compute sin(x): ');
rad=(pi/180)*degree;
x=rad
error=1;
n=0;
while error >=(10^-5);
terms = ((-1)^n)*(((x^(n+1)))/factorial(n+1));
dsin=sum(terms);
n=n+1;
error=abs((sin(x)-dsin)/sin(x))*100;
end
disp(['dsin(x)= ',num2str(dsin)])
Enter the value of x in degrees to compute sin(x): 90
x =
1.5708
dsin(x)= NaN

採用された回答

Rahul Kalampattel
Rahul Kalampattel 2017 年 2 月 15 日
編集済み: Rahul Kalampattel 2017 年 2 月 15 日
The line dsin=sum(terms) isn't summing anything, since terms isn't a vector. You can either make it a vector, or initialise dsin and change the line to dsin=dsin+terms.
Also your Taylor series for sine was using the wrong indices, you only need odd numbers for n, so increment +2 instead of +1 (making sure that the +/- signs are still in the right places).
% Edited code %
degree=input('Enter the value of x in degrees to compute sin(x): ');
rad=(pi/180)*degree;
x=rad;
error=1;
n=0;
dsin=0;
while error >=(10^-5)
newTerms = ((-1)^(n/2))*(((x^(n+1)))/factorial(n+1))
dsin=dsin+newTerms
n=n+2;
error=abs((sin(x)-dsin)/sin(x))*100
end
disp(['sin(x)= ',num2str(dsin)])

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by