eGospodarka.pl
eGospodarka.pl poleca

eGospodarka.plGrupypl.comp.programmingSimpson vs. Niski Cotes › Simpson vs. Niski Cotes
  • Path: news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.
    atman.pl!newsfeed.neostrada.pl!unt-exc-01.news.neostrada.pl!unt-spo-a-01.news.n
    eostrada.pl!news.neostrada.pl.POSTED!not-for-mail
    From: "slawek" <s...@h...pl>
    Newsgroups: pl.comp.programming
    Subject: Simpson vs. Niski Cotes
    Date: Sun, 11 Nov 2012 00:28:01 +0100
    MIME-Version: 1.0
    Content-Type: text/plain; format=flowed; charset="iso-8859-2"; reply-type=original
    Content-Transfer-Encoding: 8bit
    X-Priority: 3
    X-MSMail-Priority: Normal
    Importance: Normal
    X-Newsreader: Microsoft Windows Live Mail 14.0.8117.416
    X-MimeOLE: Produced By Microsoft MimeOLE V14.0.8117.416
    Lines: 79
    Message-ID: <509ee300$0$26682$65785112@news.neostrada.pl>
    Organization: Telekomunikacja Polska
    NNTP-Posting-Host: 62.69.202.124
    X-Trace: 1352590080 unt-rea-a-01.news.neostrada.pl 26682 62.69.202.124:65367
    X-Complaints-To: a...@n...neostrada.pl
    Xref: news-archive.icm.edu.pl pl.comp.programming:200706
    [ ukryj nagłówki ]

    W kontekście rozmaitych forumowych "specjalistów" od metod numerycznych -
    zabawne, jak tym razem będą wyjaśniać trywialny w istocie rezultat...

    exact 0.5022749400837604
    tapez 0.5022749194207212
    simpson 0.5022738634614975

    /*
    * tq.c
    * 2012 (C) slawek
    */

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

    #define NPTS 10000 /* NPTS must be even! */

    double x[NPTS+1];
    double y[NPTS+1];

    double f(double x)
    {
    return sin(x)*exp(-x);
    }

    double integrate_exact(double a, double b)
    {
    return ((cos(a) + sin(a))*exp(-a) - (cos(b) + sin(b))*exp(-b))/2.0;
    }

    double generate_points(double x[], double y[], double a, double b, int n)
    {
    int i;

    for(i = 1; i <= n; i++)
    {
    x[i] = a + (i-1) * (b - a)/(n - 1);
    y[i] = f(x[i]);
    }
    }

    double integrate_trapez(double x[], double y[], int n)
    {
    int i;
    double s = 0.;

    for(i = 2; i <= n; i++)
    s += (y[i] + y[i-1]) * (x[i] - x[i-1]) * 0.5; // an naive approach

    return s;
    }

    double integrate_simpson(double x[], double y[], int n)
    {
    int i;
    double odd = 0., even = 0.;

    for(i = 1 ; i < n ; i+=2) odd += y[i];
    for(i = 2 ; i <= n ; i+=2) even += y[i];

    return (2.0*odd-y[1]-y[n] + 4.0*even)*(x[3]-x[1])/6.0;
    }

    int main(int argc, char* argv[])
    {
    const double a = 0.;
    const double b = 5.;

    generate_points(x,y,a,b,NPTS);
    printf("exact %.16lg\n", integrate_exact(a,b));
    printf("tapez %.16lg\n", integrate_trapez(x,y,NPTS));
    printf("simpson %.16lg\n", integrate_simpson(x,y,NPTS));

    puts("press enter...");
    getchar();
    return 0;
    }


Podziel się

Poleć ten post znajomemu poleć

Wydrukuj ten post drukuj


Następne wpisy z tego wątku

Najnowsze wątki z tej grupy


Najnowsze wątki

Szukaj w grupach

Eksperci egospodarka.pl

1 1 1

Wpisz nazwę miasta, dla którego chcesz znaleźć jednostkę ZUS.

Wzory dokumentów

Bezpłatne wzory dokumentów i formularzy.
Wyszukaj i pobierz za darmo: