Numerical Analysis in C: Solving a sum

In summary, the conversation is about solving a sum using a C program, but the answer obtained is incorrect. This is due to the incorrect coding, where functions are mistakenly written as variables, resulting in incorrect values. Additionally, there may be a problem with the problem itself, as it may involve numerical overflow.
  • #1
chill_factor
903
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
  • #2
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.
 
  • #3
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!
 
  • #4
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.
 
  • #5
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
 

1. What is Numerical Analysis in C?

Numerical Analysis in C is the process of using algorithms and mathematical methods to solve numerical problems or equations using the C programming language. It involves writing code that can perform calculations and provide accurate numerical solutions.

2. How does Numerical Analysis in C differ from other programming languages?

Numerical Analysis in C is specifically designed for solving numerical problems, whereas other programming languages may have a wider range of applications. C is also known for its speed and efficiency, making it a popular choice for numerical analysis.

3. What is the importance of using C for numerical analysis?

C is a low-level programming language that allows for direct access to computer hardware, making it well-suited for performing complex calculations. It also has a large library of mathematical functions and a simple syntax, making it easier to write efficient code for numerical analysis.

4. How is a sum solved using Numerical Analysis in C?

To solve a sum using Numerical Analysis in C, the sum is first converted into an equivalent mathematical expression. Then, the appropriate algorithm is used to perform the calculation, taking into account any necessary precision or rounding. The result is then returned by the program.

5. What are some common challenges when using Numerical Analysis in C?

Some challenges when using Numerical Analysis in C include ensuring accuracy and precision in calculations, avoiding issues with floating-point arithmetic, and understanding the limitations of the programming language. It is important to have a strong understanding of both numerical analysis and C programming in order to effectively use this approach.

Similar threads

  • Computing and Technology
Replies
4
Views
664
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
499
  • Programming and Computer Science
2
Replies
50
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Atomic and Condensed Matter
Replies
3
Views
766
  • Quantum Physics
Replies
19
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
Back
Top