-1.#IND00 results from fourier series sum - c

I'm getting the "-1.#IND00" as a solution to this Fourier series.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n, x=50, L=100, q1=2;
float pi = 3.141592;
float flux1=0;
double flux2=0;
for(n=0;n<=50;n++)
{
flux1=q1*(2/(pi*n))*(cos(n*pi)-1)*(sin((n*pi*(x+L))/(2*L)));
flux2+=flux1;
}
flux2+=q1;
printf("%f\n", flux2);
return 0;
}
flux2 is coming out as "-1.#IND00" I can't work out why, since each term in the sum, (flux1), comes out as a rational number and the sum seems to converge.
Can you guys help?

This computation is in error on the first iteration when n == 0
2.0/(pi*n)
It's a "divide by zero" error.
On subsequent iterations, the computation is good, but the sum flux2 has already been corrupted by the bad value of the first flux1 and never recovers.

Related

Count function always returns at least 264 C

I am currently writing a program in c that is to do a few equations via functions. The possible data that is going to be read is up to 1 million two precision floating numbers excluding 0. I am getting an error when I try to count the number of numbers read into the array, but for some reason I get a default value of 264 every single time I run the program. So for instance if dont input any values I get a count of 264, 1 value 265, 2 values 266 and so on. I guess I could subtract 264 from count to get the accurate total but I want to know why this is happening and where the 264 comes from. I have attached the code I have so far below. Thank you.
#include <stdio.h>
#include <stdlib.h>
#define N 1000000
int count_num(double numbers[]);
double sum(double numbers[]);
double max(double numbers[]);
double min(double numbers[]);
double ar_mean(double numbers[]);
double har_mean(double numbers[]);
double variance(double numbers[]);
int main(void)
{
double numbers[N];
int i =0;
while(scanf("%lf.2",&numbers[i])!=EOF&&i<N)
{
i++;
}
int count=count_num(numbers);
printf("Count: %d\n", count);
}
int count_num(double numbers[])
{
int count=0;
for(int i=0;i<N;i++)
{
if((numbers[i]!=0)&&(numbers[i]!=0.0))
{
count++;
}
}
return count;
}
Both commenters, information_interchange and AnT, are correct, the uninitialized array is the cause of the problem-it contains unknown values. You need to initialize the array, either by looping and setting all elements to 0.0 or by using the construct double numbers[N] = {0.0};
Another one: your main function does not return int but has to according to ISO/IEC 9899:2011 5.1.2.2.1.

Arctan Taylor Series in C

I need to perform the Taylor Series for arctangent 50 times. Meaning 50 numbers between the domain of the arctan Taylor Series which is [-1,1]. I've tested it out with manual user input and it works fine, but the for loop for the 50 different inputs which I increment in the code by 0.01 and their corresponding results has been unsuccessful. I've tried everything I could think of so far, I'm out of ideas. Any help would be appreciated. Is there an issue with my brackets surrounding the Taylor Series that's conflicting with the other for loop? I've suspected it was the brackets but nothings worked when I attempted to fix it.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main (void) {
double n;
double x;
double tSeries=0.0;
double t2;
double t1;
for(n=0;n<=50;++n) {
for(x=-1;x<=1;x=x+0.01) {
t1=(pow(x,2*n+1))*pow(-1,n);
t2=t1/(2*n+1);
tSeries+=t2;
printf("arctan(%lf)=%lf\n",x,tSeries);
}
}
return 0;
}
In the code you've posted the inner loop is over the variable x, and the outer loop is over the power n.
I think you want to sum over values of n for each value of x, so the loop over n should be the inner loop.
I think you also need to zero your sum, tSeries for each value of x.
Finally, I expect you want to print the answer after calculating the sum, so printf should be outside the n loop.
There are a few tricks to the evaluation of power series. I like Numerical Recipes for this sort of thing. Try chapter 5 on the evaluation of functions. (Numerical Recipes in C, Press et al., 2nd Ed., 1992, CUP.)
One thing to note right away is that with the upper limit of the power series fixed, you are evaluating a polynomial. Section 5.3 of my copy of NR recommends strongly against using a sum of calls to pow(). They are quite firm about it!
Let me know if you want me to post correct code.
You got the loops mixed, the inner one goes out and vice versa.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
double n;
double x;
double tSeries = 0.0;
double t2;
double t1;
for (x = -1; x <= 1; x += 0.01) {
for (n = 0; n <= 50; n++) {
t1 = (pow(x, 2 * n + 1)) * pow(-1, n);
t2 = t1 / (2 * n + 1);
tSeries += t2;
}
printf("arctan(%lf)=%lf (%lf)\n", x, tSeries, atan(x));
tSeries = 0.0;
}
return 0;
}

C - How to print only the largest number in a for loop?

I am having a go at Project Euler question 4 - Finding the highest palindrome from a product of two 3-digit numbers. I have found many solutions online, none of which answer the issues I am experiencing with my own code. I am not an experienced coder and have just starter learning recreationally.
My code so far does a for loop which generates all of the numbers that are palindromes. Currently I have it printing all of them just to check that it is working, which it appears to be. I was wondering if there was a way to print only the largest number in the loop? I don't think running through manually is the most efficient way of doing it.
Here is the code so far:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int a,b,c,d;
for(a=100;a<=999;a++){
for(b=100;b<=999;b++){
c=a*b;
d=reverse(c);
if(c==d){
printf("%d is a palindrome\n",c);
}
}
}
return 0;
}
int reverse(int number){
int answer=0;
while(number!=0){
int units=number%10;
answer=answer*10+units;
number=number/10;
}
return answer;
}
Assign the largest result to a variable. Then when you find another that is bigger, update that variable.
Then when the loop is complete, print the result from the variable which is the largest.
I'll let you work out the code. It's trivial.

Issuies with the fibonacci series and c

#include <stdio.h>
#include <math.h>
int fib();
int scan;
int main() {
scanf("%d", &scan);
printf("%d\n", fib());
scanf("%s");
return 0;
}
int fib() {
return floor((pow(1+sqrt(5)/2, scan)-(-pow(1-sqrt(5)/2, scan)))/sqrt(5));
}
I'm pretty new to programming with C and decided to try and calculate any number in the Fibonacci series. I based it off of my lua script here. I'm at a loss of what I've done wrong, could someone give me some insight?
You have the formula wrong. You want fib to be:
int fib() {
return round((pow((1+sqrt(5))/2, scan)-(-pow((1-sqrt(5))/2, scan)))/sqrt(5));
}
instead. You were missing parenthesis around the 1+sqrt(5) and 1-sqrt(5) terms and were using floor instead of round, which was underestimating the fibonacci numbers in my tests. (This mostly has to do with low precision in the pow function. The seventh fibonacci number, 13, came out to 12.969)
You also probably want to change
scanf("%s");
to
char tmp;
scanf("%c", &tmp);
Since the way you have it incorrectly omits an argument.
Hope this helps!

Incorrect formula?

So here is a program that is supposed to calculate an approximation to the value of pi if you take enough terms into the sum which is mathematically described in the following program and calculates the expression of the root, you get a value that gets closer and closer to the value of pi the more terms you have.
#include <stdio.h>
#include <math.h>
main()
{
int j, terms;
double sum, precision, pi;
printf("How many terms: "); scanf("%d", &terms);
for(j=1;j<=terms;j++)
sum+=1/(j*j);
pi = sqrt(6*sum);
printf("Pi: %lf.\n", pi);
}
But there is something making it go wrong here and I can't quite figure out what.
sum+=1/(j*j);
I thought the mistake might be in that line because all others look fine,thinking at first maybe the computer isn't counting decimals.I'm unsure.But my question is: What is it in this code that makes it malfunction?And how do I fix it?
This performs integer division:
1/(j*j);
try this:
sum+=1.0/(j*j);
If j*j might overflow, do this
sum+=1.0/((double)j*j);

Resources