- #1
Menninger
- 6
- 0
Homework Statement
The Second Order Differential Equation is:
x''-u(b^2 + x^2)x'+x=0
Initial Conditions are:
x(0)=1
x'(0)=0
It is to be numerically solved for 0<=t<=500. The specific numerical method to be used isn't specified, but it must be programmed into c.
As a means to check the answer, the problem specifies that for b=1 and u<<b, the solution is approximately: (1) x(t)=2cos(t), y(t)=-2sin(t).
The Attempt at a Solution
I broke the second order equation up into two first order equations:
x'=x'
x''=u(b^2 + x^2)x'-x
Then I attempted to use Euler's Method to solve the system using the following algorithm:
(2)
x'(t+Δt)=x'+(u(b^2 + x^2)x'-x)*(Δt)
x(t+Δt)=x+x'*Δt
using the following code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char** argv) {
double t, s, y, i; /* t is the time up to which the equation is to be approximated for
* s is the number of steps to be used to calculate the approximation
* y is time length of each step
* i is time concurrent with the beginning of each step */
double x, x1, x1a, b, u, x4, x5; /* x is the value of x at the beginning each step
* x1 is the value the derivate of x at each step
* x1a is a placeholder that holds onto to the value of x1
* at the end of each step until the initial value has been
* used in the calculation of the x value at the end of the step */
x=1;
x1=0;
printf("Enter the initial value of b: \n");
scanf("%lf",&b); /* Here the user defines the initial value of b */
printf("Enter the initial value of u: \n");
scanf("%lf",&u);/* Here the user defines the initial value of u */
printf("Enter the time up to which you wish to approximate the solution of the differential equation \n");
scanf("%lf",&t); /* Here the user defines the time up to which a solution is to be approximated */
printf("%f \n", t);
printf("Enter the number of steps you wish to use to produce the approximation: \n");
scanf("%lf",&s); /* Here the user defines the number of steps which are be used to produce the approximation*/
printf("%f \n", s);
y=t/s; /* Here the user defines the temporal duration of each step */
printf("%f \n", y);
printf("First Derivative: %f Value of Function: %f\n", x1, x);
for (i=0; i<=t; i=i+y) { /* This calculates the time at the beginning of each step,
* and it terminates the loop when the final time is reached */
printf("Time: %f First Derivative: %f %f Value of Function: %f %f\n", i, x1, x4, x, x5); /* This provides the approximated position x and its derivative at the beginning and end of each step */
x5=2*cos(i);
x4=-2*sin(i);
x1a=x1+(u*(pow(b, 2)-pow(x, 2))*x1-x)*y; /* This approximates the value of the derivative of x at the beginning of the next step */
x=x+x1*y; /* This approximates the a value of x at the beginning of the next step */
x1=x1a; /* This initializes the new derivate of x for the next step */
}
return(EXIT_SUCCESS);
}
In my program (1) & (2) match in terms of periodicity, but (2) spirals outward.
Am I using the wrong technique? Do I need something more precise? And does my code accurately implement my algorithm?