Thread: Program for function f(t) in C

  1. #1
    Registered User
    Join Date
    Aug 2021
    Posts
    3

    Program for function f(t) in C

    I have a function f(t) =
    e^(5) e^(5cos cos(t)) e^(10cos(t)) e^(5sin sin(t))
    I need to find the value of f for different values of t. Let us say from t=0 to t=5. How to write coding for that?. I just know some basics of C.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,069
    For, While and Do While Loops in C - Cprogramming.com

    I would likely use a for loop.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,734
    Code:
    double f ( double t ) {
      double answer = pow(2.7182818,-5);
      return answer;
    }
    int main ( ) {
      for ( double t = 0 ; t < 5 ; t++ ) {
        printf("f(%f)=%f\n", t, f(t));
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2021
    Posts
    3
    I should get like this
    f(0)= 1
    f(1)=1
    f(2)=1
    f(3)=1
    f(4)=1
    f(5)=1
    I have done the programming for f(0). Can you tell me how to insert loop here?
    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.14159265
    int t;
    double cos(double x);
    int main(int argc, const char * argv[])
    {
         double x, ret1, val, val1;
         int roundret1;
        t = 0;
                              /* first exponential*/
        /*define variables*/
        double value;
        double result;
      /* Assign the value we will find the exp of */
        value = -5;
    /* Calculate the exponential of the value */
        result = exp(value);
     /* Display the result of the calculation */
        printf("The Exponential of %f is %f\n", value, result);
        
                            /*second exponential*/
        /*defining variables*/
        double value1;
        double ret;
        double result1;
        int tempCheck, tempret1;
        
        val = PI / 180.0;
        ret = cos( t*val ); 
        printf("The ret is %f\n", ret);
        ret1 = cos(ret*val );
        printf("The ret1 is %f\n", ret1);
        /*rounding the ret1*/
        if(ret1>0)
        {
            tempret1 = ret1*10;
            tempCheck = tempret1%10;
            if(tempCheck>=5)
            {
                roundret1 = ret1;
                roundret1++;
            }
            else
            {
                roundret1 = ret1;
            }
            printf("The roundret1 is %d\n", roundret1);
        }
        
        /* Assign the value we will find the exp of */
        value1 = -5*roundret1;
        printf("The value1 is %f\n", value1);
        
    
    
        /* Calculate the exponential of the value */
        result1 = exp(value1);
        /* Display the result of the calculation */
        printf("The Exponential of %f is %f\n", value1, result1);
        
                    /* Third exponential*/
        /*defining variables*/
        double value2, result2;
        
        value2 = 10;
        result2 = exp(value2);
     /* Display the result of the calculation */
        printf("The Exponential of %f is %f\n", value2, result2);
        
                  /* fourth exponential*/
         /*defining variables*/
         double ret2, ret3, value3, result3;
         
       ret2 = sin(t*val);
       ret3 = sin(ret2);
       value3 = -5*ret3;
        printf("The value1 is %f\n", value2);
        /* Calculate the exponential of the value */
        result3 = exp(value3);
        /* Display the result of the calculation */
        printf("The Exponential of %f is %f\n", value3, result3);
                
        /*multiplying 4 exponentials*/
        double F;
        F=result*result1*result2*result3;
        printf("The final value of F when t=0 is %f\n", F);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    886
    Are you sure the equation is correct?

    Program for function f(t) in C-untitled-png

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,734
    > Can you tell me how to insert loop here?
    I already showed you.

    You put the bulk of that main() into another function and call it f(), with a parameter of t.

    Then the loop goes in main, as I SHOWED YOU!.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    886
    And... let's say the first equation is correct... So, you want to calculate a FUNCTION, isn't it? Why not to code a function f()?

    Code:
    double f(double t)
    {
      return exp(-5)*exp(-5*cos(cos(t)))*exp(10*cos(t))*exp(-5*sin(sin(t)));
    }

  8. #8
    Registered User
    Join Date
    Aug 2021
    Posts
    3
    Yes the equation is absolutely correct

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program calling Function in another program
    By tiger5678 in forum C Programming
    Replies: 3
    Last Post: 06-27-2013, 06:43 PM
  2. Need help with a function of a program.
    By Justin Sanford in forum C Programming
    Replies: 2
    Last Post: 06-20-2013, 12:27 AM
  3. c program using if function !!!!!!
    By ravikanyal11 in forum C Programming
    Replies: 9
    Last Post: 10-24-2011, 09:06 AM
  4. Help please with function program.
    By gator6688 in forum C++ Programming
    Replies: 12
    Last Post: 10-01-2007, 11:40 AM
  5. function program help
    By katie2 in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 08:27 PM

Tags for this Thread