CODE HELP!!

qq


Last edited on
Sounds to me like you have a pretty clear specification.
We are not going to write the program for you. What exactly is it that you have problem with?
im only allowed to use cmath , im not sure of how to write ln and cos functions in c++, this is how far i've worked



#include<iostream>
#include<cmath>
double func(double&)
double diriv(double&)
double const c=0.00005;
double const double e = 2.718281828;
double const pi= 3.14159;

int main()
{ int count=0;
double x2,x1;
cout<<"Enter the first approximation: ";
cin>>x1;

x2=x1-(func(x1)/diriv(x1));
while(x2-x1>c)
{
x2=x1-(func(x1)/diriv(x1));
count++;
}

}

double func(double&)
{
double f,pi,e,x1;
f=ln(pow(x1,2)+1)-(pow(e,0.4*x1)*cos(pi*x1);

}

double diriv(double&)
{
double d,e,pi,x1;
d= (2*x1/(pow(x1,2)+1))-(0.4*pow(e,0.4*x1)*cos(pi*x1))-(pi*sin(pi*x1)*pow(e,0.4*x1));

}
Last edited on
ln and cos functions are available in <cmath>. Note that the function log() is actually the natural logarithm (ln). log10() is the log-base-10 function.
http://cplusplus.com/reference/cmath/cos/
http://cplusplus.com/reference/cmath/log/

I noticed you also used pow(e,0.4*x1). A more efficient and common function for this is exp(0.4*x1)
http://cplusplus.com/reference/cmath/exp/

Another thing is that you use pow(x1,2). The pow function isn't very efficient and it takes much less time to compute x1*x1. It's a good idea to use that instead when possible (and it saves some typing!).
Last edited on
Also, you probably meant to pass x1 to func() and deriv(), not make it a local variable (then use it uninitialized - and e and pi).

Very sloppy.
Here's a recursive one. Just give it a function pointer to the function you want to evaluate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>

typedef double (*func)(double); // func is now a pointer to a double f(double) function

// Returns f'(x) given a pointer to f, and value x.
double fprime( func f , double x)
{
    const double dx = 0.000000001; // Theoretically, this should be infinitely close to 0
    return ( f(x + dx) - f(x - dx) ) / (2*dx);
}

// Solves f(x) = 0 for x, 
// Provide: function pointer f, a guess (x0), and the required precision.
double newtonRaphson( func f , double x0, double precision)
{
    double x1 = x0 - f( x0 ) / fprime( f, x0 ); // <-- Newton's method right here!
    
    if ( abs(x1 - x0) < precision ) 
        return x1;
        
    return newtonRaphson( f , x1 , precision ); // Recursion baby!
}

int main()
{
    std::cout << newtonRaphson( cos , 1.6 , 0.0001) << std::endl;
    std::cout << newtonRaphson( log , 1.5 , 0.0001) << std::endl;
    return 0;
}


























1.5708
1.0000


This gave me 1.5708 when evaluating cos(x) with a guess of 1.6. The real answer is pi/2 which is 1.5707963267948966.... We're within the precision required!

With the exception of our test functions cos and log, the only function from <cmath> that I used was double abs(double); which certainly isn't hard to make yourself.

So yeah, you can just use newtonRaphson() above and use it to evaluate any other functions you may be interested in.

Edit: Added some comments, and typedef'd double (*f)(double).
Last edited on
If you wanted to test your function:
1
2
3
4
5
6
double func(double&)
{
double f,pi,e,x1;
f=ln(pow(x1,2)+1)-(pow(e,0.4*x1)*cos(pi*x1);

}


I would re-write it like this:
1
2
3
4
5
6
double const pi= 3.14159265359;

double testFunc(double x)
{
    return log(x*x + 1) - exp(0.4*x)*cos(pi*x);
}


Then just plug it into the code above:
1
2
3
4
5
int main()
{
    std::cout << newtonRaphson( testFunc , 1.6 , 0.00005) << std::endl;
    return 0;
}


1.74474
Last edited on
how do i use x1 unutilized ??
X1 unutilized? I'm not sure what unutilized is. Do you mean uninitialized?

Here are the problems with your function:
double func(double&)
You don't specify a name for the double that comes in as an argument. I assume you want double& x1.

double f,pi,e,x1;
You had defined pi and e in the global scope already. You don't need to define them again. x1 is defined here, but should have been defined in the first line.

f=ln(pow(x1,2)+1)-(pow(e,0.4*x1)*cos(pi*x1);
While you've defined all of these variables, pi, e, and x1 aren't initialized. If you remove the pi and e definitions, then it will use the global scope variables which are good. x1 should have been in the first line. f is ok

}
You don't return a value. try putting return f; before this line.\


One more note, it's also not a good idea to delete your posts. You were probably brought to this forum from a google search when you were searching for answers. If you delete your OP, then no one else can see what we've suggested.
Last edited on
thankk youuu !! i fixed everything you pointed out but its still not working ! i think my problem is in the loops

this is why i had to delete it.. my instructor has a very strict policy when it comes to "asking someone else for help with your assignments".
Topic archived. No new replies allowed.