How to translate this equation to C++ - c++

I am trying to translate this equation to c++ code:
x = (10π)/(a+b)*sinC^3+3(ln a)(tan C)
Here is my attempt:
#include <iostream>
#include <iomanip>
#include <math.h> using namespace std;
int main()
{
float x, y, z, a, b, C, PI;
cout << endl << "Enter value a=";
cin >> a;
cout << "Enter value b=";
cin >> b;
cout << "Enter angle C in degrees=";
cin >> C;
PI = 3.1416;
C = C * PI / 180;
x = ((10 * PI) / (a + b)) * pow(sin(C), 3);
+3 * (log(a)) * (tan(C));
y = 0;
z = 0;
cout << fixed << setprecision(4);
cout << endl << "x = " << x;
cout << endl << "y = " << y;
cout << endl << "z = " << z;
}

Pi is defined in math.h, as M_PI
Sine is defined in math.h as double sin(double)
Tangent is defined in math.h as double tan(double)
Natural Log is defined in math.h as double ln(double)
Power is defined in math.h as double pow(double,double)
You wrote:
x = ((10 * PI) / (a + b)) * pow(sin(C), 3);
+3 * (log(a)) * (tan(C));
The second 1/2 of that (starting at +3) is not part of the original expression, which ended with a semi-colon.
Try:
x = ((10 * PI) / (a + b)) * pow(sin(C), 3) + 3 * (log(a)) * (tan(C));
Also, be sure you understand the difference between log(base10) and ln(natural log).

Related

Functions arcsin and arccos are not working properly

I need to write the code for solving a simple equation, which should contain arcsin and arccos.
Formulas
F = cos^2(p(x)) + sin^2(p(y) + p(z)
p(k) = arccos(a * k) + arcsin(k)
Here is the code I wrote for solving it, but it gives -nan Ind as output. However, when I fix acos and asin to just cos and sin, everything works.
Expected input:
x = 1.20
y = 3.05
z = 2.00
Code
#include <iostream>
#include <math.h>
using namespace std;
const double a = 0.01;
double p(double k) {
double p1;
p1 = acos(a * k) + asin(k);
return (p1);
}
int main() {
double x;
double y;
double z;
double F;
cout << "x = "; cin >> x;
cout << "y = "; cin >> y;
cout << "z = "; cin >> z;
F = pow(cos(p(x)), 2) + pow(sin(p(y)), 2) + p(z);
cout << "F = " << F << endl;
return 0;
}
The OUTPUT range of SIN and COS is [-1, +1]. That means the INPUT range of ARC (inverse) sin is [-1, +1]. You are trying to do asin(1.2), which probably yields NaN.

Functions should be correct, but why is the final output 0 here?

#include <iostream>
#include <iomanip>
using namespace std;
//Power function
float power (float base, int exp)
{
if (exp < 0)
{
if (base == 0)
{
cout << "Base cannot be 0.";
return -1;
}
}
if (exp == 0)
return 1;
if (exp == 1)
return base;
return base * power (base, exp - 1);
}
//Factorial function
int facto (int n)
{
return n <= 0 ? 1 : n * facto (n - 1);
}
//Cos function
float cosCalc (float rad)
{
float cos = 0;
int x;
for (x = 0; x < 10; x++)
{
cos += power (-1, x) * power (rad, 2 * x) / facto (2 * x);
}
return cos;
}
//Sin function
float sinCalc (float rad)
{
float sin = 0;
int x;
for (x = 0; x < 10; x++)
{
sin += power (-1, x) * power (rad, 2 * x + 1) / facto (2 * x + 1);
}
return sin;
}
//Main function
int main()
{
int choice;
//Title and Menu
cout << endl << "==============" << endl << " TRIGONOMETRY " << endl << "==============";
cout << endl << "Select:";
cout << endl << "1. Calculate Cos and Sin";
cout << endl << "9. Exit";
while (true)
{
//User Prompt
cout << endl << endl << "Please enter your choice. => ";
cin >> choice;
if (choice == 1)
{
int angle, anglePh;
float rad;
float pi = 3.14159265358979323846264338327950288419716;
char angleType;
float cos = 0;
float sin = 0;
cout << endl << "Please enter an angle. => ";
cin >> angle;
anglePh = angle;
angle %= 360;
rad = angle * pi / 180;
cout << anglePh << " degrees = " << rad << " radian";
cout << endl << "Calculating Cos...";
cosCalc (rad);
cout << endl << "Cos = " << fixed << cos;
cout << endl << "Calculating Sin...";
sinCalc (rad);
cout << endl << "Sin = " << fixed << sin;
}
if (choice == 9)
{
break;
}
}
}
I am building a program that calculates Sin and Cos off an angle input, and when I run it, it outputs 0.000000 for both Sin and Cos. I suspect there is something to do with me declaring float cos = 0 and float sin = 0 in the if loop for choice == 1, and I tried messing around with it but it either results in the program straight out giving me errors on launch, or I get the same outputs.
Any idea where I went wrong?
Thanks for your insight in advance, cheers!
Your cosin and sine function return a float, but in order to get that result you still have to store it in a variable.
So instead of:
cosCalc (rad);
Do:
rad = cosCalc (rad);
and the same for your sine function.

Why does the error keep returning "variable has incomplete type void"?

I'm trying to write a code that will solve Coulomb's Law equation for force between two charged particles (using void) and I keep getting
error: variable has incomplete type 'void'.
The code should measure for r=1 and then loop (adding 3 to r every time) and finishing when r < 60. It will output all of these results.
int main() {
float Q_one;
float Q_two;
int rad = 1;
double const k = 8990000000;
cout << "Enter the charge of particle 1." << endl;
cin >> Q_one;
cout << "Enter the charge of particle 2." << endl;
cin >> Q_two;
void force (float Q_one, float Q_two, int rad);
void force = ((k * ((Q_one * .000001) * (Q_two * .000001))) / (rad * rad));
while (rad <= 60) {
{
force (Q_one, Q_two, rad);
cout << "Radius " << (rad) << " ";
cout << "Force " << (force) << endl;
rad += 3;
}
}
}
I've tried rearranging this numerous ways and changing how I define 'force' but nothing seems to work. Any ideas as to how I can fix this?
I am not sure what you trying to do.
After fixing some mistake I got this :
#include <iostream>
#include <type_traits>
#include <vector>
using namespace std;
int main() {
float Q_one;
float Q_two;
int rad = 1;
double const k = 8990000000;
cout << "Enter the charge of particle 1." << endl;
cin >> Q_one;
cout << "Enter the charge of particle 2." << endl;
cin >> Q_two;
auto force = [k](float Q_one, float Q_two, int rad) { return (k * Q_one * .000001 * Q_two * .000001) / (rad * rad); };
while (rad <= 60)
{
auto result = force (Q_one, Q_two, rad);
cout << "Radius " << (rad) << " ";
cout << "Force " << (result) << endl;
rad += 3;
}
}
Demo : wandbox
Does it look like what you want ?
I assume you are trying to make a function to calculate force with. You can, for example make it a lambda function:
auto force = [=](float Q_one, float Q_two, int rad)
{ return ((k * ((Q_one * .000001) * (Q_two * .000001))) / (rad * rad)); };
And then you call it in the loop and assign it to let's say F like this:
while (rad <= 60) {
double F = force(Q_one, Q_two, rad);
cout << "Radius " << (rad) << " ";
cout << "Force " << (F) << endl;
rad += 3;
}

Quadratic equation solver outputting in unknown format

I'm trying to make a quadratic equation solver, but for some reason my program is giving me answers in an unknown format.
I entered the simple quadratic equation x^2 + 2x + 1 = 0, expecting my program to give x = -1 or x = -1, but instead it gave x = 0138151E or x = 0138152D. It looks like it outputs these values for x for any inputs (not recognizing unreal answers and catching them). Why is this and how can I fix it?
#include "../std_lib_facilities_revised.h"
class Imaginary {};
double square(int a)
{
return a * a;
}
double quadratic_solver_pos(int a, int b, int c)
{
double x = 0.0;
double radicand = square(b) - 4 * a * c;
if (radicand < 0) throw Imaginary{};
x = (-b + sqrt(radicand)) / (2 * a);
return x;
}
double quadratic_solver_neg(int a, int b, int c)
{
double x = 0.0;
double radicand = square(b) - 4 * a * c;
if (radicand < 0) throw Imaginary{};
x = (-b - sqrt(radicand)) / (2 * a);
return x;
}
int main()
try {
cout << "This program is a quadratic equation solver.\n";
cout << "Quadratic equations are of the form: ax^2 + bx + c = 0\n";
cout << "Enter a, b, and c, respectively:\n";
double a = 0;
double b = 0;
double c = 0;
cin >> a >> b >> c;
cout << "Your quadratic equation: " << a << "x^2 + " << b << "x + " << c << " = 0\n";
cout << "x = " << quadratic_solver_pos << " or x = " << quadratic_solver_neg << '\n';
}
catch (Imaginary) {
cout << "x is unreal\n";
}
You don't pass your variables to your functions.
You need to do it like so quadratic_solver_pos(a, b, c);.

C++ truncation error?

I am learning C++ and came upon this problem while trying to use a formula to calculate the current.
And I got: 0.628818 where the answer should be:
f=200 Hz
R=15 Ohms
C=0.0001 (100µF)
L=0.01476 (14.76mH)
E = 15 V
Answer: I = 0.816918A (calculated)
Below is my code:
#include <iostream>
#include <cmath>
int main()
{
const double PI = 3.14159;
double r = 15;
double f = 200;
double c = 0.0001;
double l = 0.01476;
double e = 15;
double ans = e / std::sqrt(std::pow(r, 2) + (std::pow(2 * PI*f*l - (1.0 / 2.0 * PI*f*c), 2)));
std::cout << "I = " << ans << "A" << std::endl;
}
I have read about truncation errors and tried to use 1.0/2.0 but doesn't seem to work either.
Truncation error refers to using only the first N terms of an infinite series to estimate a value. So the answer to your question is "No." You might find the following to be of some interest however....
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
template<typename T>
T fsqr(T x) { return x * x; }
// Numerically stable and non-blowuppy way to calculate
// sqrt(a*a+b*b)
template<typename T>
T pythag(T a, T b) {
T absA = fabs(a);
T absB = fabs(b);
if (absA > absB)
{
return absA*sqrt(1.0 + fsqr(absB / absA));
} else if (0 == absB) {
return 0;
} else {
return absB*sqrt(1.0 + fsqr(absA / absB));
}
}
int main () {
double e, r, f, l, c, ans;
const double PI = 3.14159265358972384626433832795028841971693993751058209749445923078164062862089986280348253421170;
cout << "Insert value for resistance: " << endl;
cin >> r ;
cout << "Insert value for frequency: " << endl;
cin >> f;
cout << "Insert value for capacitance: " << endl;
cin >> c;
cout << "Insert value for inductance: " << endl;
cin >> l;
cout << "Insert value for electromotive force (voltage): " << endl;
cin >> e;
ans = e / pythag(r, 2*PI*f*l - (1/(2*PI*f*c)) );
cout << "I = " << ans << "A" << endl;
system("pause");
return 0;
}
Just kidding about all that PI.
The main problem is multiplying ½ by πfC instead of dividing, here:
(1.0 / 2.0 * PI*f*c)
This sort of problem is best avoided by using suitable named values (that also allows you to use faster and more precise x*x instead of std::pow(x,2)).
You can also remove some of that arithmetic by using the standard hypotenuse function instead of squaring and sqrting inline:
double ans = e / std::hypot(r, (2*PI*f*l - 0.5/PI/f/c));
#include <iostream>
#include <cmath>
int main()
{
static constexpr double PI = 4 * std::atan(1);
double r = 15; // ohm
double f = 200; // hertz
double c = 0.0001; // farad
double l = 0.01476; // henry
double e = 15; // volt
double current = e / std::hypot(r, (2 * PI*f*l - 0.5/PI/f/c));
std::cout << "I = " << current << "A" << std::endl;
}