Перейти из форума на сайт.

НовостиФайловые архивы
ПоискАктивные темыТоп лист
ПравилаКто в on-line?
Вход Забыли пароль? Первый раз на этом сайте? Регистрация
Компьютерный форум Ru.Board » Компьютеры » Прикладное программирование » Задачи по C/С++

Модерирует : ShIvADeSt

 Версия для печати • ПодписатьсяДобавить в закладки
На первую страницук этому сообщениюк последнему сообщению

Открыть новую тему     Написать ответ в эту тему

amorua

Full Member
Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору


Код:
 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
 
#define myPI 3.1415927
 
/*================================================*/
/* this function returns Lagrange interpolation at
   the point xx  if x are the nodes and y are the values in the nodes
*/
float lagrange(float xx, float* x, float* y, int N)
{
  float yy=0, tmp;
  int k,j;
   
  for(k=0;k<N;k++)
  {
     tmp=1;
     for(j=0;j<N;j++) if(j!=k) tmp = tmp* (xx-x[j])/(x[k]-x[j]);
     yy = yy + y[k]*tmp;
  };
   
  return yy;
};
/*================================================*/
/* the function to be interpolated */
float my_function(float x)
{ /* return this value f(x) = x^1.5 */
  return x*sqrt(x);
}
/*================================================*/
int main(void)
{
 
  int Npol=2; /* this is the power of the polynomial to use */
  int N;
   
  float *x; /* array of the chebyshev knots */
  float *y; /* array of the f(x)-values in the vortecies */
  float xx, yy;
   
  float a=0, b=1.0; /* interval of interpolation */
  float h=0.1;    /* step */
  int i;
   
  float tmp,err,maxerr;
   
  N=Npol+1; /* number of points is the power of the polynom + 1 */
  x = malloc(sizeof(float)*N);
  y = malloc(sizeof(float)*N);
   
   
   
  /* define the nodes and the function values in them */
  printf("function to interpolate:\nx-nodes y-values\n");
  for(i=0;i<N;i++)  
  { /*x[i]=a + (b-a)*(float)i/(float)(N-1);*/ /*equdistant nodes */
    /* Chebyshev nodes */
    x[i] =0.5*((b-a)*cos((float)(2*i+1)/(float)(2*N)*myPI)+a+b);
    y[i]=my_function(x[i]); /* these are the function values */
    printf("%f %f\n",x[i],y[i]);
  };
  printf("-----------------\n");
   
   
  maxerr=0;
  printf("Results:\n");
  printf("   x    \t   L(x) \t   f(x) \t   err\n");
  for(xx=a;xx<=b;xx=xx+h)
  {  
    yy=lagrange(xx,x,y,N); /*  Lagrange interpolation */
    tmp = my_function(xx);
    err=fabs(yy-tmp);
    if(err>maxerr) maxerr=err;
    printf("%f\t%f\t%f\t%f\n",xx,yy,tmp,err);
  } ;
   
  printf("Max error on [%f; %f] is %f\n",a,b,maxerr);
 
  free(x); free(y);
  return 0;
}
 


Всего записей: 429 | Зарегистр. 09-02-2004 | Отправлено: 16:01 26-10-2008 | Исправлено: amorua, 16:04 26-10-2008
Открыть новую тему     Написать ответ в эту тему

На первую страницук этому сообщениюк последнему сообщению

Компьютерный форум Ru.Board » Компьютеры » Прикладное программирование » Задачи по C/С++


Реклама на форуме Ru.Board.

Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
Modified by Ru.B0ard
© Ru.B0ard 2000-2024

BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

Рейтинг.ru