taylor expanssion calculation result is wrong

function [SN] = sintaylorfunction(A, tol)
k = 0;
error = 0 ;
sin = 0;
SN = sin;
sinplus1 = 0;
SM = sinplus1;
while E <= tol
sin = (-1)^k * A^(2*k+1)/factorial(2*k+1);
SN = SN + sin;
sinplus1 = (-1)^(k+1) * A^(2*(k+1)+1)/factorial(2*(k+1)+1);
SM = SM + sinplus1;
error= abs( max(SM - SN));
end
end
I wrote the sin function in this way. When I calculate sin(pi/2), I get sin(pi/2) = 1.5708. However, we know that sin(pi/2) = 1. Where is my mistake in my function?

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 3일
편집: Ameer Hamza 2020년 11월 3일

0 개 추천

kou are not incrementing 'k' inside the while-loop. Also, the while-condition is incorrect. Try the following code
sintaylorfunction(pi/2, 0.01)
function [SN] = sintaylorfunction(A, tol)
k = 0;
error = inf;
sin = 0;
SN = sin;
sinplus1 = 0;
SM = sinplus1;
while error >= tol
sin = (-1)^k * A^(2*k+1)/factorial(2*k+1);
SN = SN + sin;
sinplus1 = (-1)^(k+1) * A^(2*(k+1)+1)/factorial(2*(k+1)+1);
SM = SM + sinplus1;
error= abs( max(SM - SN));
k = k+1;
end
end

댓글 수: 6

thank very much!! I didnt know the increment term. It helps very much. Well, my another question is that will I use the same things for cos expansion in the same while loop except for its fomula ?
Yes, the formula for taylor series of cos is a bit different as shown in your question too. The method will be same.
sintaylorfunction(pi/2, 0.01)
function [SN CN] = sintaylorfunction(A, tol)
k = 0;
error = inf;
sin = 0;
SN = sin;
sinplus1 = 0;
SM = sinplus1;
while error >= tol
sin = (-1)^k * A^(2*k+1)/factorial(2*k+1);
SN = SN + sin;
sinplus1 = (-1)^(k+1) * A^(2*(k+1)+1)/factorial(2*(k+1)+1);
SM = SM + sinplus1;
error= abs( max(SM - SN));
k = k+1;
end
n=0;
error_cos=inf;
cos=1;
CN=cos;
cosplus1 = 1;
CM = cosplus1;
while error_cos >=tol
cos = (-1)^n * A^(2*n)/factorial(2*n);
CN = CN + cos;
cosplus1 = (-1)^(n+1) * A^(2*(n+1))/factorial(2*(n+1));
CM = CM + cosplus1;
error= abs( max(CM - CN));
n
= n+1;
end
end
again I get wrong result for cos. I cannot find my mistake.
I have simplified your code a bit. Following will work
[q, w] = sintaylorfunction(pi/2, 0.01)
function [SN CN] = sintaylorfunction(A, tol)
k = 0;
error = inf;
SN = 0;
while error >= tol
sin = (-1)^k * A^(2*k+1)/factorial(2*k+1);
SN = SN + sin;
sinplus1 = (-1)^(k+1) * A^(2*(k+1)+1)/factorial(2*(k+1)+1);
error= abs(sinplus1-sin);
k = k+1;
end
n=0;
error_cos=inf;
CN=0;
while error_cos >=tol
cos = (-1)^n * A^(2*n)/factorial(2*n);
CN = CN + cos;
cosplus1 = (-1)^(n+1) * A^(2*(n+1))/factorial(2*(n+1));
error_cos= abs(cosplus1-cos);
n = n+1;
end
end
many thanks dear Hamza. I see my missings.
I am glad to be of help!

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

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 11월 3일
편집: Steven Lord 2020년 11월 3일

0 개 추천

I want to walk through your code and comment on a few places.
function [SN] = sintaylorfunction(A, tol)
k = 0;
error = 0 ;
Don't create a variable named error. That identifier already has a meaning in MATLAB.
sin = 0;
The identifier sin also already has a meaning in MATLAB, so I recommend you choose a different variable name.
SN = sin;
sinplus1 = 0;
SM = sinplus1;
while E <= tol
The variable E doesn't exist.
sin = (-1)^k * A^(2*k+1)/factorial(2*k+1);
From the role this plays, a better name might be term.
SN = SN + sin;
sinplus1 = (-1)^(k+1) * A^(2*(k+1)+1)/factorial(2*(k+1)+1);
SM = SM + sinplus1;
error= abs( max(SM - SN));
In this loop you change neither the (nonexistent) variable E nor the variable tol. So if this were to enter the loop, you'd never exit.
FYI, you might want to check your answer using the funm function to compute the matrix cosine and matrix sine.

카테고리

도움말 센터File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

질문:

2020년 11월 3일

댓글:

2020년 11월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by