How Can C++ Be Used to Solve Differential Equations?

AI Thread Summary
To solve the differential equation dv/dt = t using C++, the process involves separating variables and integrating both sides. The integration leads to the solution v = (1/2)t^2 + C, where C is a constant that can be set to zero for simplicity. Additionally, the position function s can be derived as s = (1/6)t^3 + Ct + D. The discussion also raises a question about whether the program should calculate s for a specific t or perform the integration itself. Clarification on the order of the differential equation is also mentioned, suggesting a need for understanding the problem's context.
watermint
Messages
5
Reaction score
0
Differential equation

I have an equation that my teacher asked me to solve it using computer programming. I am stuck at how to do because I just learned C++ for about 3 months. Can you help me write a solution to the following problem in C++?
\frac{dv}{dt}=t
Thank you
 
Last edited:
Physics news on Phys.org
Let the constant after integrating equal 0, please help me.
 
Seems to me you should break it down by it's parts. You have a dependent variable 'v', the derivative of the dependent variable 'dv', an independent variable 't', and the derivative of t 'dt' v, dv, and t could have coefficients. In this case, the coefficient for v is 0 and the coefficients for dv and t are 1.

You'll have to program each individual step in solving the problem. Separating the variables is no problem (multiply both sides by dt). You have to also program the integration. Both sides are simple anti-derivatives using the power law. That adds a variable for each side - the power associated with v and the power associated with t.

Your program should integrate both sides by adding one to the power and then dividing the result by the new power.

After separating the variables in your separable differential equation:

v^0 dv = t^1 dt

After integrating both sides, your equation becomes:

\frac{v^1}{1} = \frac{t^2}{2} + c

(If you're writing a program for this, it helps to write out everything that's happening, even the parts you automatically drop out when you're doing this by hand.)

Since c is assumed to be zero, you can thankfully disregard it. Your coefficients all have values that allow you to ignore them in this example, but you could actually handle other coefficients in your program without much trouble.
 
watermint said:
I have an equation that my teacher asked me to solve it using computer programming. I am stuck at how to do because I just learned C++ for about 3 months. Can you help me write a solution to the following problem in C++?
\frac{dv}{dt}=t
The solution is:

v = \frac{ds}{dt} = \frac{1}{2}t^2 + C[/itex]<br /> <br /> s = \frac{1}{6}t^3 + Ct + D[/itex]&lt;br /&gt; &lt;br /&gt; Do you want the program to calculate s for a given t or do you want it to solve the differential equation (ie do an integration)?&lt;br /&gt; &lt;br /&gt; AM
 
This question doesn't make sense to me, unless the teacher is asking him to perform a numerical integration... you should remember how to compute an integral numerically from your Calc II course...
 
Andrew Mason said:
The solution is:

v = \frac{ds}{dt} = \frac{1}{2}t^2 + C[/itex]<br /> <br /> s = \frac{1}{6}t^3 + Ct + D[/itex]&lt;br /&gt; &lt;br /&gt; Do you want the program to calculate s for a given t or do you want it to solve the differential equation (ie do an integration)?&lt;br /&gt; &lt;br /&gt; AM
&lt;br /&gt; &lt;br /&gt; Why did you assume it to be a second order DE?
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...
Back
Top