cosine estimate taylor series

조회 수: 2 (최근 30일)
Elena
Elena 2022년 2월 22일
댓글: KSSV 2022년 2월 22일
sos, trying to fix in 30 min, not sure what is going wrong, im getiing 1.6985 e 5 and answer should be 0.6916.
function res = cosineEstimate(ang, k)
ang = '045-00-00'
k = 2
%DMS to degrees
angSplit = strsplit(ang, '-')
firstCell = angSplit(1)
angForm = str2double(firstCell)
%degree to rad
rad = (angForm/180)*pi;
r = 0;
for n = 1:k;
e = 2*n
f= factorial(e)
m = (-1)^(n)
r = r + m*(angForm^e)/f
res = r
end
end

답변 (2개)

KSSV
KSSV 2022년 2월 22일
편집: KSSV 2022년 2월 22일
x = 45*pi/180 ;
n = 10 ;
thesum = 0 ;
for i = 0:n
thesum = thesum+(-1)^i*x^(2*i)/factorial(2*i) ;
end
[cos(x) thesum]
ans = 1×2
0.7071 0.7071
  댓글 수: 1
KSSV
KSSV 2022년 2월 22일
cos(pi/4)
ans = 0.7071
How it is 0.6916? Are you targetting different angle? If not reduce the value of n and see.

댓글을 달려면 로그인하십시오.


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 2월 22일
Here is the corrected code:
ang = '045-00-00';
k = 2;
res = cosineEstimate(ang, k)
res = -0.6169
function res = cosineEstimate(ang, k)
%DMS to degrees
angSplit = strsplit(ang, '-');
firstCell = angSplit(1);
angForm = str2double(firstCell);
%degree to rad
RAD = (angForm/180)*pi;
res= 0;
for n = 1:k
res = res + ((-1) .^ (k-1) .* RAD .^ k) ./ factorial(k);
end
end

카테고리

Help CenterFile Exchange에서 Discrete Fourier and Cosine Transforms에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by