1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Numerical Analysis in C: Solving a sum

  1. Apr 17, 2012 #1
    1. The problem statement, all variables and given/known data

    Solve the following sum: Ʃ f(0.01k) from k=1 to k=100000 where f(k) = √minimum of (1,k) * exponential(maximum of (1.5, k)*maximum of (1.5,k))

    2. Relevant equations

    See above.

    3. The attempt at a solution

    /*I've written a C program to solve this problem. it compiles. However, when I run it, the answer is 0 which is incorrect. What's wrong with either my methodology or coding?*/

    #include<stdio.h>
    #include<math.h>

    main(){

    int k; /*declare k variable */
    double min; /*declare minimum variable*/
    double max; /*declare maximum variable*/
    double funcResult; /*declare function result*/
    double sum; /*declare sum result*/

    /*create minimum function*/

    if(0.01*k<1){
    min=0.01*k;
    }
    else{
    min=1;
    }

    /*create maximum function*/

    if(0.01*k>1.5){
    max=0.01*k;
    }
    else{
    max=1.5;
    }

    /*create funcResult function*/

    funcResult=sqrt(min*exp(-max*max));

    /*compute the sum */

    sum=0;

    for(k=1;k<=100000;k++){

    sum=(sum+funcResult);

    }

    printf("The answer to this sum is: %lf\n and the funcResult is %lf",sum,funcResult);

    }
     
  2. jcsd
  3. Apr 17, 2012 #2

    Mark44

    Staff: Mentor

    You are setting sum to sum + funcResult 100,000 times, but funcResult keeps the value that it was given before the loop started.

    You need to code funcResult as an actual function, not as a variable. Either that, or do the computation inside your loop.
     
  4. Apr 17, 2012 #3
    Thank you!!! I obtained non-zero answers!
     
  5. Apr 17, 2012 #4

    Mark44

    Staff: Mentor

    From your comments, you seem to believe that you are writing functions, but you're not.

    For example, there's this code:
    Code (Text):

    /*create minimum function*/
     
    if(0.01*k<1){
     min=0.01*k;
     }
     else{
     min=1;
     }
     [/quote]
    This code gets executed once, with garbage (uninitialized) values for k and min. I can't predict what min will be set to, since you haven't put a known value into k.

    The situation is the same for max and funcResult. The values for these three variables are set before the loop begins execution, so any changes to k inside the loop have no effect on min, max, or funcResult.
     
  6. Apr 17, 2012 #5

    Ray Vickson

    User Avatar
    Science Advisor
    Homework Helper

    Unless I misunderstand your f(k), it looks like this problem will involve serious numerical overflow. I think you are saying
    [tex] f(k) = \sqrt{\min(1,5) \exp(\max(1.5,k)^2)},[/tex] and you want to evaluate f up to f(100000/100) = f(1000). However, f(1000) = 0.1741612873e217148, so the last terms in your sum will be immense. Are you sure you have not mis-stated the problem?

    RGV
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Numerical Analysis in C: Solving a sum
  1. Numerical analysis (Replies: 5)

  2. Numerical analysis (Replies: 1)

  3. Numerical analysis (Replies: 2)

  4. Numerical Analysis (Replies: 3)

Loading...