用C语言来编进退法的搜索区间

用C语言来编进退法的搜索区间,第1张

用C语言来编进退法的搜索区间的源代码如下:

#include<stdio.h>

void main()

{

int a[20],x,i,start,end

printf("input 20 numbers:\n")

for(i=0i<20i++) scanf("%d",&a[i])

printf("please enter the number:\n")

scanf("%d",&x)

for(start=0,end=19start<=end)

{

i=start+(end-start)/2

if (x==a[i])

{

printf("%d",i+1)

getch()

return

}

else if (x>a[i]) end = i-1

else start=i+1

}

扩展资料

1、C语言查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找。

2、其实二分查找、插值查找以及斐波那契查找都可以归为一类——插值查找。插值查找和斐波那契查找是在二分查找的基础上的优化查找算法。

#include<stdio.h>

#include<math.h>

void main()

{

void function1()//搜索法

void function2()//二分法

void function4()//牛顿法

int choice

printf("请选择求解的方法:\n\t1.搜索法\n\t2.二分法\n\t3.牛顿法\n:")

switch(1)

{

case 1:function1()

case 2:function2()

case 4:function4()

}

}

void function1()//搜索法计算非线性方程的解

{

double expression1(double)

double lpoint=1.0,rpoint=2.0,step=0.0001

while(expression1(lpoint)<-0.00001)

{

lpoint=lpoint+step

}

printf("运用搜索法所求结果:%f\n",lpoint)

}

void function2()//二分法计算非线性方程的解

{

double expression1(double)

double lpoint=1,rpoint=2,mpoint

mpoint=(lpoint+rpoint)/2

while(fabs(expression1(mpoint))>0.00001)

{

mpoint=(lpoint+rpoint)/2

if(expression1(lpoint)*expression1(mpoint)<0)

rpoint=mpoint

else

lpoint=mpoint

}

printf("运用二分法所求结果:%f\n",mpoint)

}

void function4()//牛顿法计算非线性方程的解

{

double expression1(double)

double expression2(double)

double x=1.5

while(expression1(x)>0.00001)

{

x=x-expression1(x)/expression2(x)

}

printf("运用牛顿法所求结果:%f\n",x)

}

double expression1(double x)

{

double result

result=x*x*x-x*x-1

return result

}

double expression2(double x)

{

double result

result=3*x*x-2*x

return result

把以下程序存为f618.m,再运行>>[x,min]=F618(0.2,0,1)

得到:x=1,min=0

function

[x,min]=F618(precision,t0,h)

%初始区间为a,b,相对精度precision

即为原区间的多少分之,t0为初始值,h为步长

%用进退法得到高低高区间,在用0.618法求最值

%precision=0.2

%t0=0

%h=1

[a,b]=Fpush(t0,h)

An=a

Bn=b

n=1

while

0.618^(n-1)>=precision

A=0.618.*(An-Bn)+Bn

B=0.618.*(Bn-An)+An

if

A<B

if

F618Ret(A)<F618Ret(B)

Bn=B

else

An=A

end

else

if

F618Ret(A)<F618Ret(B)

An=A

else

Bn=B

end

end

n=n+1

end

x=An

min=F618Ret(An)

%=======================================

function

y=F618Ret(x)

%函数为f(x)=t^2-10*t+36

严格凸函数

y=x.^2-2*x+2

%======================================

function

[x1,x2]=Fpush(t0,h)

%进退法,调用函数为,F618Ret,t0为初始值,h为步长

a=t0

b=t0+h

while

1

if

F618Ret(a)<F618Ret(b)

h=0-h

a=a+h

b=a-h

if

F618Ret(b)<F618Ret(b-h)

&

F618(b)<F618Ret(a)

break

end

else

a=b

b=a+h

if

F618Ret(a)<F618Ret(b)

&

F618Ret(a)<F618Ret(a-h)

break

end

end

end

x1=a

x2=b


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7775099.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存