1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Function Derivatives & Sines (C++)

  1. Apr 2, 2017 #1
    1. The problem statement, all variables and given/known data

    Okay, I'm going to "cheat" a bit and add two programs here, but I don't want to clutter the board by making two threads. Anyways, here goes:

    (1) The value of the sine of an angle, measured in rads, can be found using the following formula:
    sin(x) = x - x3/3! + x5/5! - ...
    Rework the following program, so that the value we get from the formula, has the same first four decimal points as the value we get from the function (sin(x)).

    (2) The derivative of a function can be found through the formula: f'(x) = lim(h -> 0)(f(x+h) - f(x))/h
    Write a program, using the do-while command, that computes the value of the derivative of the function f(x) = cos(x), starting with h = 0.2, and then dividing h by 2, with each turn, until two consecutive computations of the derivative, have the same first four decimal points.

    2. Relevant equations

    (1) The unworked program:

    #include <iostream>
    #include <cmath>
    using namespace std;

    int main()
    {
    float x, term, sinus, sine, error;
    int i = 0;

    cout << "Type the angle in radians: ";
    cin >> x;

    error = 1;
    sine = x;
    term = sine;
    sinus = sin(x);

    while(abs(error) > 1.0e-4)
    {
    term = term*(-1)*x*x/(i+1)*(i+2);
    i += 2;
    sine += term;
    error = sinus - sine;
    }

    cout << sinus << sine << error << endl;

    return 0;
    }


    3. The attempt at a solution

    My Programs:

    (1)

    #include <iostream>
    #include <cmath>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    float x, term, sine, error, sinus;
    int i=1;

    cout << "Type the angle in rads: ";
    cin >> x;

    cout << endl;

    error = 0;
    sine = x;
    term = sine;
    sinus = sin(x);

    while(abs(error) > 1.0e-4)
    {
    term = term*(-1)*x*x/((i+1)*(i+2));
    i += 2;
    sine += term;
    error = sinus - sine;
    }
    cout << sinus << " " << sine << " " << error << endl;

    return 0;
    }

    (2)

    #include <iostream>
    #include <cmath>
    using namespace std;

    int main()
    {
    double x, h;
    double Derivative;

    cout << "Type in the value of the angle: ";
    cin >> x;
    cout << endl;

    cout << "The original function's value is: " << cos(x) << endl << endl;
    cout << "In theory, the derivative function's value is: " << -sin(x) << endl << endl;

    for( h = 0.2; ; h = h/2)
    {
    do {
    Derivative = (((cos(x)*cos(h) - sin(x)*sin(h)) - cos(x))/h);
    } while (Derivative != -sin(x));
    }

    cout << "The derivative from the formula gives us: " << Derivative << " which is about the same as the derivative from the theory." << endl;

    return 0;
    }

    Okay, a bit of background: I've been "working" with C++ for a little over 5 months, have no priot programming experience and mostly know the basics. Not for a lack of trying, it's just that the Uni Class is awful (the prof. has come to teach, what, 8 times over two Semesters, and there are only Post Grads. in our Lab, who again, do not show us anything) and I've got a whole lot of different subjects to stud for, so Prog. is being put on the backburner a bit, considering it's not the main subject I'm studying.

    Nevertheless, I've done some progress on my own, and have amanged to struggle through certain programs, but we're moving so fast that I can simply not catch up (and not only me, the majority is doing even worse) to the "supposed" curicculum.

    Anyway, I'd greatly appreciate some help, as these were supposed to be done last week (and this week's subject is arrays, whi I know little to nothing about, so... yeah...). I tried more, but...

    PS: Everything also has to be done in Fortran as well...
     
  2. jcsd
  3. Apr 2, 2017 #2

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Hi,
    I have two comments and some consolation for you: First comment:

    With error = 0 the program never enters the loop (it satisfies error < 1e-4)
    And the second:
    Now, there's a real programming language ! :smile:

    The consolation is that programming is something you can quite well teach yourself: it's a matter of doing and learning from your mistakes. And you learn more the more you do and the more mistakes you make. (Slight bummer: you have to notice the mistakes in order to fix them and learn from them :smile:.

    [edit] and a PF tip to boot: use ##[##code=C++] and ##[##/code] tags around pieces of program; and perhaps: don't repeat all code but only a chunk around the changes you make
     
  4. Apr 2, 2017 #3

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    What puzzles me somewhat is the interpretation of the criterion:
    Do they mean nonzero digits ?
    Do they mean after rounding to 4 decimal places ?
     
  5. Apr 2, 2017 #4

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    And a caveat:
    You shouldn't think lightly about the relevance of programming skills nowadays. Even law students, medical students, linguistics students neeed them !
     
  6. Apr 2, 2017 #5
    Yeah, I caught that a bit latter. The program still doesn't work though, as I get an #IND answer (when using the formula). Any recs as to where I'm going wrong? I tried to tackle it logically, but it seems I'm missing something.

    Starting from the variables:

    The sine of the inserted angle is sine, which equals sin(x) (sine = sin(x))
    i is the integer that will help me find the factorial.
    Each term is well... the term.
    The sine from the formula is named sinus, and for starters, it's equal to the term.
    The difference between the two, is the error.

    Going into the "main part":

    While the absolute value of the error is above the four decimal points, the loop keeps going on.
    Inside the loop (for the first time), we get the second the term (x3/3!).
    i is increased by 2 (so now we go from 1, to 3).
    sinus (the sine from the formula) now equals to the inserted angle, plus the second term.
    The difference between the two sines, is given by sine - sinus.
    And this keeps going on until the absolute value of error, isn't larger than 1.0e-4

    Still, the program isn't working. Where am I going wrong?

    PS: Any thoughts on the second one?

    Thanks!

    Yeah, I noticed that. I've improved a bit since the last semester, but practice (and research) takes a whole lotta time, and I'm studying physics, so programming ain't my "forte" as of right now.

    Thanks, I'll keep that in mind!

    From what I gathered, the first nonzero digits. So, for example, sin(30) = -0.988031... The formula must give us -0.9880 for it to be correct.

    oH, I know it's important, it's just that there's really no time. Universities here are... "different", for a lack of a better word. The curiculum is more wide-spread, the professors are more apathetic, the equipment is much older. They're generally in decline.
     
  7. Apr 2, 2017 #6

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Look carefully at
    Code (C):
    term = term*(-1)*x*x/(i+1)*(i+2);
    What steps would the computer take ?
     
  8. Apr 2, 2017 #7

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Been like that since ancient times. Everywhere (Pessimists are everywhere). You are the beginning of a new era :smile: and a better world !
     
  9. Apr 2, 2017 #8
    Well, that's one way to look at it. Your POV certainly plays a big part on how you tackle anything.

    Yeah, I noticed that the problem, and then fixed it like this:

    term*(-1)*x*x/((i+1)*(i+2))

    Problem is, the program completely shuts down. I enter the angle, I press enter, and nothing happens. The Order of Operations is left to right, and from the one, first go the parentheses and then the muliplications (*,/,%). Technically, it should work, but it doesn't.
     
  10. Apr 2, 2017 #9

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    If you don't have a debugger, you'll need write statements to find out where it stumbles..

    Works like a dream in Excel (I'm so brainwashed with Fortran I can only speak that and a bit of visual basic :confused: )
    Code (C):


    X 10
    SIN_X  -0.54402
    i term sine error
    1 10 10 -10.544
    3 -166.667 -156.667 156.1226
    5 833.3333 676.6667 -677.211
    7 -1984.13 -1307.46 1306.916
    9 2755.732 1448.272 -1448.82
    11 -2505.21 -1056.94 1056.395
    13 1605.904 548.9652 -549.509
    15 -764.716 -215.751 215.2072
    17 281.1457 65.3945 -65.9385
    19 -82.2064 -16.8119 16.26783
    21 19.57294 2.761091 -3.30511
    23 -3.86817 -1.10708 0.563058
    25 0.644695 -0.46238 -0.08164
    27 -0.09184 -0.55422 0.0102
    29 0.01131 -0.54291 -0.00111
    31 -0.00122 -0.54413 0.000106
    33 0.000115 -0.54401 -9E-06
    35 -9.7E-06 -0.54402 6.8E-07
    37 7.27E-07 -0.54402 -4.6E-08
     
    As you see it starts to run into numerical noise if terms become too small. X = 20 never makes it. Perhaps you want to restrict the input domain -- but I suppose that's outside the scope of this exercise.

    Haven't looked at part two (I'm male, can only do one thing at a time) :biggrin:
     
  11. Apr 2, 2017 #10
    Yeah, turns out it works just fine. I was just used to putting 30 as the angle, which I guess was too large a number. I tried with 5 and it works as intended. Thanks for the help! Now to translate it to Fortran...

    PS: Any help is appreciated on the second one!
     
    Last edited: Apr 2, 2017
  12. Apr 2, 2017 #11

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Sure. How do you want the program to exit from the loop ?
    [edit] and: it's a good principle to let the computer do the work:
    so cos(x+h) - cos(x)

    In your case you do work and the computer now has to do more work: evaluate 4 instead of 2 trigonometric functions -- if the compiler is smart enough, otherwise even five !
     
  13. Apr 2, 2017 #12
    The second one? It's the same logic (same first 4 decimals) as the first one, but this time, it's about the derivative of the function f(x) = cos(x) instead of just computing the sine of an angle.
     
  14. Apr 2, 2017 #13

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    is what you proposed. I see no way for the program to exit this loop...unless all digits of the last to are exactly the same, which will (almost) never happen (due to this numerical noise again).
    Correct. So use a similar construct.

    Corollary: never use ##a\ne b## for non-integer numbers. Remember you aren't really working with real numbers, only with (finite) digital representations for them.

    [edit] nor ##a=b##, of course. Always use something like ##abs(a-b)\le \epsilon##
     
    Last edited: Apr 2, 2017
  15. Apr 2, 2017 #14

    jim mcnamara

    User Avatar

    Staff: Mentor

    Note: float datatypes in C++ are required to support at least 6 digits of precision. I would suggest that some of the output you get is probably meaningless.
    Consider some other datatype with much greater digits of precision. At a minimum - double. Boost Multiprecision might also work for you.
     
  16. Apr 2, 2017 #15
    I tried with double at first, since it's a double precision type, but the Post-Grad guy told me to try with float, due to the two "results" (the derivative from the formula and the one from -sin(x)) needing to have the same first 4 digits.


    I noticed that after I understood the basis of the first, and did this:

    Code (C):

    Difference = 1;

      do {
        Derivative = (((cos(x)*cos(h) - sin(x)*sin(h)) - cos(x))/h);
        Difference = (-sin(x)) - Derivative;
        h /= 2;
         } while (abs(Difference) > 1.0e-4);
     
    Note: I changed error to Difference.

    No luck yet though. The excercise says that we have to use do-while. I got rid of the for loop, but I'm not getting a result.
     
  17. Apr 2, 2017 #16

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Use a write statement inside the loop to find a clue ....
    The ##[##code=..] tag doesn't want spaces
     
  18. Apr 2, 2017 #17
    Okay, this is gonna be a dumb question, but... what is a "write statement"? My books aren't in english, so I'm not that good with all definitions, commands and whatnot yet.
     
  19. Apr 2, 2017 #18

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    From your code I think it's called cout in this cryptic language :smile:
    [edit]something like
    Code (C):
    ..
    cout << "h: " << h << " gives difference " << Difference << endl ;
    h/=2;
    I managed to get your code running (Visual studio 2010, luxurious ! :cool:) and it looks just fine. But it runs so fast (at least on my PC) the console window is gone in a flash :smile: so I added a read at the end:
    Code (C):

    cout << "The derivative from the algorithm gives us: " << Derivative << " which differs " << Difference << " from the exact value" << endl;
    cout << "Type in something to finish ";
    cin >> x;
    return 0;
    }
    By the way, I really am a native Ftn/VB programmer, so I learn some C(++?) from this thread. Fun :wink: !
     
    Last edited: Apr 2, 2017
  20. Apr 2, 2017 #19
    Turns out, I had forgotten to give h a starting value (0.2 in this case) when I edited the program a bit. D'oh. But yeah, it rusn as intended. As for the "closing too fast" thing, we're using a "special" editor/compiler provided to us. When I tried to download a compiler of my own (DevC++) I found out about needing breaks. But since I'm using the first one (it's called Cedt), I forget all about them.

    Either way, thanks a ton for the help! :wink:Now to turn them into Fortran programs, and move on to the next chapter (and it features arrays, which is a shame, since I know almost nothing about them...).
     
  21. Apr 2, 2017 #20

    BvU

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Best of luck ! And PF is here if you get stuck ....
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Function Derivatives & Sines (C++)
Loading...