//依次以0.01、0.001、0.0001、0.00001为间隔在上次的最优结果附近搜索所有的可能性
//Keil的浮点数是单精度的(double也是一样),所以只能精确到小数点后面6位 //因此在误差范围内可以有很多个解。 //而最典型的2组结果就是1.105403,1.804127 和1.107604,1.808030,明显有差别,一个V1大一点点,一个V2大一点点:-)
//test31.c
#include <REG52.H> /* special function register declarations */ /* for the intended 8051 derivative */
#include <stdio.h> /* prototype declarations for I/O functions */
float min(float x,float y) { if(x<y) return(x); else return(y); }
/*------------------------------------------------ The main C function. Program execution starts here after stack initialization. ------------------------------------------------*/ void main (void) {
float r1,r2; float best_r1,best_r2; float va,vb; float maxa,maxb; unsigned int i,j;
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 254; /* TH1: reload value for 1200 baud @ 16MHz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */
while (1) { P1 ^= 0x01; /* Toggle P1.0 each time we print */ printf ("Hello World
"); /* Print "Hello World" */ maxa=maxb=0; r1=0.01; r2=0.01; for(i=0;i<120;i++) { for(j=i;j<200;j++) { va=5/(1+r1)-5/(1+r2); vb=5/(1+r1*r2/(r1+r2))-5/(1+r1); if (min(va,vb) >=min(maxa,maxb)) { maxa=va; maxb=vb; best_r1=r1; best_r2=r2; printf ("r1=%f,r2=%f Va=%f,", r1, r2,va); printf ("Vb=%f maxa=%f,maxb=%f
", vb,maxa,maxb); } // printf ("r1=%f,r2=%f Va=%f,", r1, r2,va); // printf ("Vb=%f maxa=%f,maxb=%f
", vb,maxa,maxb); r2 +=0.01; } r1+=0.01; r2=r1; } printf ("best_r1=%f,best_r2=%f
", best_r1, best_r2); printf ("best_a=%f,best_b=%f
",maxa ,maxb ); // r1=best_r1-0.1; r1=1.07; r2=1.8; for(i=0;i<300;i++) { for(j=i;j<300;j++) { va=5/(1+r1)-5/(1+r2); vb=5/(1+r1*r2/(r1+r2))-5/(1+r1); if (min(va,vb) >=min(maxa,maxb)) { maxa=va; maxb=vb; best_r1=r1; best_r2=r2; printf ("r1=%f,r2=%f Va=%f,", r1, r2,va); printf ("Vb=%f maxa=%f,maxb=%f
", vb,maxa,maxb); } r2 +=0.001; } r1+=0.001; r2=1.8; } printf ("best_r1=%f,best_r2=%f
", best_r1, best_r2); printf ("best_a=%f,best_b=%f
",maxa ,maxb ); r1=best_r1-0.01; r2=best_r2-0.02; for(i=0;i<200;i++) { for(j=i;j<500;j++) { va=5/(1+r1)-5/(1+r2); vb=5/(1+r1*r2/(r1+r2))-5/(1+r1); if (min(va,vb) >=min(maxa,maxb)) { maxa=va; maxb=vb; best_r1=r1; best_r2=r2; printf ("r1=%f,r2=%f Va=%f,", r1, r2,va); printf ("Vb=%f maxa=%f,maxb=%f
", vb,maxa,maxb); } r2 +=0.0001; } // r1=1.107+(float)(i)/10000; r1+=0.0001; r2=best_r2-0.02; } printf ("best_r1=%f,best_r2=%f
", best_r1, best_r2); printf ("best_a=%f,best_b=%f
",maxa ,maxb ); r1=best_r1-0.001; r2=best_r2-0.002; for(i=0;i<200;i++) { for(j=i;j<500;j++) { va=5/(1+r1)-5/(1+r2); vb=5/(1+r1*r2/(r1+r2))-5/(1+r1); if (min(va,vb) >=min(maxa,maxb)) { maxa=va; maxb=vb; best_r1=r1; best_r2=r2; printf ("r1=%f,r2=%f Va=%f,", r1, r2,va); printf ("Vb=%f maxa=%f,maxb=%f
", vb,maxa,maxb); } r2 +=0.00001; } // r1=1.107+(float)(i)/10000; r1+=0.00001; r2=best_r2-0.002; } printf ("best_r1=%f,best_r2=%f
", best_r1, best_r2); printf ("best_a=%f,best_b=%f
",maxa ,maxb ); while (1); } }
//本程序由xwj设计的UltraEdit脚本加亮显示,如需要脚本请访问我的Blog或联系xwjfile@21cn.com |