Вот что получилось...
Код:
/**Dano n tochek. Postroit' mnogochlen prohodyashiy cherez dannie tochki.
(Postroenie interpolyacionnogo mnogochlena v yavnom vide. Metod Lagranja)
**/
#include <stdio.h>
#include <time.h>
#include <graphics.h>
#define N 10
int driver=DETECT, mode;
int n, xl=80, xr=620, yl=20, yr=430;
double a, b, max=-100000, min=100000;
double f(double *x, double *y, double X);
void OXY(double a, double b, double M, double m);
void tochki(double *x, double *y, double kx, double ky, double x0, double y0);
void graphic(double kx, double ky, double x0, double y0, double *x, double *y);
int main()
{
int i;
double j, Y, kx, ky, x0, y0, m, M;
printf ("Vvedite kol-vo tochek :");
scanf ("%d", &n);
double x[n], y[n];
srand (time(NULL));
for (i=0; i<n; i++)
{
x[i]=1+rand()%400;
y[i]=1+rand()%400;
}
m=x[0];
M=x[0];
for (i=0; i<n; i++)
{
if (M<x[i]) M=x[i];
if (m>x[i]) m=x[i];
}
a=m; b=M;
initgraph(&driver, &mode, "");
for (j=a; j<b; j=j+0.1)
{
if (f(x, y, j)>max) max=f(x, y, j);
if (f(x, y, j)<min) min=f(x, y, j);
}
kx=(xr-xl)/(b-a);
ky=(yr-yl)/(max-min);
x0=xl-a*kx;
y0=yr+min*ky;
OXY(a, b, max, min);
graphic(kx, ky, x0, y0, x, y);
tochki(x, y, kx, ky, x0, y0);
getch();
closegraph();
return 0;
}
double f(double *x, double *y, double X)
{
int i, j;
double B, L=0;
for(j=0; j<n; j++)
{
B=y[j];
for(i=0; i<n; i++)
{
if(i!=j) B=B*(X - x[i])/(x[j] - x[i]);
}
L+=B;
}
return L;
}
void OXY(double a, double b, double M, double m)
{
int i;
double x, y;
char h[10];
setcolor (9);
rectangle (xl, yl, xr, yr);
setcolor (7);
for(i=0; i<=N; i++)
{
x=a+i*(b-a)/N;
sprintf(h,"%.1f",x);
outtextxy(xl+i*(xr-xl)/N-10,yr+10,h);
y=M-i*(M-m)/N;
sprintf(h,"%.1f",y);
outtextxy(xl-70,yl+i*(yr-yl)/N-5,h);
}
}
void tochki(double *x, double *y, double kx, double ky, double x0, double y0)
{
double tx, ty;
setcolor (5);
for (int i=0; i<n; i++)
{
tx=x0+x[i]*kx;
ty=y0-f(x, y, x[i])*ky;
circle (tx, ty, 3);
circle (tx, ty, 2);
circle (tx, ty, 1);
}
}
void graphic(double kx, double ky, double x0, double y0, double *x, double *y)
{
double xm, ym;
for (double j=a; j<b; j=j+0.01)
{
xm=x0+j*kx;
ym=y0-f(x, y, j)*ky;
putpixel(xm,ym,7);
}
}