I'm trying to do the golden ratio with fibonacci - c

I'm trying to do the golden ratio with fibonacci but I get the error that the integral goes to zero
#include <stdio.h>
#include <conio.h>
#include <math.h>
int fibonacci (double n){
if (n==0 || n==1)
return n;
else
return fibonacci(n-1)+fibonacci(n-2);
}
double fibonacci_golden_ratio(int n){
double phi;
phi = fibonacci(n) / (double) fibonacci(n-1);
return phi;
}
int main(){
int i;
for (i=1;;i++){
printf ("F = %d ",fibonacci(i));
printf ("phi = %.5lf \n", fibonacci_golden_ratio(i));
if ( fabs (fibonacci_golden_ratio(i) - fibonacci_golden_ratio(i-1)) < 0.0001 )
break;
}
printf ("phi = %.5lf\n", fibonacci_golden_ratio(i));
getch();
}
how can i run the code perfectly

Related

How do I do a summation in C without math.c?

Hello I need to create this summation and if you put the number 30000 the response should the number of pi, however it's not working here's the summation and here's the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <ctype.h>
#include <stdbool.h>
int main( void ){
int num, k;
double pi= 0;
printf("Digite o total de termos >=30000: ");
scanf("%d", &num);
if (num < 30000){
printf("Erro.");
}else {
for (k = 1; k<= num; k++){
if (k % 2 == 0){
pi = (double)(-1)/ (2*k -1);
}else{
pi = (double)(1)/ (2*k -1);
}
pi = pi * 4;
}
printf("O valor de pi e %f", pi);
}
return 0; }
Here is a general way to sum things up:
double sum(int from, int to, double (*f)(int)) {
double ret = 0.0;
for(int i=from; i<to; i++)
ret+=f(i);
return ret;
}
And then you can write this function:
double fun(int current) {
double sign = current %2 == 0 ? -1.0 : 1.0;
return sign / (2*current - 1)
}
Finally, call it like this:
double pi = sum(1, num, fun);
Do note that this is probably not a good way to go if you're a beginner student that is looking for a solution to some homework.
#include <stdio.h>
int main(void) {
double pi=0;
for(int k=1; k<=3000; ++k)
{
pi += (2.*(k%2)-1) / (2*k-1); // This line does a summation, and uses floating point math (Not Integer Math)
}
printf("Pi : %f\n", 4*pi);
return 0;
}

using the Taylor series to calculate the sin is returning big numbers

I'm trying to calculate the sin with the Taylor series but for some reason it doesn't work and it's returning big numbers (bigger than 1). I saw some examples on google but I don't see where I did a mistake. here's the code:
#include <stdio.h>
#include <math.h>
int factorial(int n) {
int i, f=1;
for (i=1; i<=n; i++) {
f= f*i;
}
return (f);
}
int main() {
int x, i, j=0;
float s=0;
printf("write a number to calculate sin(x)\n");
scanf("%d",&x);
for(i=1;i<=x ;i=i+2) {
if(j%2==0){
s -= pow(x, i)/factorial(i);
}
else{
s += pow(x, i)/factorial(i);
}
j++;
}
printf("sin(%d)=%f \n", x, s);
}
here's an example output: sin(6)=-34.799999
but in a calculator: sin(6)=-0.27941549819

Wrong result after a division

In an exercice I have to give to a function an array and his size to get the average of his value.
So I've tried this :
#include <stdio.h>
#include <stdlib.h>
double average(double array[], double array_size);
int main()
{
double array[4] = {12.0, 20.0, 8.9, 14.7};
printf("The average of the array is : %lf\n", average(array, 4.0));
return 0;
}
double average(double array[], double array_size)
{
int array_average = 0, i = 0;
while(i < array_size)
{
array_average += array[i];
i++;
}
return array_average / array_size;
}
I clang the file and run the ./a.out and that return me 13.500000 instead of 13.9 when I do the average with the calculator.
I don't see where is the errors, thanks for your help !
If that can help someone later I post my final code
#include <stdio.h>
#include <stdlib.h>
double average(double array[], double array_size);
int main()
{
double array[4] = {12.0, 20.0, 8.9, 14.7};
printf("The average of the array is : %lf\n", average(array, 4.0));
return 0;
}
double average(double array[], double array_size)
{
double array_average = 0.0;
int i = 0;
while(i < array_size)
{
array_average += array[i];
i++;
}
return array_average / array_size;
}

Calculating a Serie in C

This image is the task I should do:
Whatever I enter between -1 and 1, the outputs are always 1.0000 or 2.0000. How can I do solve this problem? Below I attached my code.
#include <stdio.h>
#include <math.h>
int main() {
int i;
float x;
float sum=0;
printf ("enter an x\n");
scanf ("%f",&x);
if ((x>-1)&&(x<1))
{
for (i=0;i<101;i++)
sum= sum + (pow(x,i));
}
printf ("result=%f",sum);
return 0;
}
if ((x>-1)&&(x<1))
With this case your code will work only if x is zero so try removing if statement and do mention what output you expect for given particular input, it will be bit more helpful to answer it.
Try this code:
#include <stdio.h>
#include <math.h>
int main() {
int i; float x;
float sum=0;
printf ("enter an x\n");
scanf ("%f",&x);
for (i=0 ;i<101; i++)
sum+= (pow(x,i));
printf ("result=%f",sum);
return 0;
}
Even upon using a type like double you haven't got enough numerical precision to sum all the powers up to 100.
Executing the following snippet, you'll notice that, while the correct (numerically speaking) result is evaluated, the loop stops way before the 100th iteration, typically at 16:
#include <stdio.h>
#include <math.h>
#include <float.h>
// Analytically calculates the limit for n -> inf of the series of powers
double sum_of_powers_limit(double x)
{
return 1.0 / (1.0 - x);
}
int main(void)
{
double x = 0.1;
const int N = 100;
double sum = 1.0;
for (int i = 1; i <= N; ++i)
{
double old_sum = sum;
sum = sum + pow(x,i);
if (old_sum == sum)
{
fprintf(stderr, "Numerical precision limit reached at i = %d\n", i);
break;
}
}
printf(" result = %.*e\n", DBL_DECIMAL_DIG, sum);
printf("expected = %.*e\n", DBL_DECIMAL_DIG, sum_of_powers_limit(x));
return 0;
}
Also note that a more efficient way to evaluate this kind of polynomials is the Horner's method:
// Evaluates the sum s(x) = 1 + x + x^2 + ... + x^n using Horner's method
// It stops when it cannot update the value anymore
double sum_of_powers(double x, int n)
{
double result = 1.0;
for (int i = 0; i < n; ++i)
{
double old_result = result;
result = 1.0 + x * result;
if (old_result == result)
{
fprintf(stderr, "Numerical precision limit reached at i = %d\n", i);
break;
}
}
return result;
}

why am I getting result=0.00000

I'm trying to calculate sin and cos without using math library with taylor series witch is
sinx =∑n=0 to n=∞ (-1)^n * x^(2n+1) / (2n+1)! and this code produces whatever the input is 0,00000 for sin 1,00000 for cos where is the problem in this code
#include <stdio.h>
#include <stdlib.h>
double fakt(int);
double power(int,int);
int negative_positive(int);
double calculate_sin(int,int);
double calculate_cos(int,int);
double calculate_radyan(int);
int main() {
int degree,number;
char command;
do{
scanf("%c",&command);
if(command=='d' || command=='D'){
scanf("%d %d",&degree,&number);
double radyan = calculate_radyan(degree);
printf("%lf \n%lf ",calculate_sin(radyan,number),calculate_cos(radyan,number));
}
}while(command!='e' && command!='E');
return 0;
}
double fakt(int n){
int i;
double result=1;
for(i=1;i<n;i++)
result = result*i;
return result;
}
double power(int base, int exponent){
int i;
double result=1;
for(i=0;i<exponent;i++)
result = result*base;
return result;
}
int negative_positive(int number){
if(number % 2 == 0)
return 1;
else
return -1;
}
double calculate_sin(int degree , int n){
int i;
double result=0;
double tmp=0;
for(i=0;i<n;i++){
tmp=negative_positive(i)*power(degree,2*i+1)/fakt(2*i+1);
result=tmp+result;}
return result;
}
double calculate_cos(int degree , int n){
int i;
double result=0;
double tmp=0;
for(i=0;i<n;i++)
{
tmp=negative_positive(i)*power(degree,i*2)/fakt(2*i);
result=tmp+result;
}
return result;
}
double calculate_radyan(int degree){
double result,pi=3.14159;
result =pi/180*degree;
return result;
}
ı will answer my own question but ı found the solution. when program trys to go to power method with double radyan variable it converts that variable to 0 cause that method just takes integer variables power(double base,int exponent) is solved my problem

Resources