Thread: Bisection with typedef declaration

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    8

    Bisection with typedef declaration

    Hi All,

    I'm working on this C programming assignment, which requires the using of bisection method to find roots of a function. However, I only have a dim idea of how the structure should be. Like how should I declare the function, where should I place the declaration, and if im looking for the root of function, lets say f(x)=x^2+e^x, how do i set it as an argument? *f?

    I try to browse around for an example of this, but couldn't find any helpful ones.

    Any help will be awesome!

    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not sure where typedef fits into all of this. This is basically an assignment about algorithm implementation. It seems a somewhat advanced problem to tackle if you don't even have the basics of C programming down (i.e. function declaration, argument passing, etc).

    Presumably you've been going to your lectures and doing previous assignments, so you should have lecture notes, a textbook, and some old assignments for reference on general C programming. For more C tutorials, check here: Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy. For an explanation of the algorithm, along with some pseudo code, check here: Bisection method - Wikipedia, the free encyclopedia.

    When you've attempted a solution, come back and show us the work you did (using code tags) and let us know what specific problems you're having. Then we can better help you.

  3. #3
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by idecline View Post
    Hi All,

    I'm working on this C programming assignment, which requires the using of bisection method to find roots of a function. However, I only have a dim idea of how the structure should be. Like how should I declare the function, where should I place the declaration, and if im looking for the root of function, lets say f(x)=x^2+e^x, how do i set it as an argument? *f?

    I try to browse around for an example of this, but couldn't find any helpful ones.

    Any help will be awesome!

    Thanks
    Have you tried writing any C code so far? What have you tried?

    If not, check out some useful links here:

    Pseudocode Examples


    Root finding methods

    or even here?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by Char*Pntr View Post
    Have you tried writing any C code so far? What have you tried?

    If not, check out some useful links here:

    Pseudocode Examples


    Root finding methods

    or even here?

    Yes, I thought I was pretty ok with C before the pointer and rootfinding methods were introduced, here s what I have done with C, which is to simulate a hockey card collection:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    /*a function that checks the duplicate cards in one  deck (for bonus question)*/
    int dupcheck(int random_deck[], int i)
    {
    	int j;
    	for(j=0;j<i;++j)
    	{
    		if(random_deck[i]== random_deck[j])
    		{
    			return (1);
    		}
    	}
    	return(0);
    }
    /* a function that keeps track of how many identicals cards you have*/
    int checkcards(int cardsinhand[], int temp, int numcards)
    .
    .
    .
    .
    .
    	mindeck=Tdeckpurchased[0];
    	printf("Max is %d, Min is %d, Ave is %d\n", maxdeck,mindeck,avedeck);
    	printf("Ave of Max number of duplicate card is %d\n", avedup);
    
    	return (0);
    
    }
    Last edited by idecline; 11-20-2010 at 04:12 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by anduril462 View Post
    I'm not sure where typedef fits into all of this. This is basically an assignment about algorithm implementation. It seems a somewhat advanced problem to tackle if you don't even have the basics of C programming down (i.e. function declaration, argument passing, etc).

    Presumably you've been going to your lectures and doing previous assignments, so you should have lecture notes, a textbook, and some old assignments for reference on general C programming. For more C tutorials, check here: Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy. For an explanation of the algorithm, along with some pseudo code, check here: Bisection method - Wikipedia, the free encyclopedia.

    When you've attempted a solution, come back and show us the work you did (using code tags) and let us know what specific problems you're having. Then we can better help you.
    Thanks for the links,
    but I learn better with examples,
    like how the function f(x) should be placed,
    and how in what form should i take the function into root finding function?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think Char*Pntr was asking if you have written any code in an attempt to solve your root finding problem. I'm guessing no, but based on your card program, you at least understand basic flow control, loops, functions and arrays, which is good.

    At the very least, you will need a function that calculates f(x); call it f and have it take the parameter x. You will also need a function that finds the root, call it find_root, which will need parameters a and b. If you need to, you can pass in the function f using function pointers (Function Pointers in C and C++ - Cprogramming.com), but I'm not sure you need to do that. It would help if we had the full assignment description.

    The wikipedia article I provided has a nice, clear pseudo-code example for you to imitate in C. They practically gave you the solution there, so just translate that pseudo code to C. You really need to take a stab at writing this program before you get any more help. Come back and post your code when you've done that, and we'll see what we can do.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Quote Originally Posted by anduril462 View Post
    I think Char*Pntr was asking if you have written any code in an attempt to solve your root finding problem. I'm guessing no, but based on your card program, you at least understand basic flow control, loops, functions and arrays, which is good.

    At the very least, you will need a function that calculates f(x); call it f and have it take the parameter x. You will also need a function that finds the root, call it find_root, which will need parameters a and b. If you need to, you can pass in the function f using function pointers (Function Pointers in C and C++ - Cprogramming.com), but I'm not sure you need to do that. It would help if we had the full assignment description.

    The wikipedia article I provided has a nice, clear pseudo-code example for you to imitate in C. They practically gave you the solution there, so just translate that pseudo code to C. You really need to take a stab at writing this program before you get any more help. Come back and post your code when you've done that, and we'll see what we can do.
    I sat in front of the computer for the whole day, but didnt know how to start. I wrote some codes down on the paper, but did not seem to work at all...
    Upload the assignment is kinda cheating? I still wanna try it out myself, so I just try to understand the parts im confused at.

    and yes, i think a function for calculating the f(x), and a function find_root are what I need. But the equation I have actually has more variables that are to be defined by users, the equation is :
    f(T)=T/w*cosh(wx/T) + ymin - T/w - y

    where all values will be given by user except T.

    I'll try to do my best and post it up, but...
    should I write a function "to calculate f(x)" first? like : f(x,y,ymin,w){...}
    and use the bisection to find the root in this same function?

    THANKS
    Last edited by idecline; 11-20-2010 at 04:18 PM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I sat in front of the computer for the whole day, but didnt know how to start. I wrote some codes down on the paper, but did not seem to work at all...
    We've all been there before. It really helps if you break your problem up into smaller pieces. Start with writing your input routines, and test that. Then write your f(x) function and test that against some hand calculations or known values to make sure you're still on track. Then write your find_root function. You could even break that up into smaller pieces, like calculate the error and midpoint of a and b, then evaluate f(x) for a, b and midpoint, printing out the values as you go. Verify this against hand calculations or known results and make sure you're on the right track. Then decide whether to pick the left half of your range (a, midpoint) or right half (midpoint, b) to discard, and set your new a or b accordingly, again printing out your new values to make sure you're on track. Then put it all in a loop and see if it converges.

    Upload the assignment is kinda cheating?
    You should check with your professor first. In my personal opinion, if you have made a serious effort and done most of the assignment yourself, then coming here for help is no different than discussing your problems with a classmate, TA or professor, and asking them to help you debug. We can help you sort out syntax and logic errors, general design questions, and just about any other pertinent question you have, we just can't write the code for you.

    should I write a function "to calculate f(x)" first? like : f(x,y,ymin,w){...}
    Yes, then test it with a few values for which you know the f(T) and make sure it's correct.

    and use the bisection to find the root in this same function?
    No. You don't want to call the find_root function from within f, you want to call f from the find_root function.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    I did the best I can and stuck at the scanf part.
    I thought float and double share the %f, but when i ran the test, it told me:
    'warning: float format, double arg'

    and I tried to use typedef to include my f() as an argument, but seems like a failure.

    Help PLEASE,
    Thank you

    Code:
    #include <math.h>
    #include <stdio.h>
    
    double f(double T0, double x, double y, double ymin, double w)
    {
    	return ( (T0 / w) * cosh(w * x / T0) + ymin - T0 / w - y);
    }
    
    typedef double (*DfD) (double);
    double find_root(DfD f, double Ta, double Tb, double tol)
    {
    	double middle = (Ta + Tb)/2.0;
    	if ((middle - Ta)< tol)
    		return middle;
    	else if (f(middle,x,y,ymin,w)* f(Ta,x,y,ymin,w)<0.0)
    		return find_root(f,Ta,middle,tol);
    	else
    		return find_root(f,middle,Tb,tol);
    }
    
    int main(void)
    {
    	double x, y, ymin, w;
    	double newy;
    	scanf("%f%f%f%f",&x,&y,&ymin,&w);
    	newy = &find_root;
    	return(0);
    }
    Last edited by idecline; 11-20-2010 at 06:18 PM.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    You should check with your professor first. In my personal opinion, if you have made a serious effort and done most of the assignment yourself, then coming here for help is no different than discussing your problems with a classmate, TA or professor, and asking them to help you debug. We can help you sort out syntax and logic errors, general design questions, and just about any other pertinent question you have, we just can't write the code for you.
    Or maybe just to help you to understand what im trying to do.
    I have it attached.
    Attachment 10179

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Made some correction, hope this is better than what i had ?
    Code:
    #include <math.h>
    #include <stdio.h>
    
    
    double x, y, ymin, w, T0;
    
    double f(double T0, double x, double y, double ymin, double w)
    {
    	return ( (T0 / w) * cosh(w * x / T0) + ymin - T0 / w - y);
    }
    
    typedef double (*DfD) (double);
    double find_root(DfD f, double Ta, double Tb, double tol)
    {
    	double middle = (Ta + Tb)/2.0;
    	if ((middle - Ta)< tol)
    		return middle;
    	else if (f(middle)* f(Ta)<0.0)
    		return find_root(f,Ta,middle,tol);
    	else
    		return find_root(f,middle,Tb,tol);
    }
    
    int main(void)
    {
    	scanf("%f%f%f%f",&x,&y,&ymin,&w);
    	f(T0,x,y,ymin,w);
    	return(0);
    }

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    8
    Thank you guys,
    I finally figure it out and got it done!
    THANKS FOR THE PUSH!!!
    YOU GUYS ARE AWESOME....

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Glad to hear, and you're very welcome!

  14. #14
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Quote Originally Posted by anduril462 View Post
    Well I quess we all learned something here! I particularly enjoyed anduril462's link.
    Bookmarked. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

Tags for this Thread