Numerical Analysis in C: Solving a sum

Click For Summary

Homework Help Overview

The problem involves evaluating the sum Ʃ f(0.01k) from k=1 to k=100000, where the function f(k) is defined as the square root of the minimum of (1,k) multiplied by the exponential of the maximum of (1.5, k) squared. Participants are discussing issues related to the implementation of this sum in C programming.

Discussion Character

  • Exploratory, Problem interpretation, Assumption checking

Approaches and Questions Raised

  • Participants are examining the structure of the C code, noting that the calculation of funcResult is done outside the loop, which leads to incorrect summation. There are suggestions to redefine funcResult as a function or to compute it within the loop for each value of k. Some participants are also questioning the potential for numerical overflow in the calculations, particularly for large values of k.

Discussion Status

The discussion is active, with participants providing insights into coding practices and potential pitfalls in the implementation. Some have reported achieving non-zero results after addressing the issues raised, while others are still exploring the implications of their coding choices and the mathematical setup.

Contextual Notes

There are concerns regarding the initialization of variables and the impact of uninitialized values on the calculations. Additionally, the possibility of numerical overflow is being discussed, particularly in relation to the exponential growth of the function for large k values.

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
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 50 ·
2
Replies
50
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K