Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cosinus with Taylor expansion
12-31-2009, 08:54 AM (This post was last modified: 12-31-2009 08:56 AM by Alireza_M.)
Post: #1
Cosinus with Taylor expansion
this program will calculate cosinus of a favorite degree with favorite Taylor expansion numbers involved,and will compare it with the original cosinus
function,heres the code(Note:the cosinus has been calculated around zero,this is popular as McLauren expansion):
Code:
Program Cosinus_Vs_TaylorExpansion(Input,Output);
  (*Programmer:Alireza Mahallati Programmer S-T,Number:8811021 Date:7,9,1388
    This program will input a degree and will calculate cosinus(for radians)
    with taylor expansion and will compare the result with the Cosinus
    Function which has been written for Pascal*)
  Uses CRT;
  Var
    Difference,CosinusFunction,Taylor,Degree,Radians,Part:Real;
    Denominator,Sign,Counter,Sentences:longint;
Begin
  CLRSCR;
  Textcolor(Yellow);
  Writeln('Which degree You want to calculate Cosinus for?');
  Readln(Degree);
  Writeln('How many sentences of Taylor expansion you want to involve?');
  Readln(Sentences);
  Radians:=(4*ArcTan(1))*(Degree)/180;
  Denominator:=1;
  If Sentences=1 Then
    Begin
      Writeln('according to Taylor expa`,Cos( ',Radians:16:14,') is -> 1');
      Readln;
      Halt;
    End
  Else
  Part:=1;
  Taylor:=1;
  Sign:=(-1);
  For Counter:=2 To Sentences Do
    Begin
      Denominator:=((2*Counter)-3)*((2*Counter)-2)*Denominator;
      Part:=(Radians*Radians)*Part;
      Taylor:=Taylor+((Sign)*(Part))/Denominator;
      Sign:=Sign*(-1);
    End;
  CosinusFunction:=Cos(Radians);
  Writeln('----------------');
  Textcolor(Green);
  Writeln;
  Writeln('Cosinus of ',Radians:6:4,' Radians is -> ',Taylor:16:14);
  Writeln;
  Writeln('Cosinus of ',Radians:6:4,' Radians by Cosinus Function is -> '
  ,CosinusFunction:16:14);
  Difference:=ABS(CosinusFunction-Taylor);
  Writeln;
  TextColor(Brown);
  Writeln('Error of Taylor expansion in calculating Cosinus of ',Radians:6:4,' Radians is '
  ,Difference:16:14);
  Writeln;
  Textcolor(White);
  Delay(1500);
  Writeln('CODED BY ALIREZA_M');
  Repeat
    Begin
      Delay(100);
    End;
  Until Keypressed;
End.

Gholamreza Takhti
who was he? find out
Find all posts by this user
Quote this message in a reply
12-31-2009, 08:58 AM
Post: #2
RE: Cosinus with Taylor expansion
Very good and mathematically correct. For this you need to know maths too !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: