Linear equations

This is a discussion on Linear equations within the C Programming forums, part of the General Programming Boards category; Has anyone wrote a program solve this f(x) = sin (2x/5) -x +1 =0 with bisection method and newtons method? ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Angry Linear equations

    Has anyone wrote a program solve this f(x) = sin (2x/5) -x +1 =0
    with bisection method and newtons method?

    I haven't a clue and any help would be appreciated.

    Thanks
    Amanda

    double left = 0 ,right = 0;
    double size, mid,root, tol = .00001, zero_check = .0001;
    int done = 0;
    float finish;


    printf("Please enter a range of values ");
    scanf ("%lf ", &left);
    scanf ("%lf", &right);

    size = right - left;

    while((size >=tol) && (!done))
    {
    mid = (right+left)/2;
    printf("f(%f) = %9.6f f(%f) = %9.6f f(%f) = %9.6f\n",
    left, f(left),mid,f(mid),right,f(right));

    if(fabs(f(mid)) <zero_check)
    done = 1;
    else if (f(mid) * f(left)<0)
    right = mid;
    else
    left = mid;
    size = right - left;
    }

    if(size >tol)
    root = mid;
    else
    root = (left +right)/2;
    printf("The root is %lf\n", root);


    }
    double f(double x)
    {
    return(x*x -4 +exp(-1.0*x));
    }

    I want to know why do you use the function double f. Can you not just check the values without applying them to this function?And when you take the right from the left what is that value called and when do you compare it the tol?
    Last edited by amandad40; 10-24-2002 at 03:42 AM.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    I've written a few solver routines, using both methods. But I'm not sure what the problem is... you don't know either of those methods is? You don't know how to take the derivative for Newton's method? Try posting what you've written so far, even if it's pseudocode. (Just use code tags when doing so..)

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Linear equations

    Sorry about this i dont really know how to use this message board so I am going to write the next part of my question in here.
    Code:
    double left = 0 ,right = 0;
    double size, mid,root, tol = .00001, zero_check = .0001;
    int done = 0;
    float finish;
    
    
    printf("Please enter a range of values ");
    scanf ("%lf ", &left);
    scanf ("%lf", &right);
    
    size = right - left;
    
    while((size >=tol) && (!done))
    {
    mid = (right+left)/2;
    printf("f(%f) = %9.6f f(%f) = %9.6f f(%f) = %9.6f\n", 
    left, f(left),mid,f(mid),right,f(right));
    
    if(fabs(f(mid)) <zero_check)
    done = 1;
    else if (f(mid) * f(left)<0)
    right = mid;
    else
    left = mid;
    size = right - left;
    }
    
    if(size >tol)
    root = mid;
    else
    root = (left +right)/2;
    printf("The root is %lf\n", root);
    
    
    }
    double f(double x)
    {
    return(x*x -4 +exp(-1.0*x));
    }
    I want to know why do you use the function double f. Can you not just check the values without applying them to this function?And when you take the right from the left what is that value called and when do you compare it the tol?

    T

    &#91;code]&#91;/code]tagged by Salem

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Angry Linear equations

    I want to know though why do you use these values in the function. Why subtitute your values into the function. What does that do to your values and is it nessecary to do this. Is that function f a generic function that is used with all linear equation problems? Can you not just use your values raw and not put them through the function?

    Thanks
    Amanda

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    What Salem said, it's a lot easier to use a function than to "use the values raw" as you put it.

    Code:
    double f(double x){
    	return(sin(2*x/5)-x+1);
    }
    Or whatever the function is you're trying to solve. I should also make some other points:
    • Bisection only works if the function changes sign over the input interval. Check that f(upper)*f(lower) < 0 after inputting the interval bounds.
    • the variable "finish" is never used.
    • that the routine does not allow roots < 0. Try:
      size = fabs(right-left);
    • that neither this equation nor the one in the other program are linear equations.
    Last edited by MPC_Engr; 10-24-2002 at 12:19 PM.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Linear equations

    Originally posted by amandad40
    I want to know though why do you use these values in the function. Why subtitute your values into the function. What does that do to your values and is it nessecary to do this. Is that function f a generic function that is used with all linear equation problems? Can you not just use your values raw and not put them through the function?

    Thanks
    Amanda
    It is a lot easier to have that function (equation) in one place, especially if you use it in several places.
    Otherwise, you have to modify your code in several places if you want to change the equation.

    It's also a better coding style to not print the same code in several places.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read coefficients of linear equations from file into 2d array
    By omaralqady in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2009, 07:39 AM
  2. solution to a linear system of equations
    By e4321 in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2009, 08:00 PM
  3. LINEAR EQUATIONS please help
    By apple_ranger in forum C Programming
    Replies: 18
    Last Post: 09-08-2008, 05:49 AM
  4. Simultaneous linear Equations
    By dvd4alll in forum C# Programming
    Replies: 8
    Last Post: 02-08-2008, 06:11 PM
  5. Solving linear equations
    By PJYelton in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21