The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 03, 2010, 05:40:58 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Can any one help me with this c++ programming question?  (Read 1638 times)
Angelica Jolly
n00b
*
Offline Offline

Posts: 1



View Profile
« on: May 13, 2009, 12:16:24 AM »

Hi,

Can anyone write a c++ programme which allows the user to solve the following non-linear equation:

f(x) = sin(2 x / 5) - x + 1 = 0

to an accuracy of 10^(-5) using both the Bisection Method and the Newton-Raphson method.
The user has to input the appropriate initial estimates to the actual root.

Thanks For The Help!
Logged

Kerpow Says the nutballs!
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1598



View Profile
« Reply #1 on: May 13, 2009, 06:58:14 AM »

Are you taking a programming course?

 ROFLMAO

P.S. Don't forget to include the Fourier transform of the above!
Logged

nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5326


DAMN YOU!!!!!!!


View Profile
« Reply #2 on: May 13, 2009, 09:05:28 AM »

V, based on the sig links, i would say no... LOL
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 8744



View Profile
« Reply #3 on: May 13, 2009, 09:15:11 AM »

Well kerpowed, Nuts.
Logged

It's the things you learn after you know it all that really count.
-John Wooden
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5326


DAMN YOU!!!!!!!


View Profile
« Reply #4 on: May 13, 2009, 11:05:53 AM »

though I give points for having an ontopic post.
Logged
nop_90
Global Moderator
Lifer
*****
Online Online

Posts: 1899


View Profile
« Reply #5 on: May 13, 2009, 03:08:21 PM »

import math
def f(x) :
    return math.sin(2.0 * x / 5.0) - x + 1

def df(x) :
    return (2.0/5.0) * math.cos(2.0 * x / 5.0) - 1

def newton(x1) :
    return x1 - (f(x1)/df(x1))

def solve(x1,delta = math.pow(10,-5)) :
    while  delta < abs(f(x1) - 0) :
        x2 = newton(x1)
        print x1,x2,f(x2)
        x1 = x2
    return x1

there is ur answer in python
then go and download cython Smiley
run the python thru cython to make in C code Cheesy
then submit that to ur teacher Cheesy
Logged
nop_90
Global Moderator
Lifer
*****
Online Online

Posts: 1899


View Profile
« Reply #6 on: May 13, 2009, 03:16:02 PM »

Actually a better way to solve in python is using generators

import math
def f(x) :
    return math.sin(2.0 * x / 5.0) - x + 1

def df(x) :
    return (2.0/5.0) * math.cos(2.0 * x / 5.0) - 1

def newton(x1) :
    while 1 :
        x2 = x1 - (f(x1)/df(x1))
        yield x2
        x1 = x2

def solve(x1,delta = math.pow(10,-5)) :
    generator = newton(x1)
    guess = x1
    while  delta < abs(f(guess) - 0) :
        guess = generator.next()
        print guess,f(guess)
    return guess

Anyway for C/C++ take the previous version I give u in python, convert it to C++, should be a 10 minute exercise
Anyway enuff fun while I drink my morning coffee Cheesy
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!