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?
 
Thread 'Voltmeter readings for this circuit with switches'
TL;DR Summary: I would like to know the voltmeter readings on the two resistors separately in the picture in the following cases , When one of the keys is closed When both of them are opened (Knowing that the battery has negligible internal resistance) My thoughts for the first case , one of them must be 12 volt while the other is 0 The second case we'll I think both voltmeter readings should be 12 volt since they are both parallel to the battery and they involve the key within what the...
Thread 'Correct statement about a reservoir with an outlet pipe'
The answer to this question is statements (ii) and (iv) are correct. (i) This is FALSE because the speed of water in the tap is greater than speed at the water surface (ii) I don't even understand this statement. What does the "seal" part have to do with water flowing out? Won't the water still flow out through the tap until the tank is empty whether the reservoir is sealed or not? (iii) In my opinion, this statement would be correct. Increasing the gravitational potential energy of the...
Back
Top