新手求帮忙看一下外推法加格点法求函数最小值为什么数字输入大了就错
小点的数值都还好,老师要求6位以内都要对,我一个机械的真心弄得人都要傻了。大神帮忙给瞅一下。我真的心好累,本来就是一个忽悠的实验但是老师较真了求帮忙#include<stdio.h>
double f(double t)
{ float z;
z=t*t-10*t+36;
return z;
}
void main()
{float h,a1,a2,a3,y1,y2,y3;
printf("请输入h,a1的值\n");
scanf("%f %f",&h,&a1);
y1=f(a1);
a2=a1+h;
y2=f(a2);
if(y2>y1)
{
h=-h;a3=a1;y3=y1;
a1=a2;y1=y2;a2=a3;y2=y3;
}
a3=a2+h;y3=f(a3);
while(y3<y2)
{
h=2*h;
a1=a2;y1=y2;a2=a3;y2=y3;
a3=a2+h;y3=f(a3);
}
printf("f(%f)=%lf\nf(%f)=%lf\nf(%f)=%lf",a1,y1,a2,y2,a3,y3);
float a,b,c,E,temp;
a=a1;b=a3;
int n;
printf("请输入n,E的值\n");
scanf("%d %f",&n,&E);
double x[9999];
double y[9999];
temp=f(a1);c=a;
while((b-a)>E)
{int i;x[0]=a;
for(i=1;i<=n;i++)
{x[i]=x[i-1]+(b-a)/n;
y[i]=f(x[i]);
if(y[i]<temp)
{temp=y[i];
c=x[i];
}
}
a=c-(b-a)/n;
b=c+(b-a)/n;
}
printf("格点法:\n");
printf("a[m-1]=%lf,a[m]=%lf,a[m+1]=%lf,f(a[m])=%lf",a,c,b,temp);
}