作业帮 > 综合 > 作业

采用逐段比较法直线插补曲线 y=5*x2+16*x-5, x∈[-5,5]

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/05/15 18:01:57
采用逐段比较法直线插补曲线 y=5*x2+16*x-5, x∈[-5,5]
麻烦把程序编出来 谢谢
采用逐段比较法直线插补曲线 y=5*x2+16*x-5, x∈[-5,5]
#include
#include
#include
#include
#include
#define SIZE 3000
float fun(float x);
float xchange(float x);
float ychange(float y);
void drawline(int color);
void drawchaline(int n,int color);
int chazhi(int *n,float deta);
const int left = 50;
const int right = 550;
const int top = 50;
const int bottom = 400;
float x[SIZE];
float x1[SIZE];
float fun(float x)
{
return 5*x*x+16*-5;
}
int main()
{
int GraphDriver;
int GraphMode;
int i;
int n = 2;
float deta = 0.0001;
char buffer[10] = {'\0'};
char szfilename[255]= {"c:\\text1.txt"};
int ok;
FILE *fp;
GraphDriver = DETECT;
initgraph(&GraphDriver,&GraphMode,"");
x[0] = -5;
x[1] = 5;
setcolor(12);
outtextxy(10,20,"Times:");
for (i = 0; i < 20; i++)
{
setcolor(0);
outtextxy(60,20,buffer);
sprintf(buffer,"%d ",i + 1);
setcolor(12);
outtextxy(60,20,buffer);
drawchaline(n,0);
ok = chazhi(&n,deta);
drawchaline(n,14);
drawline(12);
getch();
if (ok == 1)
break;
}
if ((fp = fopen(szfilename,"wt")) != NULL)
{
for (i = 0; i < n; i++)
{
fprintf(fp,"%f,%f\n",x,fun(x[i]));
}
fclose(fp);
}
closegraph();
return 0;
}
float xchange(float x)
{
const float fdiv = (-left + right) / 10.0f;
return (x + 5) * fdiv + left;
}
float ychange(float y)
{
const float fdiv = (top - bottom) / 200f;
return top + (y - 200) * fdiv;
}
void drawchaline(int n,int color)
{
int i;
setcolor(color);
moveto(xchange(x[0]),ychange(fun(-5)));
for (i = 1; i < n; i++)
{
lineto(xchange(x[i]),ychange(fun(x[i])));
}
}
/*min:x:-1.6,y:-17.8*/
void drawline(int color)
{
float x;
setcolor(color);
moveto(xchange(-5),ychange(fun(-5)));
for (x = -5; x deta)
{
return 0;
}
}
return 1;