Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My code is producing an output of nan.
I have looked around and I'm guessing it is as the equation is complicated from what I gather C++ doesn't recieve complicated equations too well.
But that doesnt seem right.
Here's my code:
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int Backfill;
double SlopeAngleOfWall, AngleOfInternalFriction, AngleOfFrictionSoilAndWall, BackfillSlope, CoefficientOfActivePressure;
cout << "Retaining Wall Calculator \n";
cout << "Enter the slope angle of the wall, this is measured from the horizontal plane, therefor will be 90 degrees if the retaining wall is vertical \n";
cin >> SlopeAngleOfWall;
cout << "Enter the angle of internal friction \n";
cin >> AngleOfInternalFriction;
cout << "Enter the angle of friction between the soil and the wall \n";
cin >> AngleOfFrictionSoilAndWall;
cout << "Enter the angle of the backfill slope \n";
cin >> BackfillSlope;
/* To make sin function work is is typed (angle*pi/180) */
/* To make sin square work is is typesd (pow(sin (angle*pi/180), 2.0) */
/* To add a square root sqrt is used */
CoefficientOfActivePressure = (pow (sin ((SlopeAngleOfWall + AngleOfInternalFriction)*pi/180), 2.0)) / ((pow (sin (SlopeAngleOfWall*pi/180), 2.0) * sin ((SlopeAngleOfWall * AngleOfFrictionSoilAndWall) * pi / 180)) *( 1 + sqrt( ( sin ((AngleOfInternalFriction + AngleOfFrictionSoilAndWall) * pi / 180) * sin ( (AngleOfInternalFriction - BackfillSlope) * pi / 180) / sin ( (SlopeAngleOfWall - AngleOfFrictionSoilAndWall) * pi / 180 ) * sin ( (SlopeAngleOfWall + BackfillSlope) * pi / 180)))));
cout << "The coefficient of active pressure acting on the wall is" << CoefficientOfActivePressure <<"\n"
}
It looks like you are trying to implement Coulomb's Theory of Lateral earth pressure. The formula looks like this:
(From http://www.soilmanagementindia.com)
Assuming that your implementation is correct, the only way to get NaN as result is if the square-root argument is negative.
The bottom line is that the equation is not valid for all possible combinations of input, and for the wrong set of input an output of NaN is to be expected.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
R (in feet) = (velocity (in ft/s))^2 / g (which is 9.8 m/s^2) * sin(2theta).
I checked various inputs using websites like this one https://www.ajdesigner.com/phpprojectilemotion/range_equation.php#ajscroll to check if my code was giving the right answer but it isn't and I don't know why. Any simple solutions?? Here it is below:
#include <iostream>
#include <cmath>
using namespace std;
const double GRAVITY_MPS = 9.8;
const double METERS = 3.28084;
int main() {
double v = 0.0, degrees = 0.0;
cout << "Enter the velocity (mi/hr) and cannon angle (degrees): \n";
cin >> v >> degrees;
double v2 = 0.0;
v2 = v * 5280.0 / 3600.0;
double radians = degrees * M_PI / 180.0;
double r = (pow(v2,2.0) / GRAVITY_MPS) * sin((2.0 * radians));
double r2 = r * METERS;
cout << "Yikes travels " << r2 << " feet.";
return 0;
}
The value of r has units (ft/s)2 / (m/s2) == (ft2/s2) / (m/s2) == ft2 / m. Then r2 has units ft2/m * ft/m resulting in units of ft3 / m2.
You want the inverse of the multiplication factor for r2, i.e. m/ft:
double r2 = r * 0.3048; // meters / foot
Your v0 can't be in feet cause gravity is in meters.
Change 5280.0 to 1609.3 (mi to m) and it works.
How would I convert degrees to radians exactly. Rather than showing 0.785398 I want the program to show pi/4.
I am currently using this script to convert degrees into radians.
cout << angle * M_PI / 180.0 << " radians";
You might do
std::cout << angle / 180.0 << " pi radians";
Which will print
0.25 pi radians
You might be interested by how-to-convert-floats-to-human-readable-fractions to convert 0.25 into 1/4.
if (GoalWeight < 0.0) {
int weeks;
cout << "How many weeks do you plan to continue this trend?\n";
cin >> weeks;
double NewWeight = GoalWeight * weeks;
double NegBMI = (weight - NewWeight) * 703 / (pow(HeightConverter, 2));
cout << "If you complete your plan for " << weeks << "weeks you will have a new BMI of: \n" << NegBMI;
}
system("pause");
return 0;
}
Output result:
What is your current weight?: 180
What is your current height in inches?" 71
Your current BMI is: 25.10(Not part of output, but this is correct)
What is your goal weight change?(lbs) -1.5
How many weeks do you plan to continue this trend?: 6
If you complete your plan for 6 weeks you will have a new BMI of: 26.36
As you can tell this is wrong
The calculation for BMI is (weight * 703) /height^2(inches)
What it is doing for negative numbers is:
180 + 9(instead of 180 - 9) giving (191 * 703) / 71^2 yielding 26.36
Instead of:
180 - 9(giving 171 * 703) / 71^2 yielding the correct output of:23.84
I know you're all shaking your heads saying I must be an idiot, and rightfully so, I'm hoping someone can help me with this!
What is your goal weight change?(lbs) -1.5
How many weeks do you plan to continue this trend?: 6
6 * ( -1.5 ) == -9
180 - (-9) == 189
So you either input goal weight change as positive number or add it, not subtract.
Your newWeight is resulting to -9 because of your statement 6 * -1.5.If you want to subtract it just make the (weight + newWeight) rather than the -.
Do you believe that if you do (+NewWeight) the value of NewWeight becomes positive?
this is not the case:
Unary Plus Operator (+): The result of an operation on a numeric type is the value of the operand itself. This operator has been predefined for all numeric types.
As a solution use Reginalds idea and make (weight + newWeight) rather than the -.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
#include <iostream>
#include <math.h>
#include "boost/math/constants/constants.hpp"
const double pi = boost::math::constants::pi<double>();
int main() {
double x = 2;
double y = 1;
double angle = 90; //in degrees
double rad { angle * pi / 180 }; //converting to rads
std::cout << "rad: " << rad << std::endl; //1.5708 - OK
double c = cos( rad );
std::cout << "cos(rad): " << c << std::endl; //6.12303e-017 - Huh? Should be ~ -3.2051033e-9
double s = sin( rad );
std::cout << "sin(rad): " << s << std::endl; //1 - OK
x = x * c - y * s;
y = x * s + y * c;
std::cout << "(" << x << "," << y << ")" << std::endl;
return 0;
}
So this is weird, I'm trying to do a rotation but the cos is giving me the wrong value. It should be something near to -3.2051033e-9 but I end up with 6.12303e-017. I've converted my angle to radians and it checks out. sin( rad ) returns the correct value as well.
What gives?
Edit:
The code has an error but not related to the question. On y = x * s + y * c; the x should refer to the the value declared at the top but as the previous equation overrides that value the y at the end would be wrong.
Thanks for the clarification of exponents. I got a bit confused.
cos(90) (degrees) is approximately zero, as in 0.0. Worrying about getting a number back that is e-17 vs e-9 is not meaningful, they are both basically zero.
How close it the result actually is depends on how they represented pi, 90.0, 180.0, and the implementation of the cos function in math.h.
It should be something near to -3.2051033e-9 but I end up with 6.12303e-017.
I don't know why you think it "should be -3.2051033e-9" because cos(90º) is zero, and -0.0000000032051033 is well outside of the margin of error of your calculations,
The value you're getting, 0.0000000000000000612303 is zero, and therefore correct, within the margin of error of your calculations.
There is no error in your code..
In the cos function (cos ), return value is a real. And here is some examples;
(cos -1) returns 0.540302
(cos 0.0) returns 1.0
(cos (* pi 0.5)) returns 6.12303e-017 which is practically equal to **0**
And also you can check it with inverse cos with cos(6.12303e-017) and you will see it is 90 degree
Write a program that determines how far and for how long a time a rock will travel when you throw it off a cliff. Click here to copy the file toss.txt to your desktop (right click the file name and choose Save as). The file contains the height of the cliff in meters.
The program will then:
Open the file toss.txt and read the cliff height into a double-precision variable, then echo print the value of the cliff height to the screen with an appropriate label.
Ask the user for the angle at which the rock is thrown (90 degrees is straight up, and 0 degrees is straight forward), and the velocity at which the rock is thrown (in miles per hour).
Check to make sure the angle is greater than or equal to 0 and less than or equal to 90. If it is not, the program terminates and prints an appropriate error message to the screen.
Check to make sure the velocity is less than or equal to 100 mph and greater than or equal to 0 mph. If it is not, the program terminates and prints an appropriate error message to the screen.
If the angle and velocity are valid, the program completes the calculations as follows:
Converts miles per hour to meters per second.
Converts the angle to radians.
Calculates the time traveled using the following equations:
where
Calculates the distance traveled in the horizontal direction using:
Outputs the time and distance traveled in the horizontal direction to the screen with appropriate labels.
Prints an appropriate message telling the user if the distance traveled in the horizontal direction was greater than, less than, or equal to the height of the cliff.
/* This program */
using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
int readit ();
int calcit (double, double, double);
int main()
{
readit ();
system ("pause");
return 0;
}
int readit ()
{
double hite, angl, v;
ifstream datain ( "toss.txt" );
datain >> hite;
cout << "The cliff height is " << hite << " meters"<< endl;
cout << "Enter the angle in degrees (from horizontal) the rock is thrown: "
<< endl;
cin >> angl;
if (angl>=0 && angl<=90)
{
cout << endl << "The angle you have entered is "<<angl<< endl <<endl;
}
else
{
cout << "The angle you have entered is not acceptable" << endl;
return 0;
}
cout << "Enter the velocity in mph the rock is thrown: " << endl;
cin >> v;
if (v>=0 && v<=100)
{
cout << endl << "The velocity at which the rock is thrown is "<<v<<
" mph" << endl << endl;
}
else
{
cout << "The velocity you have entered is not acceptable" << endl;
return 0;
}
calcit (hite, angl, v);
}
int calcit (double hite, double angl, double v)
{
double tyme, dist;
v = v * (1609.344/3600);
angl = angl*(M_PI/180);
tyme = -v*sin(angl) + (sqrt((v*sin(angl)*v*sin(angl)) + 2*9.8*hite)/9.8) + (2*(v*sin(angl))/9.8);
dist = (tyme * v) * cos(angl);
cout << tyme << " " << dist <<endl;
}
I am trying to get the correct time the rock is traveling before it hits the ground but i keep getting incorrect answers. I am not sure if i am turning the equation to figure out the time the rock will be in the air until impact into c++ language right. any have any ideas??? i really need to finish this damn project.
Starting from the equation for the y (height above 0) for the rock we have
y = h + v*sin(a)*t - g/2*t^2
which transforms into
g/2 T^2 - v*sin(a)*T - h == 0
when we solve for the final condition y(T)=0.
This yields
T = v*sin(a)/g + sqrt(v*sin(a)*v*sin(a) + 2*g*h)/g
I just can't figure out where the first part -v*sin(angl) in your equation comes from. Everything else looks just fine. So it seems not to be with your code but with the equation you started.
The equation you want is:
s =ut + 1/2 at^2
s = Total distance traveled. (Height of the cliff)
u = Starting velocity (In your case negative as you are throwing
away from the target. And take into account
that not all the starting velocity is away
from the target (eg angle 0 mean u = 0))
a = acceleration (9.81 m/s2)
t = time (The value you want to calculate).
Rearrange the formula to solve for t
To find the solution for t where s = 0...
This formula is you basic quadratic:
y = a.x^2 + b.x + c
Where:
x/y are variables.
a/b/c are constants.
The solution for a quadratic equation where y is 0 is:
x = [ -b ± sqrt(b^2 - 4ac) ] / 2a
Notice the ± symbol. There are actually two solutions to the problem.
You should be able to deduce which one is correct for you as the other
is probably negative.
In your particular case the map is:
x ==> t
y ==> 0
a ==> 1/2.g
b ==> u
c ==> -s
I would suggest a few things to "clean up" the code a bit:
If functions return int ensure that they do really return something. (main doesn't have to but other functions do).
Calculate v * sin(ang1) once then use it in your formula thereafter. Not only more efficient but will make your code clearer.
Like you have given Pi a "constant", do that with other numbers you are using like 9.8 (gravitational force?)
If you have a confusing formula in the code, just introduce more variable names until the meaning becomes obvious. So long as you don't reassign different values to the same variables, this will not make the program confusing.
int calcit (double hite_meters, double angl_deg, double v_mph)
{
double const gravity = 9.8;
double v_ms = v_mph * (1609.344/3600);
double angl_rad = angl_deg * (M_PI/180);
double v_vertical = v_ms * sin( angl_rad );
double time_up = v_vertical / gravity; // [m/s] / [m/s^2] = [s]
double time_down_over_cliff = time_up;
// use quadratic formula t = ( -v - ( v^2 - 4gd )^1/2 ) / 2g:
double time_under_cliff = ( - v_vertical
- sqrt( ( v_vertical * v_vertical )
- ( 4 * - gravity * hite_meters ) ) // negative gravity = down
) / ( 2 * - gravity ); // ( [m/s] + ([m/s]^2 - [m/s^2]*[m])^1/2 ) / [m/s^2]
// = [m/s] / [m/s^2] = [s]
double time_total = time_up + time_down_over_cliff + time_under_cliff;
double v_horizontal = v_ms * cos( angl_rad );
double dist_horizontal = v_ms * time_total;
cout << time_total << " " << dist_horizontal <<endl;
}
Every line of code produces a new, relevant piece of information. When converting to a new unit, I introduce a new variable with a new name. Formulas involving more than one unit get the unit types explained in a comment. This should help turn up unit conversion errors which otherwise I can't help you catch.
Writing this kind of code involves more typing, but the time saved on head-scratching and asking for help more than makes up for it.
The program itself is not any less efficient. More importantly, it may be easily modified, so it won't turn into an inefficient mess after a few revisions.