Function Derivatives & Sines (C++)

In summary, the unworked program 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.
  • #1
Const@ntine
285
18

Homework Statement



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.

Homework 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;
}

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...
 
Physics news on Phys.org
  • #2
Hi,
I have two comments and some consolation for you: First comment:

Darthkostis said:
Code:
 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;
}
With error = 0 the program never enters the loop (it satisfies error < 1e-4)
And the second:
PS: Everything also has to be done in Fortran as well...
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
 
  • #3
What puzzles me somewhat is the interpretation of the criterion:
Darthkostis said:
the value we get from the formula, has the same first four decimal points
Do they mean nonzero digits ?
Do they mean after rounding to 4 decimal places ?
 
  • #4
And a caveat:
Darthkostis said:
considering it's not the main subject I'm studying
You shouldn't think lightly about the relevance of programming skills nowadays. Even law students, medical students, linguistics students neeed them !
 
  • #5
BvU said:
Hi,
I have two comments and some consolation for you: First comment:

With error = o the program never enters the loop (it satisfies error < 1e-4)

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!

BvU said:
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:.

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.

BvU said:
[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.

Thanks, I'll keep that in mind!

BvU said:
What puzzles me somewhat is the interpretation of the criterion:
Do they mean nonzero digits ?
Do they mean after rounding to 4 decimal places ?

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.

BvU said:
And a caveat:
You shouldn't think lightly about the relevance of programming skills nowadays. Even law students, medical students, linguistics students neeed them !

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.
 
  • #6
Look carefully at
C:
term = term*(-1)*x*x/(i+1)*(i+2);
What steps would the computer take ?
 
  • #7
Darthkostis said:
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
Been like that since ancient times. Everywhere (Pessimists are everywhere). You are the beginning of a new era :smile: and a better world !
 
  • #8
BvU said:
Been like that since ancient times. Everywhere (Pessimists are everywhere). You are the beginning of a new era :smile: and a better world !

Well, that's one way to look at it. Your POV certainly plays a big part on how you tackle anything.

BvU said:
Look carefully at
C:
term = term*(-1)*x*x/(i+1)*(i+2);
What steps would the computer take ?

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.
 
  • #9
Darthkostis said:
Problem is, the program completely shuts down
If you don't have a debugger, you'll need write statements to find out where it stumbles..

Darthkostis said:
the program isn't working
Works like a dream in Excel (I'm so brainwashed with Fortran I can only speak that and a bit of visual basic :confused: )
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:
 
  • #10
BvU said:
If you don't have a debugger, you'll need write statements to find out where it stumbles..

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:

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:
  • #11
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 !
 
  • #12
BvU said:
Sure. How do you want the program to exit from the loop ?

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.
 
  • #13
Darthkostis said:
C:
for( h = 0.2; ; h = h/2)
{
do {
Derivative = (((cos(x)*cos(h) - sin(x)*sin(h)) - cos(x))/h);
} while (Derivative != -sin(x));
}
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).
Darthkostis said:
It's the same logic (same first 4 decimals)
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:
  • #14
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.
 
  • #15
jim mcnamara said:
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.

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.
BvU said:
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.

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

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.
 
  • #16
Use a write statement inside the loop to find a clue ...
The ##[##code=..] tag doesn't want spaces
 
  • #17
BvU said:
Use a write statement inside the loop to find a clue ...
The ##[##code=..] tag doesn't want spaces

Okay, this is going to 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.
 
  • #18
From your code I think it's called cout in this cryptic language :smile:
[edit]something like
Code:
..
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:
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:
  • #19
BvU said:
From your code I think it's called cout in this cryptic language :smile:
[edit]something like
Code:
..
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:
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: !

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...).
 
  • #20
Best of luck ! And PF is here if you get stuck ...
 
  • #21
BvU said:
Best of luck ! And PF is here if you get stuck ...

Thanks and yeah, PF is a life-saver!
 
  • #22
Darthkostis said:
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.
We would prefer that you post one problem (program) per thread. We don't consider that cluttering, and it let's us focus more on each problem with less confusion about which program we're talking about.
 
  • #23
Mark44 said:
We would prefer that you post one problem (program) per thread. We don't consider that cluttering, and it let's us focus more on each problem with less confusion about which program we're talking about.

Oh, okay. It's just that they were similar in structure, so I figured it wouldn't breach the rules too much. Either way, I'll keep it in mind for the next time, thanks!
 
  • #24
Also, please use code tags. These tags preserve the indentation you have.

[code=c]
Your code goes here
[/code]
As already mentioned, there should be no spaces at all in the first or last code tags. And code=c is the one to use, whether you're writing C, C++, or C#.
 

FAQ: Function Derivatives & Sines (C++)

What is a function derivative?

A function derivative is a measure of how much a function changes at a specific point. It is represented by the slope of the tangent line at that point and can be calculated using the rules of calculus.

Why do we use derivatives in programming?

Derivatives are used in programming to model and analyze the behavior of functions. They can help us optimize algorithms, solve optimization problems, and understand the rate of change of a function.

How do you find the derivative of a function in C++?

To find the derivative of a function in C++, we can use the built-in mathematical functions and operators, such as the power function and the division operator, to represent the derivative formula. We can also use numerical methods, such as the finite difference method, to approximate the derivative at a given point.

What is the relationship between derivatives and sines?

Sines are periodic functions that can be differentiated using the chain rule, product rule, or quotient rule to find their derivatives. The derivatives of sines are important in many applications, such as signal processing and physics, as they represent the rate of change of oscillating quantities.

Can derivatives be used to find the extreme values of a function?

Yes, derivatives can be used to find the extreme values of a function. The maximum and minimum values of a function occur at the points where the derivative is equal to zero or does not exist. These points can be found by setting the derivative equal to zero and solving for the input variable.

Similar threads

Replies
3
Views
969
Replies
2
Views
3K
Replies
8
Views
1K
Replies
3
Views
1K
Replies
7
Views
1K
Replies
2
Views
2K
Replies
9
Views
2K
Replies
6
Views
3K
Back
Top