Hiya gang. I am currently in need of help (again! Bollocks!). Currently, i am unsure of where to start because, well, quite simply, I am not that smart. The problem is as follows:

Develop a program that will solve the transcendental equation x = cos(2x) to within +/- .0001 radians. Use whichever type of loop you want to. THE TRICKY PART is that even through there is only one answer, you may need to try some different approaches to find it.

So my question, is huh?

What would be the most logical way to approach this. I'm not hip on the trig aspect as it's been about 13 years since i've taken a trig class. Something tells me this is a lot more complicated than just putting in an equation and running it. Frankly, I am not sure what is being asked.

Any help/suggestions would be greatly appreciated.

This is what I have so far, but I am VERY certain this does not even begin to answer the question...like at all.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TOLERANCE 0.0001

int main(void)
{
    double x, newx, delta ;
    int counter = 0 ;
    x = 0.8 ; // initial wild guess
    do
    {
        newx = cos((2)*(x)) ;
        delta = fabs(newx - x) ;
        x = newx ;
        counter++ ;
    }
    while (delta > 0.0001) ;
    printf("converged to %g\n", x) ;
    printf("number of iterations = %i\n", counter) ;
    system("PAUSE") ;
return (0) ;  
}

This is what I have so far, but I am VERY certain this does not even begin to answer the question...like at all.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define TOLERANCE 0.0001

int main(void)
{
    double x, newx, delta ;
    int counter = 0 ;
    x = 0.8 ; // initial wild guess
    do
    {
        newx = cos((2)*(x)) ;
        delta = fabs(newx - x) ;
        x = newx ;
        counter++ ;
    }
    while (delta > 0.0001) ;
    printf("converged to %g\n", x) ;
    printf("number of iterations = %i\n", counter) ;
    system("PAUSE") ;
return (0) ;  
}

Not sure what your Calculus background is, but you can use Newton's Method.

http://en.wikipedia.org/wiki/Newton%27s_method

Have a function f(x) = cos(2x) - x. You are trying to solve for f(x) = 0 within a certain tolerance, so you'll need to calculate the derivative function f'(x). You'll have a do-while loop as you have with the condition that you have, where delta is the absolute value of f(x). You'll continually calculate f(x) and f'(x) and adjust x based on those values according to Newton's Method.

Not sure what your Calculus background is, but you can use Newton's Method.

http://en.wikipedia.org/wiki/Newton%27s_method

Have a function f(x) = cos(2x) - x. You are trying to solve for f(x) = 0 within a certain tolerance, so you'll need to calculate the derivative function f'(x). You'll have a do-while loop as you have with the condition that you have, where delta is the absolute value of f(x). You'll continually calculate f(x) and f'(x) and adjust x based on those values according to Newton's Method.

Admittedly, my calculus background is essentially non-existent. I kinda get what you are saying. But how does this translate into code-ese? Do I set up the program to find the zero crossings then? According to the problem, there is only one answer....it's just a matter of finding it. Or am I still way off?

Admittedly, my calculus background is essentially non-existent. I kinda get what you are saying. But how does this translate into code-ese? Do I set up the program to find the zero crossings then? According to the problem, there is only one answer....it's just a matter of finding it. Or am I still way off?

There is one answer. Cosine has a range of -1 to 1, so you know x is in that range. Draw the function out on paper and you'll see that x has to be positive. If you draw it out a little more, you can narrow it down a little more closely to get a decent first guess. To use Newton's Method, you need to calculate the derivative of the f(x) function. Do that before coding. Set up these variables:

double x;
double f;       // stores f(x)
double fprime;  // stores fprime
double deltax;  // stores change needed to get to next x value
double delta;   // stores absolute value of f(x)
                //  for your do-while loop condition

You need to do this on paper before starting to code it. Basically fprime is the slope, or rise/run. f is the function value, or rise. deltax is run. So calculate f inside the loop, and fprime, and from that, calculate delatax. Your next value of x will be either x + delatax or x - deltax , depending on how you define everything.

Again, don't try to code it till you draw it out on paper and get a feel for it. Work the equation for deltax out on paper. You know rise (f) and you know rise divided by run (fprime), and you're calculating run (deltax) from those two. Make sure you have deltax going in the right direction so you're getting closer, not farther. You're looking at something like this:

do
{
     // calculate f from x.
     // calculate fprime from x.
     // calculate deltax from f and fprime.
     // calculate x by adding or subtracting deltax
     // to/from x.
     // delta is simply the absolute value of f
}
while (?)  // while condition is based on delta
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.