Numerical Analysis in C: Solving a sum

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
chill_factor
Messages
898
Reaction score
5

Homework Statement



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))

Homework Equations



See above.

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);

}
 
Physics news on Phys.org
chill_factor said:

Homework Statement



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))

Homework Equations



See above.

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);

}

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.
 
Mark44 said:
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.

Thank you! I obtained non-zero answers!
 
From your comments, you seem to believe that you are writing functions, but you're not.

For example, there's this code:
Code:
/*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.
 
chill_factor said:

Homework Statement



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))

Homework Equations



See above.

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);

}

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