The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 12:11:40 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Can any one help me with this c++ programming question?  (Read 4513 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

No links in signatures please
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



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

hai
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


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

V, based on the sig links, i would say no... LOL
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



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

Well kerpowed, Nuts.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


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

though I give points for having an ontopic post.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


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
*****
Offline Offline

Posts: 2203


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!