- #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...