Thread: Using c to solve equations

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    8

    Using c to solve equations

    Hello,

    I am trying to solve an equation using c programming. Basically I need to write a program that calulates solutions to the equation for a range of values of x, where x >0. I am new to c programming and would appreciate some help. I am trying to use arrays for the x values and a for loop so that it cycles through all the possible scenarios. I then want to print this to a file so that it can be imported to excel.

    Thanks for any help,

    Z

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
    {
    double y_value[100];
    double x_value[100];
    int n1;
    int a;
    a=0.4+(8039/25000);
    for(n1=0;n1<100;n1++)
    {
    x_value[n1]=0 + (double)n1*0.02;
    y_value[n1]=function(x_value[n1];
    }
    for(n1=0;n1<10;n1++)
    {
    printf("%g\t %g\n",x_value[n1],y_value[n1]);
    }
    return 0;
    }


    Is what I have so far, am unsure of how to define the function so it gets the result I want
    Last edited by zeb1d1ah; 12-03-2009 at 08:45 AM. Reason: Adding code

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Without knowing what your function 'function' is actually supposed to do, it's hard to really tell you where to go.
    Code:
    double function( double x )
    {
        ...do stuff...
    
        return somedouble;
    }
    You get to fill in the 'do stuff' part.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Please indent. Also you are missing a paren for your function call. So what are you expecting this function to do against your x_value[] argument you are passing in? What are your requirments. Can you explain a bit more on what it is you are trying to achieve with this function?

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    insert
    Code:
    int main()
    {
        double y_value[100];
        double x_value[100];
        int n1;
        int a;
        File*excel data;
        
        a=0.4+(8039/25000);
        for(n1=0;n1<100;n1++)
        {
            x_value[n1]=0 + (double)n1*0.02;
            y_value[n1]=function(x_value[n1];
        }
        for(n1=0;n1<10;n1++)
        {
            printf("%g\t %g\n",x_value[n1],y_value[n1]);
        }
        return 0;
    }
    Thanks for getting back to beThe function is an equation based on the constant 'a' which I defined and various values of x. I think it should be something like this:

    function=sin(pow(x_value[n1],a)-pow(x_value[n1],(1/a))+(a*x_value[n1]));

    Any more help appreciated, have tried to indent this time, sorry!

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You will need to include the math.h file. Then read what quzah mentioned about how to implement your function. Since you have your algorithm in place assign its value to a variable and just return its value, which happens to be a double.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    double result(double );
    int main()
    {
        double y_value[100];
        double x_value[100];
        int n1;
        int a;
    
        a=0.4+(8039/25000);
    
        for(n1=0;n1<100;n1++)
        {
            x_value[n1]=0+(double)n1*0.02;
            y_value[n1]=result(double(x_value[n1]));
        }
    
        for(n1=0;n1<10;n1++)
        {
            printf("%g\t %g\n",x_value[n1],y_value[n1]);
        }
    
        double result(double x_value[n1])
        {
            double result;
            result = sin(pow(x_value[n1],a)-pow(x_value[n1],(1/a))+(a*x_value[n1]));
        }   return result;
        return 0;
    }
    Thanks for the nudge in the right direction guys, still don't have it working, am I going along the right tracks?
    Last edited by zeb1d1ah; 12-03-2009 at 10:33 AM. Reason: change code

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Place your result() function outside the main() block of code. Also prior to main() you will need to declare your result() and define its argument types.


    Code:
    #includes....
    
    /*Function declarations */
    double result( double x);
    
    int main()
    {
       ...
    }
    
    /*Your custom functions outside of main() */
    
    double result(double x)
    {
       ...
      /* throw in a few printf()s here and there to test your results, good for debugging */
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double result(double );
    
    int main()
    {
        double y_value[100];
        double x_value[100];
        int n1;
        int a;
    
        a=0.4+(8039/25000);
    
        for(n1=0;n1<100;n1++)
        {
            x_value[n1]=0+(double)n1*0.02;
            y_value[n1]=result(double(x_value[n1]));
        }
    
        for(n1=0;n1<10;n1++)
        {
            printf("%g\t %g\n",x_value[n1],y_value[n1]);
        }
    
        return 0;
    }
    
    double result(double x_value[n1])
    {
        double result;
        result = sin(pow(x_value[n1],a)-pow(x_value[n1],(1/a))+(a*x_value[n1]));
        return result;
    }
    You can't have a function inside another function. I've moved the result function out from inside the main function, in this code.

    Also, the close brace - } needed to be moved to the line *after* the return, instead of being before at the start of that line of code.

    You can't return anything after the closing brace.

    Personally, I don't let variable names be the same as function names. You should be OK since you have the prototype for the function result(), above main(). Just watch your naming conventions.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    double test(double );
    int a;
    a=0.4+(8039/25000);
    int main()
    {
        double y_value[100];
        double x_value[100];
        int n1;
    
    
        for(n1=0;n1<100;n1++)
        {
            x_value[n1]=0+(double)n1*0.02;
            y_value[n1]=test(x_value[n1]);
        }
    
        for(n1=0;n1<100;n1++)
        {
            printf("%g\t %g\n",x_value[n1],y_value[n1]);
        }
    
        return 0;
    }
    
    double test(double argument1)
        {
            double result;
            result = sin(pow(argument1,a)-pow(argument1,(1/a))+(a*argument1));
            return result;
        }
    Thanks again, This works without any errors but when the printf runs it crashes the programme and doesn't display the results, sorry for being a pain but have only been doing c for a few days! This advice is helping though!

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Are you sure 'a' is supposed to be an int ?
    Code:
    int a;
    a=0.4+(8039/25000);
    I changed it to double(and 8039.0 / 25000) and it ran without any compile-time or runtime errors.

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by zeb1d1ah View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    double test(double );
    int a;
    a=0.4+(8039/25000);
    int main()
    {
        double y_value[100];
        double x_value[100];
        int n1;
    
    
        for(n1=0;n1<100;n1++)
        {
            x_value[n1]=0+(double)n1*0.02;
            y_value[n1]=test(x_value[n1]);
        }
    
        for(n1=0;n1<100;n1++)
        {
            printf("%g\t %g\n",x_value[n1],y_value[n1]);
        }
    
        return 0;
    }
    
    double test(double argument1)
        {
            double result;
            result = sin(pow(argument1,a)-pow(argument1,(1/a))+(a*argument1));
            return result;
        }
    Thanks again, This works without any errors but when the printf runs it crashes the programme and doesn't display the results, sorry for being a pain but have only been doing c for a few days! This advice is helping though!
    Look what you're doing is
    Code:
    int a;
    a=0.4+(8039/25000);
    Here 'a' will be zero coz 8039/25000 is zero(since both of them are int) and 0.4 added to 0 will be 0.4 but since you're declaring a as an int thus a=integer part of 0.4=0. Now in test function you're doing 1/a which is division by zero.
    The solution would be to declare a as float and do 8039.0/25000.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    8

    Unhappy

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    double test(double );
    double a;
    a=0.4+(8039.0/25000);
    int main()
    {
        double y_value[100];
        double x_value[100];
        int n1;
    
    
        for(n1=0;n1<100;n1++)
        {
            x_value[n1]=0+(double)n1*0.02;
            y_value[n1]=test(x_value[n1]);
        }
    
        for(n1=0;n1<100;n1++)
        {
            printf("%g\t %g\n",x_value[n1],y_value[n1]);
        }
    
        return 0;
    }
    
    double test(double argument1)
        {
            double result;
            result = sin(pow(argument1,a)-pow(argument1,(1/a))+(a*argument1));
            return result;
        }
    Guys, thanks for the feedback and advice, I still can't get this to do what I want. I am trying to produce a list of 'x_values' and the corresponding answer from 'result = ' in a table, so that I can produce a text file and transfer to excel.

    Any more guidance greatly appreciated as I am a bit stumped!

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why not drop the size of the arrays (x_value & y_value), down to like 2, and then step through it with several printf() statements added if needed, and see where the numbers go flooey?

    As small a program as this is, the problem can barely run, let alone hide.
    Last edited by Adak; 12-05-2009 at 06:50 PM.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can't put the assignment to 'a' outside of a function like that.
    If you write it as:
    Code:
    const double a = ....;
    it will then be evaulated during compilation and is allowed to be anywhere.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    I finally got this working, many thanks to those who contributed! My understanding of c is starting to increase!

    Thanks,

    Z

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. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. LUP Decomposition (simultaneous equations)
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 02:08 AM
  4. Solving linear equations
    By PJYelton in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2002, 06:00 PM
  5. equations for a program
    By anthonye in forum C Programming
    Replies: 4
    Last Post: 06-19-2002, 04:38 AM