#include <stdio.h>
#include <math.h>

int main() {

    double g = 9.81;
    double c = 0.25;

    double v0 = 0.0;
    double y0 = 0.0;

    double tempo_total = 10.0;

    double intervalos[] = {0.5, 0.1, 0.01};

    int quantidade = 3;

    for (int j = 0; j < quantidade; j++) {

        double dt = intervalos[j];

        double v = v0;
        double y = y0;

        int passos = (int)(tempo_total / dt + 0.5);

        char nome[50];

        sprintf(nome, "resultado_%.2f.csv", dt);

        FILE *arquivo = fopen(nome, "w");

        if (arquivo == NULL) {
            printf("Erro ao criar arquivo!\n");
            return 1;
        }

        fprintf(arquivo,
        "Tempo,Velocidade_Euler,Posicao_Euler,"
        "Velocidade_Analitica,Posicao_Analitica,"
        "Erro_Velocidade,Erro_Posicao\n");

        for (int i = 0; i <= passos; i++) {

            double t = i * dt;

            double v_exata =
                (g / c) * (1 - exp(-c * t));

            double y_exata =
                (g / c) * t -
                (g / (c * c)) * (1 - exp(-c * t));

            double erro_v = fabs(v - v_exata);
            double erro_y = fabs(y - y_exata);

            fprintf(arquivo,
            "%.4f,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f\n",
            t, v, y, v_exata, y_exata, erro_v, erro_y);

            double v_novo = v + dt * (g - c * v);
            double y_novo = y + dt * v;

            v = v_novo;
            y = y_novo;
        }

        fclose(arquivo);

        printf("Simulacao concluida: dt = %.2f\n", dt);
    }

    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: