You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.5 KiB

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void dglrhs(int neq, double w0, double gamma, double *x, double *f) // t muss nicht übergeben werden, nicht explizit zeitabh.
{ //für unsere DGL mit neq = 2
//sicherstellen wie in Vorlesung:(Brauch man das wirklich?
if (neq!= 2)
{
printf ("neq passt nicht.\n");
abort(); //Bedeutung?->Abbruch
}
//Speicherplatz muss zuvor bereitgestellt werden
f[0] = x[1];
f[1] = -w0*w0*x[0]-2*gamma*x[1];
}
void euler (int neq, double h, double w0, double gamma, double *x, double *f, void (*dglrhs) (int, double,
double, double*, double*))
{
int i;
(*dglrhs)(neq,w0,gamma,x,f); // Iformationen über rechte Seite holen
//Euler-Verfahren anwenden und x_n+1 berechnen
for (i = 0; i < neq; i++)
{
x[i] += h*f[i];
}
}
int main()
{
double h,t0,x0,v0,tend;
int neq = 2;
double exact, diff;
double *x, *f;
double t;
double w0, gamma;
printf("Bitte geben Sie h, t0, x0, v0 und tend, sowie omega0 und gamma ein:\n");
scanf("%le %le %le %le %le %le %le", &h, &t0, &x0, &v0, &tend, &w0, &gamma);
x = malloc(sizeof(double)*neq);
f = malloc(sizeof(double)*neq);
x[0] = x0;
x[1] = v0;
for (t = t0; t <= tend; t+=h)
{
printf("t= %20.5le ,dgl = %20.5le\n", t, x[0]);
euler (neq,h,w0,gamma,x,f,&(dglrhs));
}
free(x); free(f);
}