This is the updated version of Runge Kutta (RK4) 2nd order DE in C++ ERROR CODE
I am still experiencing difficulties with the code. Maybe this has to do with my limited knowledge of Runge-Kutta but when I run this code it doesn't produce an output.
#include <iostream>
#include <cmath>
//dvdt=-(g/L)*sin(theta)
//v=dxdt
double dxdt( double timepassed, double theta )
{
return theta/timepassed;
}
double L;
double g=9.8;
double coeff=-1*(g/L);
double dvdt( double timepassed, double x, double v)
{
return coeff*sin(x);
}
int main(){
// Standard Variables
double theta;
double theta1;
double h = 0.1;
double L;
double timepassed;
double time1;
// Input and Output Printing
std::cout << "Please input initial angle (in decimal radians), length of the pendulum (in meters) and the time desired (in seconds). Click ENTER key after each value."<<"\n";
std::cin >> theta1;
std::cin >> L;
std::cin >> timepassed;
// Specific Variable Declarations
double coeff=-1*(g/L);
double v = dxdt(theta1, timepassed);
double x = theta1;
double d2xdt2 = dvdt(timepassed, theta1, v);
// Defining K Values in Runge Kutta
double kx1,kv1;
double kx2, kv2;
double kx3, kv3;
double kx4, kv4;
double dt;
kx1=dt*dxdt(timepassed,x);
kv1=dt*dvdt(timepassed,x,v);
kx2=dt*dxdt(timepassed+dt/2,x+kx1/2);
kv2=dt*dvdt(timepassed+dt/2,x+kx1/2,v+kv1/2);
kx3=dt*dxdt(timepassed+dt/2,x+kx2/2);
kv3=dt*dvdt(timepassed+dt/2,x+kx2/2,v+kv2/2);
kx4=dt*dxdt(timepassed+dt,x+kx3);
kv4=dt*dvdt(timepassed+dt,x+kx3,v+kv3);
x = x + (1.0/6.0)*(kx1 + 2*kx2 + 2*kx3 + kx4);
v = v + (1.0/6.0)*(kx1 + 2*kv2 + 2*kv3 + kv4);
std::cout << "The angle is" << x; "\n";
std::cout << "The velocity is" << v;
}
Your system equations should be, as it is announced in the comment before it, as
//v=dx/dt
//dv/dt=d2x/dt2=-(g/L)*sin(x), where x=theta
double coeff;
double dxdt( double t, double x, double v) { return v; }
double dvdt( double t, double x, double v) { return coeff*sin(x); }
After the input of the parameters, the number coeff gets computed but not declared anew.
// Specific Variable Declarations
coeff = -(g/L);
Your step size appears to be 0.1. You need to decide what variable name to use, h or dt, and then stay with it.
It is almost certain that you need to perform more than one RK4 step, thus you need to frame them with a loop. What that loop contains in addition to the RK4 stages depends on what the output of the program is supposed to be. Also, you need to adapt the last step if the target time is not a multiple of the time step.
while(t < timepassed) {
kx1=dt*dxdt(t,x,v);
kv1=dt*dvdt(t,x,v);
kx2=dt*dxdt(t+dt/2,x+kx1/2,v+kv1/2);
kv2=dt*dvdt(t+dt/2,x+kx1/2,v+kv1/2);
kx3=dt*dxdt(t+dt/2,x+kx2/2,v+kv2/2);
kv3=dt*dvdt(t+dt/2,x+kx2/2,v+kv2/2);
kx4=dt*dxdt(t+dt,x+kx3,v+kv3);
kv4=dt*dvdt(t+dt,x+kx3,v+kv3);
t = t + dt;
x = x + (1.0/6.0)*(kx1 + 2*kx2 + 2*kx3 + kx4);
v = v + (1.0/6.0)*(kx1 + 2*kv2 + 2*kv3 + kv4);
// output for the step? Depending on the time?
}
Related
I spent quiet some time looking on the internet to find a solution to this, maybe it's out there but nothing of what I saw helped me.
I have a function !
double integrand(double r, double phi, double theta)
That I want to integrate with some given definite bounds over the three dimensions. I found multiple lines of code on the internet that implement single variable definite integrals numerical schemes. I was thinking to myself "well, I'll just integrate along one dimension after the other".
Algorithmically speaking what I wanted to do was :
double firstIntegral(double r, double phi) {
double result = integrationFunction(integrand,lower_bound,upper_bound);
return result;
}
And simply do it again two more times. This works easily in languages like Matlab where I can create functions handler anywhere but I don't know how to do it in C++. I would have to first define a function that some r and phi will calculate integrand(r, phi, theta) for any theta and make it in C++ a function of one variable only but I don't know how to do that.
How can I compute the triple integral of my three-variables function in C++ using a one -dimensional integration routine (or anything else really...) ?
This is a very slow and inexact version for integrals over cartesian coordinates, which should work with C++11.
It is using std::function and lambdas to implement the numerical integration. No steps have been taken to optimize this.
A template based solution could be much faster (by several orders of magnitude) than this, because it may allow the compiler to inline and simplify some of the code.
#include<functional>
#include<iostream>
static double integrand(double /*x*/, double y, double /*z*/)
{
return y;
}
double integrate_1d(std::function<double(double)> const &func, double lower, double upper)
{
static const double increment = 0.001;
double integral = 0.0;
for(double x = lower; x < upper; x+=increment) {
integral += func(x) * increment;
}
return integral;
}
double integrate_2d(std::function<double(double, double)> const &func, double lower1, double upper1, double lower2, double upper2)
{
static const double increment = 0.001;
double integral = 0.0;
for(double x = lower2; x < upper2; x+=increment) {
auto func_x = [=](double y){ return func(x, y);};
integral += integrate_1d(func_x, lower1, upper1) * increment;
}
return integral;
}
double integrate_3d(std::function<double(double, double, double)> const &func,
double lower1, double upper1,
double lower2, double upper2,
double lower3, double upper3)
{
static const double increment = 0.001;
double integral = 0.0;
for(double x = lower3; x < upper3; x+=increment) {
auto func_x = [=](double y, double z){ return func(x, y, z);};
integral += integrate_2d(func_x, lower1, upper1, lower2, upper2) * increment;
}
return integral;
}
int main()
{
double integral = integrate_3d(integrand, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
std::cout << "Triple integral: " << integral << std::endl;
return 0;
}
You can use functors
#include <iostream>
struct MyFunctorMultiply
{
double m_coeff;
MyFunctorMultiply(double coeff)
{
m_coeff = coeff;
}
double operator()(double value)
{
return m_coeff * value;
}
};
struct MyFunctorAdd
{
double m_a;
MyFunctorAdd(double a)
{
m_a = a;
}
double operator()(double value)
{
return m_a + value;
}
};
template<class t_functor>
double calculate(t_functor functor, double value, double other_param)
{
return functor(value) - other_param;
}
int main()
{
MyFunctorMultiply multiply2(2.);
MyFunctorAdd add3(3.);
double result_a = calculate(multiply2, 4, 1); // should obtain 4 * 2 - 1 = 7
double result_b = calculate(add3, 5, 6); // should obtain 5 + 3 - 6 = 2
std::cout << result_a << std::endl;
std::cout << result_b << std::endl;
}
If your concern is just about getting the right prototype to pass to the integration function, you can very well use alternative data passing mechanisms, the simpler of which is using global variables.
Assuming that the order of integration is on theta, then phi, then r, write three functions of a single argument:
It(theta) computes the integrand from the argument theta passed explicitly and the global phi and r.
Ip(phi) computes the bounds on theta from the argument phi passed explicitly and the global r; it also copies the phi argument to the global variable and invokes integrationFunction(It, lower_t, upper_t).
Ir(r) computes the bounds on phi from the argument r passed explicitly; it also copies the r argument to the global variable and invokes integrationFunction(Ip, lower_p, upper_p).
Now you are ready to call integrationFunction(Ir, lower_r, upper_r).
It may also be that integrationFunction supports a "context" argument where you can store what you want.
I am currently working on an assignment that requires me to compile and executie a program with the following parameters:
Write a program that does the following: Calculates the Velocity and Momentum of an object. The formula for the velocity is V=d/t and the formula for Momentum is m=mass*velocity. Your program should consist of two functions: Passing By Values (one), One Passing By Pointers (one). It should also have a for loop and necessary print statements to print the result in a tabular format.
· The Passing By Values function is to calculate the velocity of the object, where you pass two parameters to this function a constant distance, but the time is the value of the for loop: I=1:
double Velocity(double distance,int time);
· The Pass By Pointers function calculates the momentum of the object, where you pass two parameters to this function: The Velocity and a constant mass of an object: mass=100:
double Momentum(double *Velocity,double *mass);
The output should have a tabular format consisting of Time, Velocity, and Momentum. There is no need for a user to enter a value, and the time input should range from 1-200.
** Now here is my struggle, I have put together as much as I can but can't seem to have it compile properly , it just keeps going to "Press any button to continue..."
I truly on't understand what I am doing wrong and just need help to compile and run, any help at all would be appreciated .
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
//Function prototypes (declaring the functions).
double velocity(double distance, int time);
double momentum(double *velocity ,double *mass);
//Main function.
int main()
{
double PrimaryVelo(0);
double TotalMomentum(0);
int t(1);
for (double d = 1; d <= 10; d++)
{
PrimaryVelo = velocity(d, t);
} //End for the primary for loop.
system("pause"); //Prevents closing of debug automatically.
return 0;
} //End of the main function.
//Pass-by-value method for velocity.
double velocity(double distance, int time)
{
return distance / time;
}
//Pass-by-pointers method for momentum.
double momentum(double &velocity ,double &mass)
{
return (velocity * 205);
}
Well....
Here's your code.....
#include <iostream>
using namespace std;
double velocity(double distance, int time);
double momentum(double velocity ,double mass);
int main()
{
double Mass=205;
double Distance=100;
double Velocity;
//cout << "Time\t\tVelocity\tMomentum" << endl; //You'll need to reformat this
for ( int Time = 1 ; Time <= 200 ; Time++ )
{
cout << Time << "\t" ;
cout << velocity ( Distance , Time ) << "\t" ;
Velocity = velocity ( Distance , Time ) ;
cout << momentum ( Velocity , Mass ) << endl ;
}
// If you can't use conio.h and getch();
// Use something like this to stop console from closing itself
int a;
cin>>a;
return 0;
}
double velocity(double distance, int time)
{
return distance / time;
}
double momentum(double velocity ,double mass)
{
return velocity * mass;
}
Note : I've given you code with functions accepting arguments passed by values......
I hope it helps....
Have a nice day....
Would you please help me with my problem. I'm trying to write a program where objects in different class interact. I base my model on the gsl library to resolve my ordinary differential equations (ode).
Instances of different classes consume each other, so they get or loose mass when they eat or are eaten.
My 2 ode are like b[i] = sum of consumption - sum of consumed. I calculate consumption as follow, with 1 being the consumer and 0, the prey.
conso[1] = y[0] * x[1] * y[1]
The original code comes from "sample code using gsl routines" as follow with modifications. The instances of class A and B are included in maps of class C.
I'm new to c++ and I don't see how I can integrate a loop in the ode to make it calculate what has been consumed by each instance, and remove it from the other instances that have been eaten. The best would be to use functions and variables that are in the istances class but the function where the ode are has to be static. I thought about array but it seems that arrays can't be parameters of the odes.
The other problem is that for each instance, the parameters has to change and be taken in the class, but how I did here, in the loop with the iterators, the ode will work for the last instance... Again I would have like to use an array of class C that take the parameter of the instances in class A and B, but because the function is static I can't use the array of the class "C". I'm in real trouble !
Do you have any ideas ? Thank you !
// include files
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv.h>
// ************************* C.h ****************************
class C
{
public :
C(std::map(std::map<std::string,A> &A, std::map<std::string,B> &B);
~C();
int integ(void);
int static rhs (double t, const double y[], double f[], void *params_ptr);
private :
std::map<std::string,A> &A,
std::map<std::string,B> &B,
};
//*************************** C.cpp ****************************
int C::integ (void)
{
int dimension = 2; // number of differential equations
double eps_abs = 1.e-8; // absolute error requested
double eps_rel = 1.e-10; // relative error requested
// define the type of routine for making steps:
const gsl_odeiv_step_type *type_ptr = gsl_odeiv_step_rkf45;
// allocate/initialize the stepper, the control function, and the
// evolution function.
gsl_odeiv_step *step_ptr = gsl_odeiv_step_alloc (type_ptr, dimension);
gsl_odeiv_control *control_ptr = gsl_odeiv_control_y_new (eps_abs, eps_rel);
gsl_odeiv_evolve *evolve_ptr = gsl_odeiv_evolve_alloc (dimension);
gsl_odeiv_system my_system; // structure with the rhs function, etc.
// parameter for the diffeq
for(itA=A.begin(); itA!=A.end(); ++itA)
{
x = itA->second.getx();
my_system.params = &x; // parameters to pass to rhs
}
for(itB=B.begin(); itB!=B.end(); ++itB)
{
z = itB->second.getz();
my_system.params = &z; // parameters to pass to rhs
}
double y[2]; // current solution vector
double t, t_next; // current and next independent variable
double tmin, tmax, delta_t; // range of t and step size for output
double h = 1e-6; // starting step size for ode solver
// load values into the my_system structure
my_system.function = rhs; // the right-hand-side functions dy[i]/dt
my_system.dimension = dimension; // number of diffeq's
tmin = 0.; // starting t value
tmax = 100.; // final t value
delta_t = 1.;
for(itA=A.begin(); itA!=A.end(); ++itA)
{
y[0] = itA->second.getY(); // initial y value
}
for(itB=B.begin(); itB!=B.end(); ++itB)
{
y[1] = itB->second.getY(); // initial y value
}
t = tmin; // initialize t
// print initial values
cout << scientific << setprecision (5) << setw (12) << t << " "
<< setw (12) << y[0] << " " << setw (12) << y[1] << endl;
// step to tmax from tmin
for (t_next = tmin + delta_t; t_next <= tmax; t_next += delta_t)
{
while (t < t_next) // evolve from t to t_next
{
gsl_odeiv_evolve_apply (evolve_ptr, control_ptr, step_ptr,
&my_system, &t, t_next, &h, y);
}
// print at t = t_next
cout << scientific << setprecision (5) << setw (12) << t << " "
<< setw (12) << y[0] << " " << setw (12) << y[1] << endl;
}
// all done; free up the gsl_odeiv stuff
gsl_odeiv_evolve_free (evolve_ptr);
gsl_odeiv_control_free (control_ptr);
gsl_odeiv_step_free (step_ptr);
return 0;
}
//*************************** rhs ****************************
int C :: rhs (double , const double y[], double f[], void *params_ptr)
{
// get parameter(s) from params_ptr; here, just a double
double x = *(double *) params_ptr;
double z = *(double *) params_ptr;
// evaluate the right-hand-side functions at t
f[1] = x * y[1] + sum consumption - sum consumed; //objects of class A
f[0] = - z * y[0] - sum consumed; //objects of class B
return GSL_SUCCESS; // GSL_SUCCESS defined in gsl/errno.h as 0
}
I tried to add consumption, but it says "error: 'void' must be the first and only parameter if
specified" :
int C :: rhs (double , const double y[], double f[], void *params_ptr, void conso)
{
// get parameter(s) from params_ptr; here, just a double
double x = *(double *) params_ptr;
double z = *(double *) params_ptr;
// evaluate the right-hand-side functions at t
f[0] = z * y[0]; //objects of class A
f[1] = - x * y[1] + conso; //objects of class B
return GSL_SUCCESS; // GSL_SUCCESS defined in gsl/errno.h as 0
}
//***And in int C :: integ :
for(itA=A.begin(); itA!=A.end(); ++itA)
{
for(itB=B.begin(); itB!=B.end(); ++itB)
{
void conso = itA->second.conso(itB->second, x);
}
}
//***With in class A :
void A :: conso(Group &prey, double x)
{
double conso=0;
conso = x * y * prey.gety();
}
I am trying to write a block of codes in C++ that calculates sinX value with Taylor's series.
#include <iostream>
using namespace std;
// exp example
#include <cstdio> // printf
#include <cmath> // exp
double toRadians(double angdeg) //convert to radians to degree
{ //x is in radians
const double PI = 3.14159265358979323846;
return angdeg / 180.0 * PI;
}
double fact(double x) //factorial function
{ //Simply calculates factorial for denominator
if(x==0 || x==1)
return 1;
else
x * fact(x - 1);
}
double mySin(double x) //mySin function
{
double sum = 0.0;
for(int i = 0; i < 9; i++)
{
double top = pow(-1, i) * pow(x, 2 * i + 1); //calculation for nominator
double bottom = fact(2 * i + 1); //calculation for denominator
sum = sum + top / bottom; //1 - x^2/2! + x^4/4! - x^6/6!
}
return sum;
}
int main()
{
double param = 45, result;
result = mySin(toRadians(param)); //This is my sin value
cout << "Here is my homemade sin : " << result << endl;
result = sin(param); //This is library value
cout << "Here is the API sin : " << result << endl;
return 0;
}
So my program works without any error. My output is exactly:
Here is my homemade sin : nan
Here is the API sin:0.850904
I know I am making a big logic mistake but I couldn't find it out. It is my second week with C++. I am more familiar with Java. I coded the same thing and It worked absolutely perfect. The answers matched each other.
Thanks for your time and attention!
in fact, you miss the return: x*fact(x-1); should be return x*fact(x-1);. You can see the compiler complaining if you turn the warnings on. For example, with GCC, calling g++ -Wall program.cpp gives Warning: control reaches end of non-void function for the factorial function.
The API sin also needs the angle in radians, so change result=sin(param); into result=sin(toRadians(param));. Generally, if in doubt about the API, consult the docs, like here.
Your codes seems to have some logical mistakes. Here is my corrected one:
#include <iostream>
using namespace std;
double radians(double degrees) // converts degrees to radians
{
double radians;
double const pi = 3.14159265358979323846;
radians = (pi/180)*degrees;
return radians;
}
double factorial(int x) //calculates the factorial
{
double fact = 1;
for(; x >= 1 ; x--)
{
fact = x * fact;
}
return fact;
}
double power(double x,double n) //calculates the power of x
{
double output = 1;
while(n>0)
{
output =( x*output);
n--;
}
return output;
}
float sin(double radians) //value of sine by Taylors series
{
double a,b,c;
float result = 0;
for(int y=0 ; y!=9 ; y++)
{
a= power(-1,y);
b= power(radians,(2*y)+1);
c= factorial((2*y)+1);
result = result+ (a*b)/c;
}
return result;
}
double n,output;
int main()
{
cout<<"enter the value\t";
cin>>n;
n = radians(n);
cout<< "\nthe value in radians is\t"<< n << "\n";
output = sin(n);
cout<< "\nsine of the given value is\t"<< output;
return 0;
}
The intention of this program was to use custom functions instead of libraries to make learning for others easy.
There are four user defined functions in this program.The first three user defined functions 'radians()', 'factorial()','power()', are apparently simple functions that perform operations as their name suggests.
The fourth function 'sin()' takes input in radians given by the function 'radians()'. The sin function uses Taylors series iterated term wise in the function's 'for(int y= 0;y!=9;y++)' loop till nine iterations to calculate the output.The 'for()' loop iterates the general mathematical expression: Term(n)=((-1)^n).(x^(2n+1))/(2n+1)!
sin(x)= x- x^3/3! + x^5/5! -x^7/7! + x^9/9!
=x-x^3/2*3 (1- x^2/4*5 + x^4/4*5*6*7 + x^6/4*5*6*7*8*9)
=x - x^3/2*3 {1- x^2/4*5(1- x^2/6*7 + x^4/6*7*8*9)}
=x - x^3/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}]
=x(1 - x^2/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}])
double sin_series_recursion(double x, int n){
static double r=1;
if(n>1){
r=1-((x*x*r)/(n*(n-1)));
return sin_series_recursion(x,n-2);
}else return r*x;
}
Here's the code.
int a;
int pi = 3.14;
int area;
int main()
{
cout << "Input the radius of the circle ";
cin >> a;
a *= a *= pi >> area;
cout << "The area is " << area;
}
The >> operator when used with numbers is right shift, not assignment. You want something like
area = a * a * pi;
Update
You also need to use a floating point type or your answer won't be what you expect.
float a;
float pi = 3.14f;
float area;
I don't have enough patience to decipher your strange code. How about just area = a * a * pi?
Your code doesn't make any sense.
pi(and all your other variables) need to be double or float,... not int. An int can only contain an integral number. And pi is obviously not integral.
a *= a *= pi >> area; should be area = a * a * pi;
>> is a bitshift, not an assignment to the right side
*= is multiply assign and not just multiply. i.e. it is similar to left=left*right
The area of a circle is pi * r * r therefore you would want to do;
a = a * a * pi
Hope that helps
and they all would need to be floats.
Your code doesn't do what I think you wanted it to do. You don't assign to variables with >>; that is only for stream extraction (and bitshifting).
Also, a *= a *= pi probably doesn't do what you think it does.
Also, you want floating-point values, not int. An "int" pi is just 3.
Also, you should have error checking on your stream extraction!
Try:
int main()
{
const float pi = 3.14;
float a;
cout << "Input the radius of the circle ";
if (!(cin >> a)) {
cout << "Invalid radius!";
return 1;
}
float area = (a * a * pi);
cout << "The area is " << area;
}
int pi = 3.14;
Wrong datatype. Assigning double value to int? That's wrong.
Write this:
double pi = 3.14;
And likewise, change other datatypes to double as well.
Because you're using int, or integer, for all your variables. You want to use doubles or even floats. (doubles are more precise).
All your variables are declared as int, which simply drops any fractional portion assigned to it. To work with floating-point values, use double instead.
Also, your equation in almost incomprehensible. Not sure what you're trying to do there.