Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Taylor Series in C
New Posts  All Forums:Forum Nav:

# Programming Taylor Series in C

So the overall goal is to approximate sin(x) using the Taylor Series formula

sin(x) = x - ((x^3)/(3!)) + ((x^5)/(5!)) - ((x^7)/(7!)) + .... ((x^n)/(n!))

The plan is to get an input angle(x) in degrees, and the desired maximum error.

The error cant be greater than (|x|^(n+2)) / (n+2)! so I'm figuring I'm going to have to use a do-while loop to figure out the number of terms(n) to correctly calculate within that desired error.

However, I'm not really sure how I am going to code in the addition/subtraction of the following term. Like I would be able to do it if I was adding every term, but I'm not quite sure how to make a loop where the following term has the opposite sign of the previous.

If anyone could help me out on just this part that would be cool beans.
 Ok (13 items)
CPUMotherboardGraphicsRAM
i7 920 D0 @ 4.2 ASUS P6T Deluxe V2 9800 GT =( Mushkin Enhanced Redline 6GB
Hard DriveOptical DriveOSMonitor
2x-Intel X25-M 80GB SSD RAID0 1tb sinpoint f3 Samsung Writemaster Windows 7 64bit Syncmaster 24"
KeyboardPowerCaseMouse
G11 BFG Tech EX-1200W Antec 1200 G5
QCK+
 Ok (13 items)
CPUMotherboardGraphicsRAM
i7 920 D0 @ 4.2 ASUS P6T Deluxe V2 9800 GT =( Mushkin Enhanced Redline 6GB
Hard DriveOptical DriveOSMonitor
2x-Intel X25-M 80GB SSD RAID0 1tb sinpoint f3 Samsung Writemaster Windows 7 64bit Syncmaster 24"
KeyboardPowerCaseMouse
G11 BFG Tech EX-1200W Antec 1200 G5
QCK+
let me start by saying i'm not very familiar with c. so in terms of general programming, you could reverse the sign of every other term by multiplying it by -1. you could use a boolean and do something like this:

Code:
``````bool flip_sign = false;

while(loop_variable){
if(flip_sign){
f(x) = f(x) * (-1);
flip_sign = false;
}
else{
flip_sign = true;
}
//modify f(x) and move to next term in series
}``````
hope that's useful for you
 Fractal Design (15 items) 775 4 life (15 items) Lenovo Thinkpad T410 Laptop (9 items)
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5
Mouse
Razor Abyssus
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ...
Power
9-cell plus Slice battery
 Fractal Design (15 items) 775 4 life (15 items) Lenovo Thinkpad T410 Laptop (9 items)
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5
Mouse
Razor Abyssus
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ...
Power
9-cell plus Slice battery
Quote:
 Originally Posted by danman00 So the overall goal is to approximate sin(x) using the Taylor Series formula sin(x) = x - ((x^3)/(3!)) + ((x^5)/(5!)) - ((x^7)/(7!)) + .... ((x^n)/(n!)) The plan is to get an input angle(x) in degrees, and the desired maximum error. The error cant be greater than (|x|^(n+2)) / (n+2)! so I'm figuring I'm going to have to use a do-while loop to figure out the number of terms(n) to correctly calculate within that desired error. However, I'm not really sure how I am going to code in the addition/subtraction of the following term. Like I would be able to do it if I was adding every term, but I'm not quite sure how to make a loop where the following term has the opposite sign of the previous. If anyone could help me out on just this part that would be cool beans.

you could easily set this up in a for loop.

if i%2 == 1
term *= -1
Quote:
 Originally Posted by B-80 if i%2 == 1 term *= -1
thats a much better solution than mine. i need to stop answering questions late at night
 Fractal Design (15 items) 775 4 life (15 items) Lenovo Thinkpad T410 Laptop (9 items)
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5
Mouse
Razor Abyssus
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ...
Power
9-cell plus Slice battery
 Fractal Design (15 items) 775 4 life (15 items) Lenovo Thinkpad T410 Laptop (9 items)
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5
Mouse
Razor Abyssus
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ...
Power
9-cell plus Slice battery
Quote:
 Originally Posted by travesty thats a much better solution than mine. i need to stop answering questions late at night
haha, I noticed how convoluted that was. I was like, this kid is a computer scientist at heart
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Coding and Programming
• Programming Taylor Series in C
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Taylor Series in C