Write a C++ program to calculate velocity in 1-D. It is possible to approximate
ID: 663756 • Letter: W
Question
Write a C++ program to calculate velocity in 1-D.
It is possible to approximate double integrals (and higher dimensions) using the trapezoid method. The equations of motion are used frequently in games and frequently need to be integrated to find the player's velocity and position. Velocity is obtained by integrating acceleration. Position is obtained by integrating velocity. Given a 1-D function for acceleration (as a function of time), your program should compute the 1-D position assuming that the velocity at time t=0 is 0 and the position at t=0 is 0.
You will need to use a nested loop to accomplish this. In order to find the area underneath a position section, you will need the velocity at the start and end of that section to use the trapezoid method, but you don't have the function for velocity, only for acceleration. This means that you will need to use the trapezoid method with the acceleration function in order to obtain the velocity at the end of the section.
Include a header file Motion.h containg the following functions.
Example:
t0 = 0
t1 = 2
n = 100
answer will be: 16.0004
Explanation / Answer
I don't have the function between acceleration and velocity. This is a generic program
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
float f(float);
void main()
{float a,b,h,sum,x,trap;
int n,i,m;
cout<<"Enter the limits a & b & number of subintervals n ";
cin>>a>>b>>n;
h=(b-a)/n;
sum=0.0;
m=n-1;
for(i=1;i<=m;i++)
{x=a+i*h;
sum=sum+f(x);
}
trap=h*(f(a)+2.0*sum+f(b))/2.0;
cout<<"Value of integral with "<<n<<endl;
cout<<"Subintervals="<<trap;
getche();
}
float f(float x)
{float fun;
fun=(x);
return(fun);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.